{"id":135105,"date":"2024-10-31T07:53:43","date_gmt":"2024-10-30T23:53:43","guid":{"rendered":"https:\/\/server.hk\/cnblog\/135105\/"},"modified":"2024-10-31T07:53:44","modified_gmt":"2024-10-30T23:53:44","slug":"spring%e9%80%b2%e9%9a%8e%ef%bc%9a%e4%b8%89%e6%ad%a5%e6%90%9e%e5%ae%9amybatisplus%e5%a4%9a%e6%95%b8%e6%93%9a%e6%ba%90%ef%bc%8c%e8%a9%b3%e7%b4%b0%e9%85%8d%e7%bd%ae%e5%8f%8a%e5%8e%9f%e7%90%86%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/135105\/","title":{"rendered":"Spring\u9032\u968e\uff1a\u4e09\u6b65\u641e\u5b9aMybatisPlus\u591a\u6578\u64da\u6e90\uff0c\u8a73\u7d30\u914d\u7f6e\u53ca\u539f\u7406\u89e3\u6790"},"content":{"rendered":"<h1 id=\"spring%e9%80%b2%e9%9a%8e%ef%bc%9a%e4%b8%89%e6%ad%a5%e6%90%9e%e5%ae%9amybatisplus%e5%a4%9a%e6%95%b8%e6%93%9a%e6%ba%90%ef%bc%8c%e8%a9%b3%e7%b4%b0%e9%85%8d%e7%bd%ae%e5%8f%8a%e5%8e%9f%e7%90%86%e8%a7%a3-dHkoPEWluT\">Spring\u9032\u968e\uff1a\u4e09\u6b65\u641e\u5b9aMybatisPlus\u591a\u6578\u64da\u6e90\uff0c\u8a73\u7d30\u914d\u7f6e\u53ca\u539f\u7406\u89e3\u6790<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u9ad4\u958b\u767c\u4e2d\uff0c\u6578\u64da\u6e90\u7684\u7ba1\u7406\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u5c24\u5176\u662f\u5728\u5fae\u670d\u52d9\u67b6\u69cb\u4e2d\uff0c\u61c9\u7528\u7a0b\u5e8f\u53ef\u80fd\u9700\u8981\u9023\u63a5\u591a\u500b\u6578\u64da\u5eab\u3002MybatisPlus\u4f5c\u70ba\u4e00\u500b\u5f37\u5927\u7684ORM\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u7c21\u5316\u6578\u64da\u64cd\u4f5c\u7684\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Spring\u4e2d\u914d\u7f6eMybatisPlus\u4ee5\u652f\u6301\u591a\u6578\u64da\u6e90\uff0c\u4e26\u89e3\u6790\u5176\u80cc\u5f8c\u7684\u539f\u7406\u3002<\/p>\n<h2 id=\"%e7%ac%ac%e4%b8%80%e6%ad%a5%ef%bc%9a%e6%b7%bb%e5%8a%a0%e4%be%9d%e8%b3%b4-dHkoPEWluT\">\u7b2c\u4e00\u6b65\uff1a\u6dfb\u52a0\u4f9d\u8cf4<\/h2>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u7684\u9805\u76ee\u4e2d\u5df2\u7d93\u6dfb\u52a0\u4e86MybatisPlus\u548cSpring Boot\u7684\u76f8\u95dc\u4f9d\u8cf4\u3002\u5728\u4f60\u7684<code>pom.xml<\/code>\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4f9d\u8cf4\uff1a<\/p>\n<pre><code>\n\n    com.baomidou\n    mybatis-plus-boot-starter\n    3.4.3\n\n\n    org.springframework.boot\n    spring-boot-starter-data-jpa\n\n\n    mysql\n    mysql-connector-java\n    runtime\n\n<\/code><\/pre>\n<h2 id=\"%e7%ac%ac%e4%ba%8c%e6%ad%a5%ef%bc%9a%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e6%ba%90-dHkoPEWluT\">\u7b2c\u4e8c\u6b65\uff1a\u914d\u7f6e\u6578\u64da\u6e90<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5728<code>application.yml<\/code>\u4e2d\u914d\u7f6e\u591a\u500b\u6578\u64da\u6e90\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\u914d\u7f6e\uff1a<\/p>\n<pre><code>\nspring:\n  datasource:\n    first:\n      url: jdbc:mysql:\/\/localhost:3306\/first_db\n      username: root\n      password: password\n      driver-class-name: com.mysql.cj.jdbc.Driver\n    second:\n      url: jdbc:mysql:\/\/localhost:3306\/second_db\n      username: root\n      password: password\n      driver-class-name: com.mysql.cj.jdbc.Driver\n<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u5169\u500b\u6578\u64da\u6e90\uff1afirst\u548csecond\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u6578\u64da\u6e90\u7684\u914d\u7f6e\u985e\u3002<\/p>\n<h3 id=\"%e6%95%b8%e6%93%9a%e6%ba%90%e9%85%8d%e7%bd%ae%e9%a1%9e-dHkoPEWluT\">\u6578\u64da\u6e90\u914d\u7f6e\u985e<\/h3>\n<p>\u5275\u5efa\u4e00\u500b\u914d\u7f6e\u985e\u4f86\u7ba1\u7406\u9019\u4e9b\u6578\u64da\u6e90\uff1a<\/p>\n<pre><code>\nimport com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;\nimport com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;\nimport org.springframework.beans.factory.annotation.Qualifier;\nimport org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;\nimport org.springframework.boot.context.properties.ConfigurationProperties;\nimport org.springframework.boot.jdbc.DataSourceBuilder;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.context.annotation.Primary;\nimport org.springframework.jdbc.datasource.DataSourceTransactionManager;\nimport org.springframework.orm.jpa.JpaTransactionManager;\nimport org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;\n\nimport javax.sql.DataSource;\n\n@Configuration\npublic class DataSourceConfig {\n\n    @Bean(name = \"firstDataSource\")\n    @Primary\n    @ConfigurationProperties(\"spring.datasource.first\")\n    public DataSource firstDataSource() {\n        return DataSourceBuilder.create().build();\n    }\n\n    @Bean(name = \"secondDataSource\")\n    @ConfigurationProperties(\"spring.datasource.second\")\n    public DataSource secondDataSource() {\n        return DataSourceBuilder.create().build();\n    }\n\n    @Bean(name = \"firstTransactionManager\")\n    @Primary\n    public DataSourceTransactionManager firstTransactionManager(@Qualifier(\"firstDataSource\") DataSource dataSource) {\n        return new DataSourceTransactionManager(dataSource);\n    }\n\n    @Bean(name = \"secondTransactionManager\")\n    public DataSourceTransactionManager secondTransactionManager(@Qualifier(\"secondDataSource\") DataSource dataSource) {\n        return new DataSourceTransactionManager(dataSource);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%ac%ac%e4%b8%89%e6%ad%a5%ef%bc%9a%e4%bd%bf%e7%94%a8mybatisplus%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e6%93%8d%e4%bd%9c-dHkoPEWluT\">\u7b2c\u4e09\u6b65\uff1a\u4f7f\u7528MybatisPlus\u9032\u884c\u6578\u64da\u64cd\u4f5c<\/h2>\n<p>\u5728\u914d\u7f6e\u597d\u6578\u64da\u6e90\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528MybatisPlus\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u4e0d\u540c\u7684\u6578\u64da\u6e90\u4e0a\u57f7\u884c\u67e5\u8a62\uff1a<\/p>\n<pre><code>\nimport com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class UserService {\n\n    @Autowired\n    private UserMapper userMapper; \/\/ \u5047\u8a2d\u9019\u662f\u4f60\u7684Mapper\u63a5\u53e3\n\n    public User getUserFromFirstDataSource(Long id) {\n        return userMapper.selectById(id);\n    }\n\n    public User getUserFromSecondDataSource(Long id) {\n        \/\/ \u9019\u88e1\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684Mapper\u6216\u914d\u7f6e\u4f86\u64cd\u4f5c\u7b2c\u4e8c\u6578\u64da\u6e90\n        return userMapper.selectById(id);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e5%8e%9f%e7%90%86%e8%a7%a3%e6%9e%90-dHkoPEWluT\">\u539f\u7406\u89e3\u6790<\/h2>\n<p>MybatisPlus\u652f\u6301\u591a\u6578\u64da\u6e90\u7684\u539f\u7406\u4e3b\u8981\u4f9d\u8cf4\u65bcSpring\u7684<code>AbstractRoutingDataSource<\/code>\u3002\u901a\u904e\u91cd\u5beb<code>determineCurrentLookupKey<\/code>\u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u6839\u64da\u7576\u524d\u7684\u4e0a\u4e0b\u6587\u4f86\u6c7a\u5b9a\u4f7f\u7528\u54ea\u500b\u6578\u64da\u6e90\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u4f7f\u5f97\u5728\u904b\u884c\u6642\u53ef\u4ee5\u9748\u6d3b\u5730\u5207\u63db\u6578\u64da\u6e90\uff0c\u6eff\u8db3\u4e0d\u540c\u7684\u696d\u52d9\u9700\u6c42\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-dHkoPEWluT\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u4e09\u500b\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u5728Spring\u4e2d\u6210\u529f\u914d\u7f6eMybatisPlus\u4ee5\u652f\u6301\u591a\u6578\u64da\u6e90\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u64cd\u4f5c\u7684\u9748\u6d3b\u6027\uff0c\u4e5f\u70ba\u5fae\u670d\u52d9\u67b6\u69cb\u4e2d\u7684\u6578\u64da\u7ba1\u7406\u63d0\u4f9b\u4e86\u4fbf\u5229\u3002\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684<code><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/code>\u4f86\u90e8\u7f72\u4f60\u7684\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u4f60\u8f15\u9b06\u7ba1\u7406\u4f60\u7684\u6578\u64da\u6e90\u548c\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1Spring\u9032\u968e\u6280\u5de7\uff0c\u4e09\u6b65\u9a5f\u8f15\u9b06\u914d\u7f6eMybatisPlus\u591a\u6578\u64da\u6e90\uff0c\u6df1\u5165\u89e3\u6790\u8a73\u7d30\u914d\u7f6e\u8207\u539f\u7406\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-135105","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=135105"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135105\/revisions"}],"predecessor-version":[{"id":135106,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135105\/revisions\/135106"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=135105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=135105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=135105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}