本篇內(nèi)容主要講解“怎么用springboot+mybatis+sharding-jdbc做讀寫分離”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“怎么用springboot+mybatis+sharding-jdbc做讀寫分離”吧!
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供靖邊網(wǎng)站建設(shè)、靖邊做網(wǎng)站、靖邊網(wǎng)站設(shè)計(jì)、靖邊網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、靖邊企業(yè)網(wǎng)站模板建站服務(wù),10余年靖邊做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
現(xiàn)已更名為: sharding-sphere,官方網(wǎng)址如下
https://shardingsphere.apache.org/document/current/cn/overview/
簡(jiǎn)單來(lái)說(shuō),最主要的可以做以下事情:
1.數(shù)據(jù)庫(kù)讀寫分離
2.數(shù)據(jù)庫(kù)分庫(kù)分表
3.分布式事務(wù)
在今天的DEMO中, 我們一起來(lái)用shard-sphere來(lái)做數(shù)據(jù)庫(kù)的讀寫分離
主要需要以下幾步:
1.準(zhǔn)備主從的數(shù)據(jù)庫(kù),
參考文章:
https://www.javastudy.cloud/articles/2019/11/14/1573693221155.html
2.在springboot工程中,引入相應(yīng)的mybatis和shard-spere的依賴
3.編寫測(cè)試類
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'
runtimeOnly 'MySQL:mysql-connector-java'
// 這里多了一個(gè)shardingsphere的依賴
compile group: 'org.apache.shardingsphere', name: 'sharding-jdbc-spring-boot-starter', version: '4.0.0-RC3'
# 這里我們有一主一從
spring.shardingsphere.datasource.names=master,slave0
# 主庫(kù)的配置
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
# 從庫(kù)的配置
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
# sharding-jdbc本身的一些配置
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
這里要注意, 主庫(kù)和從庫(kù)配置的第一行, datasource.master.type 這里, 要寫HikariDataSource, 這樣就可以使用HikariCP了
然后平常hikariCP和Mybatis的配置照常配置就可以了, 但是不在需要spring.datasource.url,spring.datasource.username
這些配置了
mybatis的mapper還是按原來(lái)的寫法寫, 然后我們使用autowire進(jìn)行注入
@Autowired
private ArticleMapper articleMapper;
@Test
public void testDataSource(){
List<ArticleDO> articleDOS = articleMapper.listArticles(new ArticleQC());
System.out.println(articleDOS);
}
運(yùn)行單元測(cè)試,可通過(guò)日志看出使用了hikariCP+sharding-jdbc
到此,相信大家對(duì)“怎么用springboot+mybatis+sharding-jdbc做讀寫分離”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
文章題目:怎么用springboot+mybatis+sharding-jdbc做讀寫分離
文章出自:http://jinyejixie.com/article20/ggidjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、用戶體驗(yàn)、網(wǎng)站制作、網(wǎng)站排名、微信公眾號(hào)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)