In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use springboot+mybatis+sharding-jdbc to separate reading and writing". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use springboot+mybatis+sharding-jdbc to separate reading and writing.
Introduction to sharding-jdbc
It has been renamed to sharding-sphere, and the official website is as follows
Https://shardingsphere.apache.org/document/current/cn/overview/
To put it simply, the most important things can be done:
1. Database read-write separation
two。 Database sub-table
3. Distributed transaction
In today's DEMO, let's use shard-sphere to separate read and write from the database.
The main steps are as follows:
1. Prepare the master-slave database
Reference article:
Https://www.javastudy.cloud/articles/2019/11/14/1573693221155.html
two。 In the springboot project, introduce the corresponding mybatis and shard-spere dependencies
3. Write test classes
Springboot+sharding-jdbc+HikariCP+mybatis does read-write separation and adds dependency implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'
RuntimeOnly 'mysql:mysql-connector-java'
/ / there is an extra shardingsphere dependency here.
Compile group: 'org.apache.shardingsphere', name:' sharding-jdbc-spring-boot-starter', version: '4.0.0 Murray RC3'
Add the configuration of springboot # here we have one master and one slave
Spring.shardingsphere.datasource.names=master,slave0
# configuration of the main library
Spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
Spring.shardingsphere.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
Spring.shardingsphere.datasource.master.jdbcUrl=jdbc:mysql://localhost:33309/tools
Spring.shardingsphere.datasource.master.username=root
Spring.shardingsphere.datasource.master.password=javastudy
# configuration of slave library
Spring.shardingsphere.datasource.slave0.type=com.zaxxer.hikari.HikariDataSource
Spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.cj.jdbc.Driver
Spring.shardingsphere.datasource.slave0.jdbcUrl=jdbc:mysql://localhost:33308/tools
Spring.shardingsphere.datasource.slave0.username=root
Spring.shardingsphere.datasource.slave0.password=javastudy
# some configurations of sharding-jdbc itself
Spring.shardingsphere.masterslave.name=ms
Spring.shardingsphere.masterslave.master-data-source-name=master
Spring.shardingsphere.masterslave.slave-data-source-names=slave0
Spring.shardingsphere.props.sql.show=true
Note here that the first line of the configuration of the master and slave libraries, datasource.master.type, is to write HikariDataSource so that you can use HikariCP.
Then the usual configuration of hikariCP and Mybatis will be fine, but the configuration of spring.datasource.url,spring.datasource.username is no longer required.
Write test classes
The mapper of mybatis is still written in the original way, and then we use autowire for injection
@ Autowired
Private ArticleMapper articleMapper
@ Test
Public void testDataSource () {
List articleDOS = articleMapper.listArticles (new ArticleQC ())
System.out.println (articleDOS)
}
Run the unit test, and you can see from the log that hikariCP+sharding-jdbc is used
At this point, I believe you have a deeper understanding of "how to use springboot+mybatis+sharding-jdbc to do read-write separation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.