springboot integrates mybatis and mycat
In fact, the configuration of springboot integrating mycat and mysql through mybatis is basically similar. The biggest pitfall is the version dependency of mybatis.
The code is directly above. This module is a submodule under springcloud. The module contains configuration information about eureka registry and other configuration information. If it is just a pure springboot project, you can ignore eureka related dependencies and configuration information, and only focus on the configuration of mybatis and mycat. can.
Preparation conditions: 1. The
java environment is set up.
2. Mycat installation and configuration is complete:
please refer to: https://blog.csdn.net/qq_37488998/article/details/110679222
The configuration of the schema table is as follows:
rule.xml configuration information
server.xml configuration information:
3. Use mycat to add some data, take the tenant table as an example:
1. Create the project module, the directory structure is as follows
2. Pom dependency
Note: mysql driver, mycat cannot use too high version dependency, only the 5.* version.
The blogger started to use 8. *, and kept reporting errors when inquiring about something missing.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ideal-mycat-1016</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- mysql驱动,mycat使用的不能用高的依赖,只能用5.*,还不能高的小版本-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!--可以用maven直接将代码打成jar包-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.yml configuration file
server:
port: 1016
spring:
main:
allow-bean-definition-overriding: true
application:
name: IDEAL-MYCAT
# Mycat配置信息
datasource:
url: jdbc:mysql://localhost:8066/TESTDB?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
# mybatis是依赖5.*的版本,所以相对于8.*的少个cj;
driver-class-name: com.mysql.jdbc.Driver
# MyBatis
mybatis:
type-aliases-package: com.demo.mycat.mapper
mapper-locations: classpath:/mapper/*.xml
configuration:
map-underscore-to-camel-case: true
eureka:
instance:
prefer-ip-address: true # 注册服务的时候使用服务的ip地址
hostname: mycat-service
client:
service-url:
defaultZone: http://localhost:1001/eureka/
# 是否注册自身到eureka服务器
register-with-eureka: true
fetch-registry: true
4. The remaining interfaces are consistent with the mysql method
package com.demo.mycat.controller;
import com.demo.mycat.dto.Result;
import com.demo.mycat.dto.Tenant;
import com.demo.mycat.mapper.TenantMapper;
import lombok.extern.slf4j.Slf4j;
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.RestController;
import java.util.List;
@RestController
@RequestMapping("/mycat")
@Slf4j
public class MycatController {
@Autowired
private TenantMapper tenantMapper;
@GetMapping
Result getTest(){
List<Tenant> tenants = tenantMapper.queryAllTenant();
if(null != tenants && !tenants.isEmpty()){
return Result.ok(tenants);
}
return Result.ok("未查询到数据!");
}
}
package com.demo.mycat.mapper;
import com.demo.mycat.dto.Tenant;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface TenantMapper {
List<Tenant> queryAllTenant();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.mycat.mapper.TenantMapper">
<select id="queryAllTenant" resultType="com.demo.mycat.dto.Tenant">
SELECT id,tenant_id,tenant_name,phone from tenant
</select>
</mapper>
5. Test results At
this point, the integration of mycat is basically completed!