一、前言
小編最近一直在研究關于分庫分表的東西,前幾天Docker安裝了mycat實現(xiàn)了分庫分表,但是都在說mycat的bug很多。很多人還是傾向于shardingsphere,其實他是一個全家桶,有JDBC、Proxy 和 Sidecar組成,小編今天以最簡單的JDBC來簡單整合一下!
現(xiàn)在最新版已經(jīng)是5.1.1,經(jīng)過一天的研究用于解決了所有問題,完成了單庫分表!!
二、踩過的坑1. 數(shù)據(jù)源問題
不要使用druid-spring-boot-starter這個依賴,啟動會有問題
com.alibabadruid-spring-boot-starter1.1.21
報錯信息:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMApper' defined in file[D:jiawayundemotargetclassescomexampledemomapperUserMapper.class]:Invocation of init method failed; nested exception isJAVA.lang.IllegalArgumentException: Property 'sqlSessionFactory'or 'sqlSessionTemplate' are required
==解決方案:==
使用單獨的druid
com.alibabadruid1.2.8
建議使用==默認的數(shù)據(jù)源==,sharding-jdbc也是使用的默認的數(shù)據(jù)源,小編使用的自帶的,忘記druid后面會不會有問題了!!
type: com.zaxxer.hikari.HikariDataSource
2. Insert 語句不支持分表路由到多個數(shù)據(jù)節(jié)點
報錯信息:
Insert statement does not support sharding table routing to multiple data nodes.
解決方案:
解決不支持分表路由問題:https://blog.csdn.NET/qq_52423918/article/details/125004312
三、導入maven依賴org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-enginejunitjunittestorg.Apache.shardingsphereshardingsphere-jdbc-core-spring-boot-starter5.1.1org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.projectlomboklombok1.18.10org.springframework.bootspring-boot-starter-jdbcMySQLmysql-connector-javacom.baomidouMyBatis-plus-boot-starter3.5.1
四、新建表1. 新建二張表
命名為:user_0、user_1
CREATE TABLE `user_0` (`cid` bigint(25) NOT NULL,`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`gender` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`data` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`cid`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;SET FOREIGN_KEY_CHECKS = 1;
2. 數(shù)據(jù)庫結構
五、框架全局展示1. User實體類@Datapublic class User implements Serializable {private static final long serialVersionUID = 337361630075002456L;private Long cid;private String name;private String gender;private String data;
2. controller@RestController@RequestMapping("/test")public class UserController {@Autowiredprivate UserMapper userMapper;@GetMapping("/insertTest")public void insertTest(){for (int i = 1 ; i < 10; i++) {User test = new User("王"+i,"男","數(shù)據(jù)" + i);userMapper.insert(test);
3. mapper
我們直接省略了service,簡單一下哈!!
public interface UserMapper extends BaseMapper {
4. application.yml配置server:port: 8089spring:shardingsphere:mode:type: memory# 是否開啟datasource:# 數(shù)據(jù)源(邏輯名字)names: m1# 配置數(shù)據(jù)源m1:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useSSL=false&autoReconnect=true&characterEncoding=UTF-8&serverTimezone=UTCusername: rootpassword: root# 分片的配置rules:sharding:# 表的分片策略tables:# 邏輯表的名稱user:# 數(shù)據(jù)節(jié)點配置,采用Groovy表達式actual-data-nodes: m1.user_$->{0..1}# 配置策略table-strategy:# 用于單分片鍵的標準分片場景standard:sharding-column: cid# 分片算法名字sharding-algorithm-name: user_inlinekey-generate-strategy: # 主鍵生成策略column: cid # 主鍵列key-generator-name: snowflake # 策略算法名稱(推薦使用雪花算法)key-generators:snowflake:type: SNOWFLAKEsharding-algorithms:user_inline:type: inlineprops:algorithm-expression: user_$->{cid % 2}props:# 日志顯示具體的sqlsql-show: truelogging:level:com.wang.test.demo: DEBUGmybatis-plus:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.example.demo.entityconfiguration:#在映射實體或者屬性時,將數(shù)據(jù)庫中表名和字段名中的下劃線去掉,按照駝峰命名法映射 address_book ---> addressBookmap-underscore-to-camel-case: true
5. 啟動類@MapperScan("com.example.demo.mapper")@SpringBootApplicationpublic class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);
六、測試插入九條數(shù)據(jù)
==本次測試策略是:行表達式分片策略:inline==
1. 插入數(shù)據(jù)
輸入 :localhost:8089/test/insertTest
==分片成功==
2. 單個查詢@GetMapping("/selectOneTest")public void selectOneTest(){User user = userMapper.selectOne(Wrappers.lambdaQuery().eq(User::getCid,736989417020850176L));System.out.println(user);
這時他會根據(jù)cid去自動獲取去那個表中獲取數(shù)據(jù)
3. 全查詢@GetMapping("/selectListTest")public void selectListTest(){List list = userMapper.selectList(null);System.out.println(list);
由于沒有條件,他會去把兩個表UNION ALL進行匯總
4. 分頁查詢
需要先配置mybatis-plus分頁配置類:
@Configurationpublic class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;@GetMapping("/selectListPage")public void selectListPage(){IPage page = new Page(1,6);IPage userIPage = userMapper.selectPage(page,null);List records = userIPage.getRecords();System.out.println(records);
我們user_0有5條數(shù)據(jù),user_1有4條數(shù)據(jù)
==我們發(fā)現(xiàn)它會向所有的表中去進行一遍分頁查詢,第一個表數(shù)據(jù)不夠就會加上另一個表分頁拿到的值==
==分頁size為3時,一個user_0就可以滿足分頁條件,就會忽略user_1的分頁數(shù)據(jù)。==
5. 非分片屬性查詢
我們先把user_0表性別修改兩個為女,然后進行查詢!看看沒有分片的字段是否能夠只去user_0去查詢
@GetMapping("/selectListByGender")public void selectListByGender(){List list = userMapper.selectList(Wrappers.lambdaQuery().eq(User::getGender, "女"));System.out.println(list);
有圖可見:不是分片的字段查詢,回去全連接表去查詢一遍,效率和不分表一樣了哈!!
6. 分片屬性來自一個表in查詢@GetMapping("/selectInList")public void selectList(){List users = userMapper.selectList(Wrappers.lambdaQuery().in(User::getCid,736989417020850176L,736989418119757824L));System.out.println(users);
我們可以發(fā)現(xiàn),我們根據(jù)分片字段進行in查詢,sharding-jdbc會識別出來來自于那個表進而提高效率,不會所有的表進行全連接。
七、總結
這樣就完成了最新版的sharding-jdbc的簡單測試和一些坑的解決,總的來說配置很費勁,不能有一定的錯誤!