feat: 读写分离
This commit is contained in:
parent
ca6e8eff22
commit
8b924446ed
12
pom.xml
12
pom.xml
@ -28,10 +28,16 @@
|
|||||||
<artifactId>spring-boot-starter-aop</artifactId>
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- 实现对 MyBatis 的自动化配置 -->
|
<!-- 实现对 MyBatis 的自动化配置 -->
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
|
||||||
|
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
|
||||||
|
<!-- <version>2.1.1</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
<!-- 实现对 MyBatis Plus 的自动化配置 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mybatis.spring.boot</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
<version>2.1.1</version>
|
<version>3.2.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
|
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package com.yupi.project.mapper;
|
package com.yupi.project.mapper;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.yupi.project.model.entity.OrderDO;
|
import com.yupi.project.model.entity.OrderDO;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface OrderMapper {
|
public interface OrderMapper extends BaseMapper<OrderDO> {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void insert(OrderDO order);
|
// void insert(OrderDO order);
|
||||||
|
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package com.yupi.project.model.entity;
|
package com.yupi.project.model.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
@ -9,6 +11,8 @@ import java.io.Serializable;
|
|||||||
* @author Administrator
|
* @author Administrator
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@TableName(value = "orders")
|
||||||
|
@Accessors(chain = true)
|
||||||
public class OrderDO implements Serializable {
|
public class OrderDO implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
36
src/main/java/com/yupi/project/service/OrderService.java
Normal file
36
src/main/java/com/yupi/project/service/OrderService.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.yupi.project.service;
|
||||||
|
|
||||||
|
import com.yupi.project.mapper.OrderMapper;
|
||||||
|
import com.yupi.project.model.entity.OrderDO;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Administrator
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class OrderService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OrderMapper orderMapper;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void add(OrderDO order) {
|
||||||
|
// <1.1> 这里先假模假样的读取一下。读取从库
|
||||||
|
OrderDO exists = orderMapper.selectById(1);
|
||||||
|
System.out.println(exists);
|
||||||
|
|
||||||
|
// <1.2> 插入订单
|
||||||
|
orderMapper.insert(order);
|
||||||
|
|
||||||
|
// <1.3> 这里先假模假样的读取一下。读取主库
|
||||||
|
exists = orderMapper.selectById(1);
|
||||||
|
System.out.println(exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderDO findById(Integer id) {
|
||||||
|
return orderMapper.selectById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -35,6 +35,11 @@ spring:
|
|||||||
inline:
|
inline:
|
||||||
sharding-column: user_id
|
sharding-column: user_id
|
||||||
algorithm-expression: orders_${user_id % 8}
|
algorithm-expression: orders_${user_id % 8}
|
||||||
|
# 读写分离配置,对应 YamlMasterSlaveRuleConfiguration 配置类
|
||||||
|
masterslave:
|
||||||
|
name: ms # 名字,任意,需要保证唯一
|
||||||
|
master-data-source-name: db0 # 主库数据源
|
||||||
|
slave-data-source-names: db1 # 从库数据源
|
||||||
# 拓展属性配置
|
# 拓展属性配置
|
||||||
props:
|
props:
|
||||||
sql:
|
sql:
|
||||||
@ -43,6 +48,14 @@ server:
|
|||||||
port: 7529
|
port: 7529
|
||||||
servlet:
|
servlet:
|
||||||
context-path: /api
|
context-path: /api
|
||||||
mybatis:
|
# mybatis-plus 配置内容
|
||||||
mapper-locations: classpath*:src/main/resources/mapper/*.xml
|
mybatis-plus:
|
||||||
|
configuration:
|
||||||
|
map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。
|
||||||
|
global-config:
|
||||||
|
db-config:
|
||||||
|
id-type: none # 虽然 MyBatis Plus 也提供 ID 生成策略,但是还是使用 Sharding-JDBC 的
|
||||||
|
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
|
||||||
|
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
|
||||||
|
mapper-locations: classpath*:mapper/*.xml
|
||||||
type-aliases-package: com.yupi.project.model.entity
|
type-aliases-package: com.yupi.project.model.entity
|
@ -2,6 +2,7 @@ package com.yupi.project.service;
|
|||||||
|
|
||||||
import com.yupi.project.mapper.OrderMapper;
|
import com.yupi.project.mapper.OrderMapper;
|
||||||
import com.yupi.project.model.entity.OrderDO;
|
import com.yupi.project.model.entity.OrderDO;
|
||||||
|
import org.apache.shardingsphere.api.hint.HintManager;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -15,14 +16,39 @@ public class OrderMapperTest {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private OrderMapper orderMapper;
|
private OrderMapper orderMapper;
|
||||||
|
@Autowired
|
||||||
|
private OrderService orderService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSelectById() { // 测试从库的负载均衡
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
OrderDO order = orderMapper.selectById(1);
|
||||||
|
System.out.println(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSelectById02() { // 测试强制访问主库
|
||||||
|
try (HintManager hintManager = HintManager.getInstance()) {
|
||||||
|
// 设置强制访问主库
|
||||||
|
hintManager.setMasterRouteOnly();
|
||||||
|
// 执行查询
|
||||||
|
OrderDO order = orderMapper.selectById(1);
|
||||||
|
System.out.println(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInsert() {
|
public void testInsert() {
|
||||||
OrderDO order = new OrderDO();
|
OrderDO order = new OrderDO();
|
||||||
order.setId(2L);
|
order.setUserId(3);
|
||||||
order.setUserId(4);
|
|
||||||
orderMapper.insert(order);
|
orderMapper.insert(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGroup() {
|
||||||
|
orderService.add(new OrderDO().setUserId(10));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user