lpfvip00 2020-07-19
1、搭建环境
(1)SpringBoot2.2.1+MyBatisPlus+Sharding-JDBC+Druid连接池
(2)创建SpringBoot工程,版本2,2,1
(3)引入相关依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.0.0-RC1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2、按照水平分表的方式创建数据库,创建数据库表
1)创建两个数据库
2)创建数据库和表
3、编写代码,完成对分库分表后数据的操作
(1)创建实体类,mapper
@Data public class Course { private Long cid; private String cname; private Long userId; private String cstatus; } @Repository public interface CourseMapper extends BaseMapper<Course> { }
4、配置sharding-jdbc的分片策略
(1)在项目的配置文件中进行配置
#sharding-jdbc 分片策略 # 数据源名称,多数据源以逗号分隔 # 水平分库,所以要配置多数据源 spring.shardingsphere.datasource.names=m1,m2 #一个实体类对应两张表 spring.main.allow-bean-definition-overriding=true # 第一个数据源 # 数据库连接池 spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource #数据库驱动类名 spring.shardingsphere.datasource.m1.driver-class-name= com.mysql.cj.jdbc.Driver # 数据库 URL 连接 spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=GMT%2B8 # 数据库用户名 spring.shardingsphere.datasource.m1.username= root # 数据库密码 spring.shardingsphere.datasource.m1.password=houchen # 第二个数据源 # 数据库连接池 spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource #数据库驱动类名 spring.shardingsphere.datasource.m2.driver-class-name= com.mysql.cj.jdbc.Driver # 数据库 URL 连接 spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8 # 数据库用户名 spring.shardingsphere.datasource.m2.username= root # 数据库密码 spring.shardingsphere.datasource.m2.password=houchen # 指定数据库的分布情况 数据库里面表的分布情况 spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{1..2}.course_$->{1..2} #指定course表中主键的生成策略 SNOWFLAKE:雪花算法 spring.shardingsphere.sharding.tables.course.key-generator.column = cid spring.shardingsphere.sharding.tables.course.key-generator.type =SNOWFLAKE # 指定数据表分片的策略 约定cid值 : 偶数--》course_1 奇数-->course_2 spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column= cid spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid%2 +1} # 指定数据库分片的策略 约定:user_id是偶数,则将数据添加到m1的数据库中 user_id是奇数,则将数据添加到m2的数据库中 #spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column= user_id #spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=m$->{user_id%2+1} spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_id spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{user_id%2+1} #打开sql的输出日志 spring.shardingsphere.props.sql.show = true
5、编写测试代码
@RunWith(SpringRunner.class) @SpringBootTest public class ShardingJdbcDdemoApplicationTest { @Autowired private CourseMapper courseMapper; // 测试水平分库 @Test public void testEduCourse(){ Course course =new Course(); course.setCname("java"); course.setUserId(100L); //偶数,按照前面的配置,应该插入到m1库 course.setCstatus("normal"); courseMapper.insert(course); } }
user_id为偶数,插入到m1库,cid为奇数,插入到course_2表