本文最后更新于:February 5, 2023 pm
积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里,不积小流无以成江海。齐骥一跃,不能十步,驽马十驾,功不在舍。面对悬崖峭壁,一百年也看不出一条裂缝来,但用斧凿,能进一寸进一寸,能进一尺进一尺,不断积累,飞跃必来,突破随之。
目录
分库
依赖:
| <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId> <version>5.1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.2</version> </dependency>
|
yaml配置:
这里的type根据个人情况可以选择Druid,但我自己测试了多种使用Druid的方式均不能启动成功,所以,这里就使用默认的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| spring: application: name: start shardingsphere: datasource: names: db0,db1 db0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: jdbc:mysql://127.0.0.1:3306/jwgl?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8 db1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: jdbc:mysql://127.0.0.2:3306/jwglaly?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl server: port: 8080
|
📢:url的具体内容根据自己的情况进行修改!两个不同数据库即可。
启动项目成功后,可以看见以下内容:
| 2023-02-05 17:07:21.105 INFO 2455 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1547 ms 2023-02-05 17:07:26.402 INFO 2455 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2023-02-05 17:07:26.513 INFO 2455 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2023-02-05 17:07:26.521 INFO 2455 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Starting... 2023-02-05 17:07:26.846 INFO 2455 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Start completed.
|
实体类
根据数据库字段增加实体类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.tothefor.start.pojo;
import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data;
@Data @Builder @AllArgsConstructor public class XXB { private Integer jsh; private String jsxm; private String bmh; }
|
Mapper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.tothefor.start.mapper;
import com.tothefor.start.pojo.Xxb; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;
@Mapper public interface XxbMapper { @Select("select * from jsjbxxb where jsh=#{jsh}") Xxb get(String jsh);
@Insert("insert into jsjbxxb(jsh,jsxm,bmh) values (#{jsh},#{jsxm},#{bmh})") int add(Xxb xxb); }
|
增加分库策略配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| spring: application: name: start shardingsphere: datasource: names: db0,db1 db0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: jdbc:mysql://127.0.0.1:3306/jwgl?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8 db1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: jdbc:mysql://127.0.0.2:3306/jwglaly?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
rules: sharding: tables: jsjbxxb: actual-data-nodes: db0.jsjbxxb,db1.jsjbxxb database-strategy: standard: sharding-colum: jsh sharding-algorithm-name: my-alg sharding-algorithm: my-alg: type: MOD props: sharding-count: 2
props: sql-show: true
mybatis-plus: mapper-locations: classpath:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl server: port: 8080
|