Don’t be deterred
I’m afraid the title is too high, scared to see, in fact, very simple, or springboot familiar style, rely on, add configuration can be used!! Really, kid sou have no deceit, follow write, do not come out, you come looking for small ning!!
Sharding – JDBC introduction
Has now changed its name to: sharding – sphere, the official web site as https://shardingsphere.apache.org/document/current/cn/overview/ in simple terms, the main can do the following things: 1. In today’s DEMO, we use shard-sphere to do database read and write separation mainly need the following steps :1. Preparation of master-slave database, refer to the article: https://www.javastudy.cloud/articles/2019/11/14/1573693221155.html2. In the Springboot project, the corresponding mybatis and Shard-SPere dependencies are introduced 3. Writing test classes
Springboot + Sharding – JDBC +HikariCP+ Mybatis do read and write separation
Add the dependent
implementation 'org. Mybatis. Spring. The boot: mybatis - spring - the boot - starter: 2.1.1'runtimeOnly 'mysql:mysql-connector-java'// compile group is a dependency of shardingsphere:'org.apache.shardingsphere', name: 'sharding-jdbc-spring-boot-starter', version: '4.0.0 -rc3' Copy the code
Example Add the SpringBoot configuration
# we have a master here from spring. Shardingsphere. The datasource. Names = master, slave0 # Main library configuration spring. Shardingsphere. The datasource. Master. Type = com. Zaxxer. Hikari. HikariDataSourcespring. Shardingsphere. The datasource. Mas ter.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.master.jdbcUrl=jdbc:mysql://localhost:333 09/toolsspring.shardingsphere.datasource.master.username=rootspring.shardingsphere.datasource.master.password=javastudy# From the library configuration spring. Shardingsphere. The datasource. Slave0. Type = com. Zaxxer. Hikari. HikariDataSourcespring. Shardingsphere. The datasource. Sl ave0.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.slave0.jdbcUrl=jdbc:mysql://localhost:33 308/toolsspring.shardingsphere.datasource.slave0.username=rootspring.shardingsphere.datasource.slave0.password=javastudy # Sharding - some of the configuration spring JDBC itself. Shardingsphere. Masterslave. Name = msspring. Shardingsphere. Masterslave. Master - data - the source - name = m asterspring.shardingsphere.masterslave.slave-data-source-names=slave0spring.shardingsphere.props.sql.show=trueCopy the code
Mybatis/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource/HikariDataSource But no longer need a spring. The datasource. Url, spring. The datasource. The username these configurations
Writing test classes
Mybatis mapper is still written in the original way, and then we use Autowire for injection
@Autowired private ArticleMapper articleMapper; @Test public void testDataSource(){ List<ArticleDO> articleDOS = articleMapper.listArticles(new ArticleQC()); System.out.println(articleDOS); }Copy the code
Run the unit test and you can see from the log that hikariCP+ Sharding-JDBC was used
The DEMO freshair
Read and write separation is the number of library level up after the preferred optimization scheme, in terms of code use level, not as difficult as we think, it is recommended to practice in the project! If you have any problems in practice, please feel free to communicate with us
Soul asking
There is a question I have thought for a long time, but has not gone to verify, and share with you to think about, is the database connection pool is maintained by hikariCP or mybatis maintenance, how to verify? If I call a method of a Mapper file twice, is it the same Connection or two connections? If the connection is the same, is the statement the same or separate one?