首先,需要在pom.xml文件中添加以下依赖:
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- Sharding JDBC -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>5.0.0-alpha</version>
</dependency>
<!-- MySQL Connector/J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-JAVA</artifactId>
<version>8.0.27</version>
</dependency>
</dependencies>
然后,在Application.properties文件中配置Druid和Sharding JDBC的数据源信息:
# Druid
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Sharding JDBC
spring.shardingsphere.datasource.names=ds0,ds1
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/test_0?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/test_1?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
# Sharding rule
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds${user_id % 2}
上述配置文件中,配置了Druid和Sharding JDBC的数据源信息,以及Sharding规则,规则中根据user_id取模的结果,将数据分配到不同的数据库中。
接下来,创建一个User实体类,用于映射数据库中的用户表:
@Data
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private String password;
private Long userId;
}
然后,创建一个UserMapper接口,使用MyBatis Plus的注解器和Mapper注解,定义一些基本的CRUD操作:
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Select("select * from user where user_id = #{userId}")
User findByUserId(@Param("userId") Long userId);
}
接下来,创建一个UserController类,实现根据userId查询用户信息的功能:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/{userId}")
public User findByUserId(@PathVariable Long userId) {
return userMapper.findByUserId(userId);
}
}
最后,启动Spring Boot应用程序,访问http://localhost:8080/user/1,即可查询userId为1的用户信息。
需要注意的是,在上述示例代码中,我们使用了Druid、MyBatis Plus和Sharding JDBC来实现分表功能。其中,Druid是一个高性能的数据库连接池,MyBatis Plus是一个基于MyBatis的ORM框架,Sharding JDBC是一个分库分表中间件,可以帮助我们快速实现分表功能。当然,在实际开发中,还需要考虑更多的因素,例如数据同步、数据迁移、分布式事务等。