您现在的位置是:首页 >技术交流 >Springboot整合Mycat2网站首页技术交流
Springboot整合Mycat2
简介Springboot整合Mycat2
Springboot连接Mycat2
环境准备:
http://t.csdn.cn/zRO5v
创建Springboot项目
1.新建项目:
配置好对应的选项:
选择好对应的springboot版本,和项目自带的默认依赖:
如果你的环境搭建的时候是按照上面的教程搭建,那么请选择MySQL Drive驱动
配置Maven:
如果下载速度慢,请在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
多次测试查询
- http://localhost:8080/test/selectAll
- 获取一个zhang3,一个li4不同的结果,证明mycat读写分离成功
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。