As a lightweight Java framework, Sharding JDBC provides additional services in Java’s JDBC layer, which can be understood as an enhanced VERSION of the JDBC driver. Among them, the operation of the database and table is an important part of it. Next, follow me to see how this operation is carried out. Environment preparation Pom.xml org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE <java.version>1.8</java.version> < sharding version > 3.1.0 < / sharding version >. IO shardingsphere sharding – JDBC – the core ${sharding. Version}
<dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${sharding.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>org.mybatis</groupId> < artifactId > mybatis < / artifactId > < version > 3.4.5 < / version > < / dependency > < the dependency > < the groupId > org. Mybatis. Spring. The boot < / groupId > < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 1.3.1 < / version > </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> < version > 5.1.46 < / version > < / dependency > < the dependency > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies>Copy the code
Format sharding.. JDBC datasource. The connection pool. XXX: set the four elements of information
sharding.jdbc.datasource.db0.type=com.alibaba.druid.pool.DruidDataSource sharding.jdbc.datasource.db0.driver-class-name=com.mysql.jdbc.Driver sharding.jdbc.datasource.db0.url=jdbc:mysql://db0Ip:port/sharing sharding.jdbc.datasource.db0.username=xxx sharding.jdbc.datasource.db0.password=xxx
sharding.jdbc.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource sharding.jdbc.datasource.db1.driver-class-name=com.mysql.jdbc.Driver sharding.jdbc.datasource.db1.url=jdbc:mysql://db1Ip:port/sharing sharding.jdbc.datasource.db1.username=xxx sharding.jdbc.datasource.db1.password=xxx
Set branch rules
Sharding. JDBC. Config. Sharding. Default – database – strategy. The inline. Sharding – column: depots
Sharding. JDBC. Config. Sharding. Default – database – strategy. The inline. Algorithm – expression: depots algorithm
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=id sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=db$->{id % 2}
Bound logical table
sharding.jdbc.config.sharding.binding-tables=employee
Set table rules
sharding.jdbc.config.sharding.tables. Actual -data-nodes: indicates the actual table corresponding to a logical table
sharding.jdbc.config.sharding.tables. Table – strategy.inline-sharding -column: indicates a sub-table column
sharding.jdbc.config.sharding.tables. Logical table. The table – strategy. The inline. Algorithm – expression: table algorithm
sharding.jdbc.config.sharding.tables. Key-generator-column-name: indicates the primary key column
sharding.jdbc.config.sharding.tables.employee.actual-data-nodes=db– > {0.. 1} sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.sharding-column=id sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.algorithm-expression=employee_$->{id % 2} sharding.jdbc.config.sharding.tables.employee.key-generator-column-name=id
Print log
sharding.jdbc.config.props.sql.show=true mapper /**
-
The Employee table written here is the logical table configured above
-
The bottom layer will follow the sharding rules, */ @mapperPublic interface EmployeeMapper {@select (” Select * from employee”) List Select All();
@Insert(“insert into employee (name) values (#{name})”) void inser(Employee entity); } test @ RunWith (SpringRunner class) @ SpringBootTest (classes = ShardingApplication. Class) public class ShardingApplicationTests {
@Autowired private EmployeeMapper employeeMapper;
@Test public void save() { for (int i = 0; i < 10; i++) { Employee employee = new Employee(); employee.setName(“xx”+i); employeeMapper.inser(employee); }}
@Test public void list() { employeeMapper.selectAll().forEach(System.out::println); The amount of data in the single table is small after the split. The big data in the single table is split. This solves the problem The previous atomic operations were separated into multiple operations, and transaction processing became complicated. Multiple DB maintenance costs increased
Sub-library sub-table is only a small part of shardingJDBC, there are a lot of content we need to continue to study, if you are interested in this, please follow me, next I will release more relevant tutorials for you to learn, if you encounter problems I will try to help you.