Sharding+SpringBoot+Mybatis 读写分离

时间:2022-07-23 21:48:24

基于Sharding JDBC的读写分离

1.引入pom.xml


<dependencies>
<!-- mybatis -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency> <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.3</version>
</dependency> <dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC2</version>
</dependency> <!-- https://mvnrepository.com/artifact/io.shardingjdbc/sharding-jdbc-core -->
<dependency>
<groupId>io.shardingjdbc</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>2.0.3</version>
</dependency> <dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependencies>

2.建立实体类


public class User {
@Id
private Integer id;
private String name;
private Integer age; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

3.建立mapper

public interface UserMapper extends Mapper<User> {
}

4.建立service


/**
* @description:
* @author: mmc
* @create: 2020-03-09 23:26
**/
@Service
public class UserService { @Autowired
private UserMapper userMapper; public void add(User user){
userMapper.insert(user);
} public List<User> list(){
return userMapper.selectAll();
}
}

5.建立controller

/**
* @description:
* @author: mmc
* @create: 2020-03-09 23:27
**/
@RestController
public class UserController { @Autowired
private UserService userService; @RequestMapping("/addUser")
public void add(User user){
userService.add(user);
} @RequestMapping("/selectUser")
public List<User> selectUser(){
return userService.list();
} @RequestMapping("/addAndList")
public List<User> addAndList(User user){
userService.add(user);
return userService.list();
}
}

6.写配置文件


spring.shardingsphere.datasource.names=master,slave0 spring.shardingsphere.datasource.master.type=org.apache.commons.dbcp.BasicDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=0490218292 spring.shardingsphere.datasource.slave0.type=org.apache.commons.dbcp.BasicDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave0.url=jdbc:mysql://localhost:3306/slave0?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=0490218292 spring.shardingsphere.masterslave.name=ms
spring.shardingsphere.masterslave.master-data-source-name=master
spring.shardingsphere.masterslave.slave-data-source-names=slave0 spring.shardingsphere.props.sql.show=true

7.启动类

@SpringBootApplication
@MapperScan("com.mmc.master.mapper")
public class ShardingApplication { public static void main(String[] args) {
SpringApplication.run(ShardingApplication.class,args);
}
}

8.测试

先在浏览器中测试新增是否是走主库

http://localhost:8080/addUser?name=wangxiao&age=11

在测试查询是否走的从库

http://localhost:8080/selectUser

注:本文描述的主从库并没有配置真正的主从关系,所以数据并没有同步,仅供学习读写分离。