springboot怎么集成shardingjdbc 按照步骤,一步一步操作
时间: 2023-11-28 17:05:25 浏览: 116
好的,下面是Spring Boot集成Sharding JDBC的步骤:
1. 引入Sharding JDBC依赖
在`pom.xml`文件中添加Sharding JDBC的依赖:
```xml
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
```
2. 配置数据源
在`application.yml`文件中配置数据源信息:
```yaml
spring:
datasource:
# 主库
master:
url: jdbc:mysql://localhost:3306/db_master?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
# 从库
slave:
url: jdbc:mysql://localhost:3306/db_slave?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
# Sharding JDBC配置
sharding:
# 数据源名称
data-sources:
# 主库
master:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/db_master?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
hikari:
pool-name: master
# 从库
slave:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/db_slave?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
hikari:
pool-name: slave
# 分片规则配置
sharding-rule:
# 分库策略
database-strategy:
inline:
sharding-column: user_id # 分库键
algorithm-expression: master_${user_id % 2} # 数据库名称规则,user_id % 2 代表取模,结果为0或1
# 分表策略
table-strategy:
inline:
sharding-column: order_id # 分表键
algorithm-expression: order_${order_id % 2} # 表名称规则,order_id % 2 代表取模,结果为0或1
# 默认数据源名称
default-data-source: master
# 数据源名称和表名称映射关系
binding-tables:
- t_order
- t_order_item
props:
# 打印SQL语句
sql:
show: true
```
3. 编写Mapper
编写DAO层的Mapper接口,使用@Mapper注解标注:
```java
@Mapper
public interface OrderMapper {
/**
* 查询订单列表
* @param userId 用户ID
* @return 订单列表
*/
List<Order> selectOrderList(@Param("userId") Long userId);
/**
* 新增订单
* @param order 订单对象
* @return 影响行数
*/
int insertOrder(Order order);
}
```
4. 编写Service
编写Service层的接口和实现类:
```java
public interface OrderService {
/**
* 查询订单列表
* @param userId 用户ID
* @return 订单列表
*/
List<Order> getOrderList(Long userId);
/**
* 新增订单
* @param order 订单对象
* @return 影响行数
*/
int addOrder(Order order);
}
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
@Override
public List<Order> getOrderList(Long userId) {
return orderMapper.selectOrderList(userId);
}
@Override
public int addOrder(Order order) {
return orderMapper.insertOrder(order);
}
}
```
5. 测试
编写测试类进行测试:
```java
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderServiceTest {
@Autowired
private OrderService orderService;
@Test
public void testGetOrderList() {
List<Order> orderList = orderService.getOrderList(1L); // 查询用户ID为1的订单列表
System.out.println(orderList);
}
@Test
public void testAddOrder() {
Order order = new Order();
order.setUserId(1L);
order.setOrderId(1L);
order.setOrderName("测试订单");
int result = orderService.addOrder(order); // 新增订单
System.out.println(result);
}
}
```
以上就是Spring Boot集成Sharding JDBC的全部步骤,希望对你有所帮助。
阅读全文