1、POM文件
<properties>
<java.version>1.8</java.version>
<mysql.version>8.0.11</mysql.version>
<mybatis-plus.version>3.0.7</mybatis-plus.version>
<sharding-jdbc.version>4.1.1</sharding-jdbc.version>
</properties>
<dependencies>
<!-- 解决 swagger 依赖的 guava 包冲突的错误问题 -->
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-core -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>${sharding-jdbc.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-spring-boot-starter -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-jdbc.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-spring-namespace -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>${sharding-jdbc.version}</version>
</dependency>
</dependencies>
2、配置 application.properties
# web port
server.port=8080
server.http2.enabled=true
spring.main.lazy-initialization=false
# undertow configuration
server.undertow.threads.io=16
server.undertow.threads.worker=256
server.undertow.buffer-size=1024
server.undertow.direct-buffers=true
kailo.swagger.title=xxx
kailo.swagger.description=xxxxx
kailo.swagger.version=1.0.0
# mybatis-plus configuration
mybatis-plus.global-config.banner=false
mybatis-plus.global-config.db-config.id-type=auto
mybatis-plus.global-config.db-config.field-strategy=not_empty
mybatis-plus.global-config.db-config.table-underline=true
mybatis-plus.global-config.db-config.db-type=h2
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
# 读写分离
spring.shardingsphere.datasource.names=master,slave0
# Hikari 用 jdbc-url
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://xxxxxxxx:3308/shardingjdbc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
spring.shardingsphere.datasource.master.username=base_sa
spring.shardingsphere.datasource.master.password=base_sa
spring.shardingsphere.datasource.slave0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.slave0.jdbc-url=jdbc:mysql://xxxxxxxx:3309/shardingjdbc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=GMT%2B8
spring.shardingsphere.datasource.slave0.username=base_sa
spring.shardingsphere.datasource.slave0.password=base_sa
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
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
3、编写测试代码
@ApiModel(value = "用户实体")
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_base_user")
public class User {
@ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.NONE)
private Long id;
@ApiModelProperty(value = "姓名")
private String userName;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
<?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.test.shardingshperejdbc.mapper.UserMapper">
</mapper>
public interface UserService extends IService<User> {
}
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
@Api(value = "用户管理")
@Log4j2
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {
private UserService userService;
@ApiOperation(value = "保存或更新用户", notes = "userName是必输入项")
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "id", value = "用户主键", dataTypeClass = Long.class, required = false),
@ApiImplicitParam(name = "userName", value = "用户姓名", dataTypeClass = String.class, required = true)
})
@PostMapping("/saveOrUpdate")
public ResponseEntity<Result<User>> saveOrUpdate(
@RequestParam(required = false) Long id,
@RequestParam String userName
) {
User user = new User(id, userName, new Date());
userService.saveOrUpdate(user);
return ResultUtils.ok(user);
}
@ApiOperation(value = "查询所有用户", notes = "")
@GetMapping("/queryAll")
public ResponseEntity<Result<List<User>>> queryAll() {
List<User> userList = userService.list();
return ResultUtils.ok(userList, "查询成功");
}
}
4、启动类
@SpringBootApplication
public class ShardingShpereJdbcApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingShpereJdbcApplication.class, args);
}
}