mycat系列三SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

Posted 阿甘正专

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mycat系列三SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合相关的知识,希望对你有一定的参考价值。

点击关注“阿甘正专”,设为星标

奇迹每天都在发生,等你而来

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

目录

一、前言

二、整合 SpringBoot + MybatisPlus + Mycat

1、新建一个SpringBoot项目

2、pom依赖

3、application.properties 配置

4、生成实体类

5、测试


一、前言

通过前两篇博客,我们可以通过docker搭建mysql多主多从(主从复制)服务,并使用Mycat中间件实现分片、读写分离等:

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

下面,我们进入实际的项目开发演示,如何整合 SpringBoot + MybatisPlus + Mycat 的demo 示例。

二、整合 SpringBoot + MybatisPlus + Mycat

1、新建一个SpringBoot项目

  • 名称:springboot-mycat

  • 版本:2.3.3.RELEASE

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

2、pom依赖

主要依赖如下:lombok、mybatis-plus、pagehelper(分页插件)、Jdbc连接驱动等

<dependency>

  1. <groupId>org.springframework.boot</groupId>

  2. <artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

  1. <groupId>org.projectlombok</groupId>

  2. <artifactId>lombok</artifactId>

  3. <optional>true</optional>

</dependency>

<dependency>

  1. <groupId>mysql</groupId>

  2. <artifactId>mysql-connector-java</artifactId>

  3. <version>5.1.38</version>

</dependency>

<dependency>

  1. <groupId>com.baomidou</groupId>

  2. <artifactId>mybatis-plus-boot-starter</artifactId>

  3. <version>3.1.1</version>

</dependency>

<dependency>

  1. <groupId>com.baomidou</groupId>

  2. <artifactId>mybatis-plus-generator</artifactId>

  3. <version>3.1.2</version>

</dependency>

<dependency>

  1. <groupId>org.freemarker</groupId>

  2. <artifactId>freemarker</artifactId>

  3. <version>2.3.28</version>

</dependency>

<!-- 数据库连接池 -->

<dependency>

  1. <groupId>com.alibaba</groupId>

  2. <artifactId>druid</artifactId>

  3. <version>1.1.22</version>

</dependency>

<!-- 分页-->

<dependency>

  1. <groupId>com.github.pagehelper</groupId>

  2. <artifactId>pagehelper-spring-boot-starter</artifactId>

  3. <version>1.2.5</version>

  4. <exclusions>

  5.     <exclusion>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis</artifactId>

        </exclusion>

  6. </exclusions>

</dependency>

3、application.properties 配置

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

【说明】

  • spring.datasource.url:需要配置的是mycat的ip与端口,由mycat去拦截我们的sql语句,然后根据mycat配置的分片规则去操作具体的mysql物理数据库。

4、生成实体类

我们可以使用 mybatis-plus-generator 包,自动生成实体类等,这里就不演示了。

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

5、测试

5.1 新增测试

这里我们通过上一节《Docker安装mycat并实现MySQL主从复制、读写分离》,在test_db库中的t_test表进行新增测试


  
    
    
  
  1. @RestController

  2. @RequestMapping("/test")

  3. public class TestController {


  4. @Resource

  5. private TestDao testDao;

  6. @Autowired

  7. private ITestService testService;


  8. /**

  9. * 新增

  10. * @return

  11. */

  12. @RequestMapping("/save")

  13. public int save() {

    1. Test entity = new Test();

    2. entity.setTitle("马马哈哈");

    3. int num = testDao.insert(entity);

    4. return num;


    5. }

  14. }

运行springboot的启动类,访问 localhost:8080/test/save ,成功后,便往我们的mysql数据库新增了一条记录,通过Navicat连接 mycat 查看:

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

发现新增了一条数据,这里根据mycat配置的rule.xml规则分片,实际是插入到了第二个主库mysql-master2(当然它对应的从库也会同步复制过去)

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

5.2 查询测试

这里,我们查询t_test表的所有记录(未排序)


  
    
    
  
  1. /**

  2. * 所有记录

  3. * @return

  4. */

  5. @RequestMapping("/list")

  6. public List<Test> query() {

    1. List<Test> list = testDao.selectList(null);

    2. return list;

  7. }

postman工具访问结果如下:

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

5.3 分页测试

这里,使用到pagehelper分页插件,它是通过拦截我们的sql,进行分页参数添加

controller


  
    
    
  
  1. /**

  2. * 列表分页查询

  3. * @return

  4. */

  5. @RequestMapping("/listByPage")

  6. public PageInfo listBypage() {

    1. PageInfo pageInfo = testService.listBypage();

    2. return pageInfo;

  7. }

service,按id升序


  
    
    
  
  1. /**

  2. * 列表分页查询

  3. * @return

  4. */

  5. @Override

  6. public PageInfo listBypage() {

  7. //分页,每页10条

  8. PageHelper.startPage(1, 10);

  9. // 按id升序

  10. List<Test> list = testDao.selectList(new QueryWrapper<Test>().orderByAsc("id"));


  11. PageInfo pageInfo = new PageInfo(list);

  12. return pageInfo;

  13. }

测试结果,如下:

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

控制台,打印的sql

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

当然,你可以根据mycat配置不同的分片规则,往不同的mysql数据库操作数据,这里就不过多演示了。

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

查看更多好文,进入公众号--撩我--往期精彩

【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

右上角按钮分享给更多人哦~【mycat系列三】SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合

来都来了,点个在看再走吧~~~



以上是关于mycat系列三SpringBoot + mybatisPlus + Mycat + Mysql (多主多从) 整合的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot集成Mycat时异常:CLIENT_PLUGIN_AUTH is required

SpringBoot整合MyCat实现读写分离,稳进大厂

mycat系列二Docker安装mycat并实现MySQL的读写分离

mycat系列一基于 Docker 搭建 MySQL 主从复制的详细教程

SpringBoot集成Mycat实现读写分离高可用

springboot集成mybatis和mycat