{"id":1959,"date":"2023-04-01T10:24:27","date_gmt":"2023-04-01T02:24:27","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1959"},"modified":"2023-04-22T08:38:05","modified_gmt":"2023-04-22T00:38:05","slug":"sharding-sphere-sharding-jdbc-sub-database-and-sub-table-based-on-mybatis","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/sharding-sphere-sharding-jdbc-sub-database-and-sub-table-based-on-mybatis\/","title":{"rendered":"Sharding-Sphere\uff1aSharding-JDBC\u5206\u5e93\u5206\u8868\uff08\u57fa\u4e8eMyBatis\uff09"},"content":{"rendered":"<p>\u5206\u4eab\u4e86 Sharding-JDBC \u642d\u5efa\u8bfb\u5199\u5206\u79bb\uff0c\u672c\u7ae0\u5c06\u7528 Sharding-JDBC \u5b9e\u73b0\u5206\u5e93\u5206\u8868\u529f\u80fd<\/p>\n<h2>\u4e3a\u4ec0\u4e48\u8981\u5206\u5e93\u5206\u8868\uff1f<\/h2>\n<p>\u4e3e\u4e2a\u6817\u5b50\uff1a\u5f53\u5355\u8868\u6570\u636e\u91cf\u8d85\u8fc71000\u4e07\u540e\uff0c\u67e5\u8be2\u7684\u901f\u5ea6\u5c06\u4f1a\u6162\u4e0b\u6765\uff0c\u5229\u7528\u7d22\u5f15\uff0c\u8bfb\u5199\u5206\u79bb\u7b49\u4f18\u5316\uff0c\u5982\u679c\u5f53\u6570\u636e\u91cf\u8d85\u8fc75000\u4e07\u65f6\uff0c\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\u5c06\u5931\u53bb\u4f5c\u7528\uff0c\u6b64\u65f6\u628a\u5355\u8868\u6c34\u5e73\u5212\u5206\u5230\u591a\u5e93\u591a\u8868\u4e2d\uff0c\u63d0\u5347\u67e5\u8be2\u901f\u5ea6\uff0c\u800csharding-jdbc\u53ef\u4ee5\u5e2e\u52a9\u5b8c\u6210\u6c34\u5e73\u62c6\u5206\uff0c\u800c\u6211\u4eec\u53ea\u9700\u6dfb\u52a0\u4e00\u4e9b\u914d\u7f6e\u5373\u53ef<\/p>\n<p><!-- more --><\/p>\n<h2>\u5b98\u65b9\u6587\u6863<\/h2>\n<p>GitHub\u7684\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/apache\/incubator-shardingsphere\">https:\/\/github.com\/apache\/incubator-shardingsphere<\/a><br \/>\nshardingsphere\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/apache\/incubator-shardingsphere-example\">https:\/\/github.com\/apache\/incubator-shardingsphere-example<\/a><br \/>\n\u4e2d\u6587\u6587\u6863\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/shardingsphere.apache.org\/document\/current\/cn\/overview\/\">https:\/\/shardingsphere.apache.org\/document\/current\/cn\/overview\/<\/a><\/p>\n<h2>\u4ee3\u7801\u5b9e\u73b0\uff08MyBatis\uff09<\/h2>\n<p>\uff081\uff09\u67093\u5f20\u8868<code>t_member<\/code>\u3001<code>t_order<\/code>\u3001<code>t_order_item<\/code>\uff0c\u62c6\u5206\u52302\u4e2a\u6570\u636e\u5e93\u4e2d\uff0c\u6bcf\u4e2a\u6570\u636e\u5e932\u4efd\uff0c\u76f8\u5f53\u4e8e\u6bcf\u5f20\u8868\u62c6\u6210\u4e864\u5f20\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u6bcf\u4e2a\u8868\u7684\u4e3b\u952eId\u5c3d\u91cf\u8bbe\u7f6e\u6210 bigint \uff08\u6211\u5f00\u59cb\u8bbe\u7f6e\u4e3avarchar\uff0c\u7ed3\u679c\u5206\u8868\u65f6\u62a5\u9519\uff09\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/\u5206\u5e93\u5206\u8868\u8bbe\u8ba1.png\" alt=\"\u5206\u5e93\u5206\u8868\u8bbe\u8ba1\" \/><\/p>\n<pre><code class=\"language-sql\">-- ----------------------------\n-- Table structure for t_member0\n-- ----------------------------\nDROP TABLE IF EXISTS `t_member0`;\nCREATE TABLE `t_member0` (\n  `member_id` bigint(20) NOT NULL,\n  `member_name` varchar(36) DEFAULT NULL,\n  `nick_name` varchar(36) DEFAULT NULL,\n  `account_no` varchar(20) DEFAULT NULL,\n  `password` varchar(20) DEFAULT NULL,\n  `age` int(11) DEFAULT NULL,\n  `birth_date` datetime DEFAULT NULL,\n  `ebl_flag` varchar(1) DEFAULT &#039;1&#039;,\n  `del_flag` varchar(1) DEFAULT &#039;0&#039;,\n  `description` varchar(255) DEFAULT NULL,\n  `create_time` datetime DEFAULT NULL,\n  `update_time` datetime DEFAULT NULL,\n  PRIMARY KEY (`member_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=&#039;\u4f1a\u5458\u8868&#039;;\n\n-- ----------------------------\n-- Table structure for t_member1\n-- ----------------------------\nDROP TABLE IF EXISTS `t_member1`;\nCREATE TABLE `t_member1` (\n  `member_id` bigint(20) NOT NULL,\n  `member_name` varchar(36) DEFAULT NULL,\n  `nick_name` varchar(36) DEFAULT NULL,\n  `account_no` varchar(20) DEFAULT NULL,\n  `password` varchar(20) DEFAULT NULL,\n  `age` int(11) DEFAULT NULL,\n  `birth_date` datetime DEFAULT NULL,\n  `ebl_flag` varchar(1) DEFAULT &#039;1&#039;,\n  `del_flag` varchar(1) DEFAULT &#039;0&#039;,\n  `description` varchar(255) DEFAULT NULL,\n  `create_time` datetime DEFAULT NULL,\n  `update_time` datetime DEFAULT NULL,\n  PRIMARY KEY (`member_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=&#039;\u4f1a\u5458\u8868&#039;;\n\n-- ----------------------------\n-- Table structure for t_order0\n-- ----------------------------\nDROP TABLE IF EXISTS `t_order0`;\nCREATE TABLE `t_order0` (\n  `order_id` bigint(20) NOT NULL,\n  `member_id` varchar(36) DEFAULT NULL,\n  `order_code` varchar(36) DEFAULT NULL,\n  `order_amount` varchar(20) DEFAULT NULL,\n  `status` varchar(1) DEFAULT &#039;1&#039;,\n  `create_time` datetime DEFAULT NULL,\n  PRIMARY KEY (`order_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=&#039;\u8ba2\u5355\u8868&#039;;\n\n-- ----------------------------\n-- Table structure for t_order1\n-- ----------------------------\nDROP TABLE IF EXISTS `t_order1`;\nCREATE TABLE `t_order1` (\n  `order_id` bigint(20) NOT NULL,\n  `member_id` varchar(36) DEFAULT NULL,\n  `order_code` varchar(36) DEFAULT NULL,\n  `order_amount` double DEFAULT NULL,\n  `status` varchar(1) DEFAULT &#039;1&#039;,\n  `create_time` datetime DEFAULT NULL,\n  PRIMARY KEY (`order_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=&#039;\u8ba2\u5355\u8868&#039;;\n\n-- ----------------------------\n-- Table structure for t_order_item0\n-- ----------------------------\nDROP TABLE IF EXISTS `t_order_item0`;\nCREATE TABLE `t_order_item0` (\n  `item_id` bigint(20) NOT NULL,\n  `order_id` varchar(36) DEFAULT NULL,\n  `product_name` varchar(128) DEFAULT NULL,\n  `item_account` double DEFAULT NULL,\n  `create_time` datetime DEFAULT NULL,\n  PRIMARY KEY (`item_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=&#039;\u8ba2\u5355\u8be6\u60c5\u8868&#039;;\n\n-- ----------------------------\n-- Table structure for t_order_item1\n-- ----------------------------\nDROP TABLE IF EXISTS `t_order_item1`;\nCREATE TABLE `t_order_item1` (\n  `item_id` bigint(20) NOT NULL,\n  `order_id` varchar(36) DEFAULT NULL,\n  `product_name` varchar(128) DEFAULT NULL,\n  `item_account` double DEFAULT NULL,\n  `create_time` datetime DEFAULT NULL,\n  PRIMARY KEY (`item_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=&#039;\u8ba2\u5355\u8be6\u60c5\u8868&#039;;<\/code><\/pre>\n<p>\uff082\uff09\u65b0\u5efaSpringBoot\u9879\u76ee\uff0c\u5f15\u5165\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.shardingsphere&lt;\/groupId&gt;\n    &lt;artifactId&gt;sharding-jdbc-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;version&gt;4.0.0-RC1&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.mybatis.spring.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;mybatis-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;version&gt;2.0.0&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;mysql&lt;\/groupId&gt;\n    &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\n    &lt;version&gt;5.1.47&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;!-- https:\/\/mvnrepository.com\/artifact\/com.alibaba\/druid --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;\n    &lt;artifactId&gt;druid&lt;\/artifactId&gt;\n    &lt;version&gt;1.1.16&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;\n    &lt;artifactId&gt;fastjson&lt;\/artifactId&gt;\n    &lt;version&gt;1.2.47&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;\n    &lt;artifactId&gt;lombok&lt;\/artifactId&gt;\n    &lt;optional&gt;true&lt;\/optional&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\uff083\uff09\u5f15\u5165 mybatis generator \u81ea\u52a8\u751f\u6210\u4ee3\u7801\u63d2\u4ef6\uff0c\u751f\u6210\u5b9e\u4f53\u7c7b\u4e0emybatis xml\u6587\u4ef6<\/p>\n<pre><code class=\"language-xml\">&lt;plugin&gt;\n    &lt;groupId&gt;org.mybatis.generator&lt;\/groupId&gt;\n    &lt;artifactId&gt;mybatis-generator-maven-plugin&lt;\/artifactId&gt;\n    &lt;version&gt;1.3.6&lt;\/version&gt;\n    &lt;configuration&gt;\n        &lt;configurationFile&gt;${basedir}\/src\/main\/resources\/generatorConfig.xml&lt;\/configurationFile&gt;\n        &lt;overwrite&gt;true&lt;\/overwrite&gt;\n        &lt;verbose&gt;true&lt;\/verbose&gt;\n    &lt;\/configuration&gt;\n&lt;\/plugin&gt;<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE generatorConfiguration\n        PUBLIC &quot;-\/\/mybatis.org\/\/DTD MyBatis Generator Configuration 1.0\/\/EN&quot;\n        &quot;http:\/\/mybatis.org\/dtd\/mybatis-generator-config_1_0.dtd&quot;&gt;\n&lt;generatorConfiguration&gt;\n    &lt;!-- \u6570\u636e\u5e93\u9a71\u52a8:\u9009\u62e9\u4f60\u7684\u672c\u5730\u786c\u76d8\u4e0a\u9762\u7684\u6570\u636e\u5e93\u9a71\u52a8\u5305--&gt;\n    &lt;classPathEntry  location=&quot;D:\\software\\database\\mybatis-generator-core-1.3.2\\lib\\mysql-connector-java-5.1.25-bin.jar&quot;\/&gt;\n    &lt;context id=&quot;DB2Tables&quot;  targetRuntime=&quot;MyBatis3&quot; defaultModelType=&quot;flat&quot;&gt;\n        &lt;commentGenerator&gt;\n            &lt;property name=&quot;suppressDate&quot; value=&quot;true&quot;\/&gt;\n            &lt;!-- \u662f\u5426\u53bb\u9664\u81ea\u52a8\u751f\u6210\u7684\u6ce8\u91ca true: \u662f, false: \u5426 --&gt;\n            &lt;property name=&quot;suppressAllComments&quot; value=&quot;true&quot;\/&gt;\n        &lt;\/commentGenerator&gt;\n        &lt;!--\u6570\u636e\u5e93\u94fe\u63a5URL\uff0c\u7528\u6237\u540d\u3001\u5bc6\u7801 --&gt;\n        &lt;jdbcConnection driverClass=&quot;com.mysql.jdbc.Driver&quot; userId=&quot;xnxx&quot; password=&quot;Xnxx85781245!&quot; connectionURL=&quot;jdbc:mysql:\/\/47.107.177.150:3306\/zy_sharding_business1?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf8&amp;maxReconnects=15000&amp;allowMultiQueries=true&quot;&gt;\n        &lt;\/jdbcConnection&gt;\n        &lt;javaTypeResolver&gt;\n            &lt;property name=&quot;forceBigDecimals&quot; value=&quot;false&quot;\/&gt;\n        &lt;\/javaTypeResolver&gt;\n        &lt;!-- \u751f\u6210\u6a21\u578b\u7684\u5305\u540d\u548c\u4f4d\u7f6e--&gt;\n        &lt;javaModelGenerator targetPackage=&quot;com.zypcy.sharding.sublibrarytable.entity&quot; targetProject=&quot;src\/main\/java&quot;&gt;\n            &lt;property name=&quot;enableSubPackages&quot; value=&quot;true&quot;\/&gt;\n            &lt;property name=&quot;trimStrings&quot; value=&quot;true&quot;\/&gt;\n        &lt;\/javaModelGenerator&gt;\n        &lt;!-- \u751f\u6210\u6620\u5c04\u6587\u4ef6\u7684\u5305\u540d\u548c\u4f4d\u7f6e--&gt;\n        &lt;sqlMapGenerator targetPackage=&quot;mapping&quot; targetProject=&quot;src\/main\/resources&quot;&gt;\n            &lt;property name=&quot;enableSubPackages&quot; value=&quot;true&quot;\/&gt;\n        &lt;\/sqlMapGenerator&gt;\n        &lt;!-- \u751f\u6210DAO\u7684\u5305\u540d\u548c\u4f4d\u7f6e--&gt;\n        &lt;javaClientGenerator type=&quot;XMLMAPPER&quot; targetPackage=&quot;com.zypcy.sharding.sublibrarytable.repository&quot; targetProject=&quot;src\/main\/java&quot;&gt;\n            &lt;property name=&quot;enableSubPackages&quot; value=&quot;true&quot;\/&gt;\n        &lt;\/javaClientGenerator&gt;\n        &lt;!-- \u8981\u751f\u6210\u7684\u8868 tableName\u662f\u6570\u636e\u5e93\u4e2d\u7684\u8868\u540d\u6216\u89c6\u56fe\u540d domainObjectName\u662f\u5b9e\u4f53\u7c7b\u540d--&gt;\n        &lt;!-- \u6ce8\u610f\uff1a\u5df2\u751f\u6210\u7684\u4e0d\u8981\u518d\u751f\u6210\uff0c\u4e0d\u7136\u4f1a\u8986\u76d6\u5df2\u751f\u6210\u7684\u6587\u4ef6 --&gt;\n        &lt;!--\n        &lt;table tableName=&quot;t_member&quot; domainObjectName=&quot;Member&quot; enableCountByExample=&quot;false&quot; enableUpdateByExample=&quot;false&quot; enableDeleteByExample=&quot;false&quot; enableSelectByExample=&quot;false&quot; selectByExampleQueryId=&quot;false&quot;&gt;&lt;\/table&gt;\n        &lt;table tableName=&quot;t_order&quot; domainObjectName=&quot;Order&quot; enableCountByExample=&quot;false&quot; enableUpdateByExample=&quot;false&quot; enableDeleteByExample=&quot;false&quot; enableSelectByExample=&quot;false&quot; selectByExampleQueryId=&quot;false&quot;&gt;&lt;\/table&gt;\n        &lt;table tableName=&quot;t_order_item&quot; domainObjectName=&quot;OrderItem&quot; enableCountByExample=&quot;false&quot; enableUpdateByExample=&quot;false&quot; enableDeleteByExample=&quot;false&quot; enableSelectByExample=&quot;false&quot; selectByExampleQueryId=&quot;false&quot;&gt;&lt;\/table&gt;\n        --&gt;\n    &lt;\/context&gt;\n&lt;\/generatorConfiguration&gt;<\/code><\/pre>\n<p>\uff084\uff09\u6dfb\u52a0\u914d\u7f6e\u6587\u4ef6\uff0c\u8fd9\u70b9\u5f88\u91cd\u8981\uff0c\u5426\u5219\u65e0\u6cd5\u5b9e\u73b0\u5206\u5e93\u5206\u8868\uff0c<code>ds$-&gt;{0..1}.t_member$-&gt;{0..1}<\/code>\uff0c\u8868\u8fbe\u5f0f\u76f8\u5f53\u4e8e\uff1a<code>ds0.t_member0<\/code>\u3001<code>ds0.t_member1<\/code>\u3001<code>ds1.t_member0<\/code>\u3001<code>ds1.t_member1<\/code>\uff0c\u5728\u914d\u7f6e\u4e2d\u914d\u7f6e\u4e86<code>key-generator.column<\/code>\uff0c\u6240\u4ee5\u65b0\u589e\u8be5\u8868\u6570\u636e\u65f6\uff0c\u4e0d\u9700\u8981\u7ed9\u4e3b\u952eid\u8d4b\u503c\uff0csharding-jdbc\u4f1a\u8c03\u7528\u5185\u7f6e\u7684SNOWFLAKE\u7b97\u6cd5\u751f\u6210\u5206\u5e03\u5f0fid\uff0c\u8bf7\u5f80\u4e0b\u770b<\/p>\n<pre><code>server.port=8071\n\n# mybatis \u914d\u7f6e\nmybatis.mapper-locations=classpath:mapping\/*.xml\nmybatis.type-aliases-package=com.zypcy.sharding.sublibrarytable.entity\n\nspring.shardingsphere.datasource.names=ds0,ds1\n\nspring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource\nspring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver\nspring.shardingsphere.datasource.ds0.url=jdbc:mysql:\/\/localhost:3306\/ds0?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;maxReconnects=15000&amp;allowMultiQueries=true&amp;useSSL=false\nspring.shardingsphere.datasource.ds0.username=root\nspring.shardingsphere.datasource.ds0.password=123456\n\nspring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource\nspring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver\nspring.shardingsphere.datasource.ds1.url=jdbc:mysql:\/\/localhost:3306\/ds1?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;maxReconnects=15000&amp;allowMultiQueries=true&amp;useSSL=false\nspring.shardingsphere.datasource.ds1.username=root\nspring.shardingsphere.datasource.ds1.password=123456\n\n# \u5206\u5e93\u914d\u7f6e\uff0c\u6839\u636emember_id\u5206\u5e93\nspring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=member_id\nspring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$-&gt;{member_id % 2}\n\n# \u5206\u8868\u914d\u7f6e\uff0c\u6839\u636emember_id\u5206\u8868\nspring.shardingsphere.sharding.tables.t_member.actual-data-nodes=ds$-&gt;{0..1}.t_member$-&gt;{0..1}\nspring.shardingsphere.sharding.tables.t_member.table-strategy.inline.sharding-column=member_id\nspring.shardingsphere.sharding.tables.t_member.table-strategy.inline.algorithm-expression=t_member$-&gt;{member_id % 2}\nspring.shardingsphere.sharding.tables.t_member.key-generator.column=member_id\nspring.shardingsphere.sharding.tables.t_member.key-generator.type=SNOWFLAKE\n\n# \u5206\u8868\u914d\u7f6e\uff0c\u6839\u636eorder_id\u5206\u8868\nspring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$-&gt;{0..1}.t_order$-&gt;{0..1}\nspring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id\nspring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$-&gt;{order_id % 2}\nspring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id\nspring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE\n\n# \u5206\u8868\u914d\u7f6e\uff0c\u6839\u636eorder_id\u5206\u8868\nspring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds$-&gt;{0..1}.t_order_item$-&gt;{0..1}\nspring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id\nspring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item$-&gt;{order_id % 2}\nspring.shardingsphere.sharding.tables.t_order_item.key-generator.column=order_item_id\nspring.shardingsphere.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE\n\n#spring.shardingsphere.sharding.binding-tables=t_member,t_order,t_order_item\n#spring.shardingsphere.sharding.broadcast-tables=t_config\n\nspring.shardingsphere.props.sql.show=true<\/code><\/pre>\n<p>\uff085\uff09Durid\u914d\u7f6e<\/p>\n<pre><code class=\"language-java\">@Configuration\npublic class DuridConfig {\n\n    @Bean\n    public Filter statFilter() {\n        StatFilter filter = new StatFilter();\n        filter.setSlowSqlMillis(5000);\n        filter.setLogSlowSql(true);\n        filter.setMergeSql(true);\n        return filter;\n    }\n\n    @Bean\n    public ServletRegistrationBean statViewServlet() {\n        \/\/\u521b\u5efaservlet\u6ce8\u518c\u5b9e\u4f53\n        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), &quot;\/druid\/*&quot;);\n        \/\/\u8bbe\u7f6eip\u767d\u540d\u5355\n        servletRegistrationBean.addInitParameter(&quot;allow&quot;, &quot;127.0.0.1&quot;);\n        \/\/\u8bbe\u7f6eip\u9ed1\u540d\u5355\uff0c\u5982\u679callow\u4e0edeny\u5171\u540c\u5b58\u5728\u65f6,deny\u4f18\u5148\u4e8eallow\n        \/\/servletRegistrationBean.addInitParameter(&quot;deny&quot;,&quot;192.168.0.19&quot;);\n        \/\/\u8bbe\u7f6e\u63a7\u5236\u53f0\u7ba1\u7406\u7528\u6237\n        servletRegistrationBean.addInitParameter(&quot;loginUsername&quot;, &quot;admin&quot;);\n        servletRegistrationBean.addInitParameter(&quot;loginPassword&quot;, &quot;123456&quot;);\n        \/\/\u662f\u5426\u53ef\u4ee5\u91cd\u7f6e\u6570\u636e\n        servletRegistrationBean.addInitParameter(&quot;resetEnable&quot;, &quot;false&quot;);\n        return servletRegistrationBean;\n    }\n}<\/code><\/pre>\n<p>\uff086\uff09\u6dfb\u52a0MemberService\u670d\u52a1\u63a5\u53e3\u4e0e\u5b9e\u73b0<\/p>\n<pre><code class=\"language-java\">public interface IMemberService {\n\n    int insert(Member record);\n\n    Member selectByPrimaryKey(Long memberId);\n\n    int deleteByPrimaryKey(Long memberId);\n}\n\n@Service\npublic class MemberServiceImpl implements IMemberService {\n\n    @Autowired private MemberMapper memberMapper;\n\n    @Override\n    public int insert(Member record) {\n        return memberMapper.insertSelective(record);\n    }\n\n    @Override\n    public Member selectByPrimaryKey(Long memberId) {\n        return memberMapper.selectByPrimaryKey(memberId);\n    }\n\n    @Override\n    public int deleteByPrimaryKey(Long memberId) {\n        return memberMapper.deleteByPrimaryKey(memberId);\n    }\n}\n\n@Mapper\npublic interface MemberMapper {\n    int deleteByPrimaryKey(Long memberId);\n\n    int insert(Member record);\n\n    int insertSelective(Member record);\n\n    Member selectByPrimaryKey(Long memberId);\n\n    int updateByPrimaryKeySelective(Member record);\n\n    int updateByPrimaryKey(Member record);\n}<\/code><\/pre>\n<p>\uff087\uff09\u6dfb\u52a0MemberController<\/p>\n<pre><code class=\"language-java\">@RequestMapping(&quot;\/member&quot;)\n@RestController\npublic class MemberController {\n\n    @Autowired\n    private IMemberService memberService;\n\n    @RequestMapping(&quot;\/add&quot;)\n    public Member add(){\n        Member member = new Member();\n        \/\/member.setMemberId(IdWorker.getLongId()); \u4e0d\u7528\u624b\u52a8\u8bbe\u7f6e\u4e3b\u952eid\uff0c\u65b0\u589e\u65f6\uff0csharding-jdbc\u4f1a\u81ea\u52a8\u8d4b\u503c\uff0c\u56e0\u4e3a\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u4e86\u8be5\u5217\u4f7f\u7528SNOWFLAKE\u7b97\u6cd5\u751f\u6210\u503c\n        member.setMemberName(&quot;\u5f20\u4e09&quot;);\n        member.setNickName(&quot;\u95ea\u8000\u7684\u77ac\u95f4&quot;);\n        member.setAccountNo(member.getMemberId()+&quot;&quot;);\n        member.setPassword(&quot;123465&quot;);\n        member.setAge(27);\n        member.setBirthDate(new Date());\n        member.setEblFlag(&quot;1&quot;);\n        member.setDelFlag(&quot;0&quot;);\n        member.setDescription(&quot;xxx&quot;);\n        member.setCreateTime(new Date());\n        member.setUpdateTime(new Date());\n        memberService.insert(member);\n        return member;\n    }\n\n    @RequestMapping(&quot;\/findById&quot;)\n    public Member findById(Long memberId){\n        return memberService.selectByPrimaryKey(memberId);\n    }\n\n    @RequestMapping(&quot;\/delete&quot;)\n    public String delete(Long memberId){\n        memberService.deleteByPrimaryKey(memberId);\n        return &quot;success&quot;;\n    }\n}<\/code><\/pre>\n<p>\uff088\uff09\u542f\u52a8\u9879\u76ee\uff0c\u8bbf\u95ee\u65b0\u589e\u63a5\u53e3 <a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:8071\/member\/add\">http:\/\/localhost:8071\/member\/add<\/a> \uff0c\u53ef\u4ee5\u770b\u5230\u9875\u9762\u8fd4\u56de\u4e86\u65b0\u589e\u540e\u7684\u6570\u636e<\/p>\n<p>\u540c\u65f6\u67e5\u770bIntellij IDEA\u63a7\u5236\u53f0\uff0c\u53ef\u4ee5\u770b\u5230\u6570\u636e\u63d2\u5165\u5230<code>ds1<\/code>\u5e93\u7684<code>t_member1<\/code>\u8868\u4e86<\/p>\n<p>\u518d\u67e5\u770b<code>ds1<\/code>\u6570\u636e\u5e93\u7684<code>t_member1<\/code>\u8868\uff0c\u6709\u521a\u521a\u65b0\u589e\u7684\u8bb0\u5f55<\/p>\n<p>\uff089\uff09\u8bbf\u95ee\u67e5\u8be2\u63a5\u53e3 <a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:8071\/member\/findById?memberId=xxxxxx\">http:\/\/localhost:8071\/member\/findById?memberId=xxxxxx<\/a><\/p>\n<p>\u540c\u65f6\u67e5\u770b\u63a7\u5236\u53f0\uff0csql\u4f1a\u81ea\u52a8\u8def\u7531\u5230<code>ds1<\/code>\u5e93\u53bb\u67e5\u8be2\u6570\u636e<\/p>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/gitee.com\/zhuyu1991\/spring-cloud\/tree\/master\/sharding-business\/sub-library-table\">https:\/\/gitee.com\/zhuyu1991\/spring-cloud\/tree\/master\/sharding-business\/sub-library-table<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u4eab\u4e86 Sharding-JDBC \u642d\u5efa\u8bfb\u5199\u5206\u79bb\uff0c\u672c\u7ae0\u5c06\u7528 Sharding-JDBC \u5b9e\u73b0\u5206\u5e93\u5206\u8868\u529f\u80fd \u4e3a\u4ec0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[491],"tags":[111,436,492,299],"class_list":["post-1959","post","type-post","status-publish","format-standard","hentry","category-sharding-sphere","tag-mybatis","tag-sharding-jdbc","tag-sharding-sphere","tag-299"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1959","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1959"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1959\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}