Springboot整合Mycat2

Springboot连接Mycat2

环境准备:

http://t.csdn.cn/zRO5v

创建Springboot项目

Springboot整合Mycat2_第1张图片

1.新建项目:

配置好对应的选项:
Springboot整合Mycat2_第2张图片

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

如果你的环境搭建的时候是按照上面的教程搭建,那么请选择MySQL Drive驱动
Springboot整合Mycat2_第3张图片

配置Maven:

Springboot整合Mycat2_第4张图片

如果下载速度慢,请在Maven的/conf/settings.xml文件中配置国内镜像源
Springboot整合Mycat2_第5张图片Springboot整合Mycat2_第6张图片

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

Springboot整合Mycat2_第7张图片

将配置文件改为yml后缀
Springboot整合Mycat2_第8张图片

  • pom.xml

            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druid-spring-boot-starterartifactId>
                <version>1.1.10version>
            dependency>
    
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.42version>
                <scope>runtimescope>
            dependency>
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>2.1.0version>
            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";
    }
}


最终文件目录结构图:

Springboot整合Mycat2_第9张图片

测试插入

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

Springboot整合Mycat2_第10张图片

多次测试查询

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

在这里插入图片描述

在这里插入图片描述

你可能感兴趣的:(spring,boot,java,spring)