A, Sharding-jdbc Introduction
1 Introduction
Sharding-jdbc is a client of the agency Dangdang CAPE source middleware. Sharding-jdbc fragmentation and sub-libraries comprising separate read and write functions. No application code invasive, almost no changes, orm framework compatible with the mainstream, the mainstream database connection pool. Currently part of the Apache incubator project ShardingSphere.
Sharding-jdbc positioned as a lightweight Java framework, additional services provided by Java's JDBC layer. It uses direct client database, provide services in the form of jar package, without additional deployment and dependence, understood as enhanced version of the JDBC driver, JDBC and is fully compatible with all kinds of ORM frameworks.
ORM framework for any JDBC-based, such as: JPA, Hibernate, Mybatis, Spring JDBC Template directly or JDBC.
Support any third-party database connection pool, such as: DBCP, C3P0, BoneCP, Druid , HikariCP and so on.
Support any implementation database JDBC specification. Currently supports MySQL, Oracle, SQLServer, PostgreSQL database as well as any follow SQL92 standard.
2, solution architecture
Two, Sharding-jdbc sub-library sub-table
1. Create two databases land1, land2, each containing t_user0, t_user1 two tables
DROP TABLE IF EXISTS `t_user0`; CREATE TABLE `t_user0` ( `id` bigint(20) NOT NULL, `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名称', city_id` ` int ( 12 ) NULL the DEFAULT NULL the COMMENT ' city ' , sex` ` tinyint ( . 1 ) NULL the DEFAULT NULL the COMMENT ' Sex ' , `phone` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '电话', `email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱', `create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间', `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8; DROP TABLE IF EXISTS `t_user0`; CREATE TABLE `t_user0` ( `id` bigint(20) NOT NULL, `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名称', city_id` ` int ( 12 ) NULL the DEFAULT NULL the COMMENT ' city ' , sex` ` tinyint ( . 1 ) NULL the DEFAULT NULL the COMMENT ' Sex ' , `phone` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '电话', `email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱', `create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间', `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8;
2, Springboot add Maven project dependent, Mybatisplus, Druid, Sharding-jdbc-dependent
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> <!-- sharding-sphere --> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-namespace</artifactId> <version>3.1.0</version> </dependency>
3, was added sharding-jdbc sub-sub-table in the database configuration application.xml
server: port: 8800 spring: application: name: service-hi main: allow-bean-definition-overriding: true # allowed to cover registration eureka: instance: prefer-ip-address: true # turn on the display IP address instance-id: $ {spring.cloud.client.ip-address}: $ {server.port} #eureka page displays the IP address: port number client: ServiceUrl: defaultZone: http://localhost:8761/eureka/ sharding: jdbc: datasource: names: ds0,ds1 ds0: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/land1?characterEncoding=utf-8&useUnicode=true&useSSL=true username: root password: root ds1: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/land2?characterEncoding=utf-8&useUnicode=true&useSSL=true username: root password: root config: sharding: default-data-source-name: ds0 # fragmentation is not configured by default rule table data source localization default-database-strategy: # default database partitioning strategy, the same strategy sub-libraries inline: sharding-column: city_id algorithm-expression: ds${city_id % 2} default-table-strategy: # default table partitioning strategy, with the sub-table strategy inline: shardingColumn: sex algorithm-expression: t_user${sex % 2} props: sql.show: true # sql statement is displayed tables: t_user: #t_user表 key-generator-column-name: id #主键 actual-data-nodes: ds $ {0..1} .t_user $ {0..1} # data node database-strategy: # points Library Strategy inline: sharding-column: city_id algorithm-expression: ds${city_id % 2} table-strategy: # sub-table strategy inline: shardingColumn: sex algorithm-expression: t_user${sex % 2} mybatis-plus: type-aliases-package: com.landcode.service.hi.model mapper-locations: classpath:mapper/*.xml
4, note that in mapper.xml sql configuration file, use the table name user, rather than user0, user1. sharding-jdbc sql performs automatic configuration according to the rules in the corresponding table
<select id="selectUserList" resultType="com.landcode.service.hi.model.User"> select <include refid="Base_Column_List" /> from t_user </select>