Springboot整合Mycat2

该文章介绍了如何在Springboot项目中配置Mycat以实现数据库的读写分离。通过添加相关依赖,配置数据源,创建实体类、DAO接口和Controller,实现了数据的插入和查询操作,验证了Mycat的读写分离功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot连接Mycat2

环境准备:

http://t.csdn.cn/zRO5v

创建Springboot项目

在这里插入图片描述

1.新建项目:

配置好对应的选项:
在这里插入图片描述

选择好对应的springboot版本,和项目自带的默认依赖:

如果你的环境搭建的时候是按照上面的教程搭建,那么请选择MySQL Drive驱动
在这里插入图片描述

配置Maven:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GxCupYk1-1684724379377)(C:\Users\ubunt\AppData\Roaming\Typora\typora-user-images\1684720903155.png)]

如果下载速度慢,请在Maven的/conf/settings.xml文件中配置国内镜像源
在这里插入图片描述在这里插入图片描述

等待Mave依赖同步完成。。。。。

在这里插入图片描述

将配置文件改为yml后缀
在这里插入图片描述

  • pom.xml

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.10</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.42</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
    
    
    
  • 修改配置文件.yml

    server:
      port: 8080
    
    spring:
      main:
        allow-bean-definition-overriding: true
      application:
        name: mycat
      # Mycat配置信息
      datasource:
        url: jdbc:mysql://192.168.174.138:8066/TESTDB?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: 123456
        # mybatis是依赖5.*的版本,所以相对于8.*的少个cj;
        driver-class-name: com.mysql.jdbc.Driver
    
    # MyBatis
    mybatis:
      type-aliases-package: com.example.bootmycat.dao
      mapper-locations: classpath:/mapper/*.xml
      configuration:
        map-underscore-to-camel-case: true
    
    
  • 新建一个实体类

    /**
     * @author jigua
     * @version 1.0
     * @className Enterprise
     * @description
     * @create 2022/9/27 16:14
     */
    
    public class Enterprise {
    
        private Long id;
    
        private String phone;
    
        private String name;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    
    
  • 新建一个dao

import com.example.bootmycat.pojo.Enterprise;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author jigua
 * @version 1.0
 * @className TestDao
 * @description
 * @create 2022/9/27 16:13
 */
@Mapper
public interface TestDao {

    @Select("select * from enterprise")
    List<Enterprise> selectAll();

    @Insert("insert into enterprise(phone,name) values(#{phone},#{name})")
    int add(Enterprise enterprise);
}


  • 新建一个controller
    • 示例提供一个查询和一个插入方法
import com.example.bootmycat.dao.TestDao;
import com.example.bootmycat.pojo.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * @author jigua
 * @version 1.0
 * @className TestController
 * @description
 * @create 2022/9/27 16:15
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestDao testDao;

    @GetMapping("/selectAll")
    @ResponseBody
    public List<Enterprise> selectAll() {
        List<Enterprise> enterprises = testDao.selectAll();

        return enterprises;
    }

    @GetMapping("/insert")
    @ResponseBody
    public String insert() {
        Enterprise enterprise = new Enterprise();
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        enterprise.setName(name);
        enterprise.setPhone(name.substring(0, 3));
        int i = testDao.add(enterprise);
        if (i > 0) {
            return "success";
        }
        return "fail";
    }
}


最终文件目录结构图:

在这里插入图片描述

测试插入

  • http://localhost:8080/test/insert

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TCqkzBZJ-1684724379379)(C:\Users\ubunt\AppData\Roaming\Typora\typora-user-images\1684723833644.png)]

多次测试查询

  • http://localhost:8080/test/selectAll
  • 获取一个zhang3,一个li4不同的结果,证明mycat读写分离成功

在这里插入图片描述

在这里插入图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值