1.架构

通过分库分表的高可用章节搭建的MySQL架构,在此基础上,整合SpringBoot,实现分库分表和读写分离。

11.SpringBoot整合DBLE分库分表和读写分离_spring

2.整合SpringBoot
(1).引入依赖

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>

(2).添加配置
在application.properties文件中添加如下配置信息。

############################################################
#
# MySQL配置
#
############################################################
### DBLE数据端连接信息,testdb为虚拟数据库
spring.datasource.url = jdbc:mysql://121.4.211.73:8066/testdb
### 用户名
spring.datasource.username = root
### 密码
spring.datasource.password = 123456
### 驱动
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

############################################################
#
# MyBatis配置
#
############################################################
### po类存放目录
mybatis.type-aliases-package = com.example.springboot.dao.po
### mapper(.xml)资源文件存放路径
mybatis.mapper-locations = classpath:mybatis/mapper/*.xml

(3).开发服务端通用返回对象
在项目目录“/src/main/java/com/example/springboot”下新建common目录,并在common目录下新建ResponseCode枚举类和ServerResponse类,具体代码如下。

public enum ResponseCode {
//1.定义枚举值
ERROR(0,"ERROR"),

SUCCESS(1,"SUCCESS");

//2.定义枚举属性
private final int code;

private final String desc;

//3.定义构造函数
ResponseCode(int code, String desc){
this.code = code;
this.desc = desc;
}

//4.定义get方法
public int getCode(){
return code;
}

public String getDesc(){
return desc;
}
}
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保证序列化json的时候,如果是null的对象,key也会消失
public class ServerResponse<T> implements Serializable {
//1.定义属性
private int status;

private String message;

private T data;

//2.定义构造函数
private ServerResponse(int status) {
this.status = status;
}

private ServerResponse(int status, T data) {
this.status = status;
this.data = data;
}

private ServerResponse(int status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}

private ServerResponse(int status, String message) {
this.status = status;
this.message = message;
}

//3.getter方法
public int getStatus() {
return status;
}

public T getData() {
return data;
}

public String getMessage() {
return message;
}

@JsonIgnore
//使之不在json序列化结果当中
//4.判断这个响应是不是一个正确的响应
public boolean isSuccess() {
return this.status == ResponseCode.SUCCESS.getCode();
}

//5.定义返回对象的方法
public static <T> ServerResponse<T> createBySuccess() {
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
}

public static <T> ServerResponse<T> createBySuccessMessage(String message) {
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), message);
}

public static <T> ServerResponse<T> createBySuccess(T data) {
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), data);
}

public static <T> ServerResponse<T> createBySuccess(String message, T data) {
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), message, data);
}

public static <T> ServerResponse<T> createByError() {
return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
}

public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) {
return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
}

public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) {
return new ServerResponse<T>(errorCode, errorMessage);
}

public static <T> ServerResponse<T> createByErrorCodeMessageData(int errorCode, String errorMessage,T data) {
return new ServerResponse<T>(errorCode, errorMessage,data);
}
}

(4).dao层开发
首先在项目目录“/src/main/java/com/example/springboot”下新建“/dao/po”目录,并在po目录下新建Test类,具体代码如下。

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Test {
private Integer id;

private String content;
}

然后在dao目录下新建mapper目录,并在mapper目录下新建TestMapper接口,具体代码如下。

@Repository
public interface TestMapper {
Test testSelect(Integer id);

Integer testInsert(Test test);
}

最后在resource目录下新建“mybatis/mapper”目录,并在mapper目录下新建TestMapper.xml文件,具体代码如下。

<?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.example.springboot.dao.mapper.TestMapper">
<resultMap id="BaseResultMap" type="com.example.springboot.dao.po.Test">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
<arg column="content" javaType="java.lang.String" jdbcType="VARCHAR"/>
</constructor>
</resultMap>
<select id="testSelect" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select id, content
from tb_range_sharding
where id = #{id,jdbcType=INTEGER}
</select>

<insert id="testInsert" parameterType="com.example.springboot.dao.po.Test">
insert into tb_range_sharding
values(#{id},#{content})
</insert>
</mapper>

(5).service层开发
在项目目录“/src/main/java/com/example/springboot”下新建service目录,并在service目录下新建ITest接口,具体代码如下。

public interface ITest {
ServerResponse testSelect(Integer id);

ServerResponse testInsert(Test test);
}

然后在service目录下新建impl目录,并在impl目录下新建TestImpl实现类,具体代码如下。

@Service
public class TestImpl implements ITest {
@Autowired
TestMapper testMapper;

@Override
public ServerResponse testSelect(Integer id) {
Test test = testMapper.testSelect(id);
if (test == null) {
return ServerResponse.createByErrorMessage("没有查询到数据");
}
return ServerResponse.createBySuccess(test);
}

@Override
public ServerResponse testInsert(Test test) {
Integer rs = testMapper.testInsert(test);
if (rs != 1) {
return ServerResponse.createByErrorMessage("插入失败");
}
return ServerResponse.createBySuccess();
}
}

(6).controller层开发
在项目目录“/src/main/java/com/example/springboot”下新建controller目录,并在controller目录下新建TestController类,具体代码如下。

@Controller
@RequestMapping("/test")
public class TestController {
@Autowired
TestImpl testImpl;

@RequestMapping(value = "/testSelect", method = RequestMethod.POST)
@ResponseBody
public ServerResponse testSelect(@RequestBody Test test) {
return testImpl.testSelect(test.getId());
}

@RequestMapping(value = "/testInsert", method = RequestMethod.POST)
@ResponseBody
public ServerResponse testInsert(@RequestBody Test test) {
return testImpl.testInsert(test);
}
}

(7).启动项目
在启动类MyBatisApplication上添加注解“@MapperScan(basePackages = “com.example.springboot.dao.mapper”)”,然后启动项目。

@MapperScan(basePackages = "com.example.springboot.dao.mapper")
@SpringBootApplication
public class SpringBootDemoApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}

}

(8).工程目录结构

11.SpringBoot整合DBLE分库分表和读写分离_spring_02

3.测试

(1).查询id为1的数据

id为1的数据存储在集群dbGroup1的主节点MySQLA(121.4.211.73)和从节点MySQLA’(121.4.191.137)的数据库db_1上,因为配置了读操作全部打到从节点上,所以在121.4.191.137服务器上可以看到查询语句。

11.SpringBoot整合DBLE分库分表和读写分离_spring_03


11.SpringBoot整合DBLE分库分表和读写分离_Test_04

(2).查询id为5000001的数据

id为5000001的数据存储在集群dbGroup2的主节点MySQLB(121.4.191.124)的数据库db_2上,虽然配置了读操作全部打到从节点上,但是MySQLB没有从节点,所以只能在121.4.191.124服务器上看到查询语句。

11.SpringBoot整合DBLE分库分表和读写分离_java_05


11.SpringBoot整合DBLE分库分表和读写分离_java_06

(3).插入id为3的数据

id为3的数据会存储在集群dbGroup1的主节点MySQLA(121.4.211.73)的数据库db_1上,所以在121.4.211.73服务器上可以看到插入语句。

因为MySQLA有从节点MySQLA’(121.4.191.137),通过binlog的主从复制,所以在121.4.191.137上也可以查询到id为3的数据。

11.SpringBoot整合DBLE分库分表和读写分离_spring_07


11.SpringBoot整合DBLE分库分表和读写分离_mybatis_08


11.SpringBoot整合DBLE分库分表和读写分离_mybatis_09

(4).插入id为5000003的数据

id为5000003的数据会存储在集群dbGroup2的主节点MySQLB(121.4.191.124)的数据库db_2上,所以在121.4.191.124服务器上可以看到插入语句和数据。

11.SpringBoot整合DBLE分库分表和读写分离_mybatis_10


11.SpringBoot整合DBLE分库分表和读写分离_mybatis_11


11.SpringBoot整合DBLE分库分表和读写分离_mybatis_12