diff --git a/service/src/main/java/cn/bunny/service/controller/OrderController.java b/service/src/main/java/cn/bunny/service/controller/OrderController.java
index 910b923..c76940b 100644
--- a/service/src/main/java/cn/bunny/service/controller/OrderController.java
+++ b/service/src/main/java/cn/bunny/service/controller/OrderController.java
@@ -3,15 +3,13 @@ package cn.bunny.service.controller;
import cn.bunny.entity.order.Order;
import cn.bunny.pojo.result.Result;
import cn.bunny.service.service.OrderService;
+import cn.bunny.service.service.UserService;
import cn.bunny.vo.page.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
/**
*
@@ -27,6 +25,8 @@ import org.springframework.web.bind.annotation.RestController;
public class OrderController {
@Autowired
private OrderService orderService;
+ @Autowired
+ private UserService userService;
@Operation(summary = "分页查询订单", description = "分页查询订单")
@GetMapping("queryPage/{page}/{limit}")
@@ -35,4 +35,11 @@ public class OrderController {
PageResult vo = orderService.queryPage(pageParams);
return Result.success(vo);
}
+
+ @Operation(summary = "插入数据", description = "插入数据")
+ @PostMapping("addOrder")
+ public Result addOrder(@RequestBody Order order) {
+ orderService.save(order);
+ return Result.success();
+ }
}
diff --git a/service/src/main/resources/sharding/sharding-sharding.yaml b/service/src/main/resources/sharding/sharding-sharding.yaml
index 92b95cb..d757725 100644
--- a/service/src/main/resources/sharding/sharding-sharding.yaml
+++ b/service/src/main/resources/sharding/sharding-sharding.yaml
@@ -15,21 +15,17 @@ dataSources:
jdbcUrl: jdbc:mysql://192.168.3.21:3305/db_order?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
username: root
password: "02120212"
-rules:
- # - !SINGLE
- # # 匹配所有表
- # tables:
- # - "*.*"
- # # 默认数据库
- # defaultDataSource: server_user
+rules:
# 数据分片配置
- !SHARDING
tables:
- server_user:
+ t_user:
actualDataNodes: server_user.t_user
- server_order:
+ t_order:
actualDataNodes: server_order.t_order
+ # 可以配置多个
+ # actualDataNodes: server_order.t_order, server_order.t_order1, server_order.t_order2
# 打印SQL
props:
diff --git a/service/src/test/java/cn/bunny/service/service/impl/OrderServiceTest.java b/service/src/test/java/cn/bunny/service/service/impl/OrderServiceTest.java
new file mode 100644
index 0000000..e339fca
--- /dev/null
+++ b/service/src/test/java/cn/bunny/service/service/impl/OrderServiceTest.java
@@ -0,0 +1,39 @@
+package cn.bunny.service.service.impl;
+
+import cn.bunny.entity.order.Order;
+import cn.bunny.entity.user.User;
+import cn.bunny.service.mapper.OrderMapper;
+import cn.bunny.service.mapper.UserMapper;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.util.DigestUtils;
+
+import java.math.BigDecimal;
+import java.security.NoSuchAlgorithmException;
+
+@SpringBootTest
+public class OrderServiceTest {
+ @Autowired
+ private UserMapper userMapper;
+ @Autowired
+ private OrderMapper orderMapper;
+
+ @Test
+ void insertOrder() throws NoSuchAlgorithmException {
+ double num = Math.random();
+ String md5Hex = DigestUtils.md5DigestAsHex(String.valueOf(num).getBytes());
+
+ // 插入USER数据
+ User user = new User();
+ user.setUname("呵呵呵");
+ userMapper.insert(user);
+
+ // 插入ORDER数据
+ Order order = new Order();
+ order.setOrderNo(md5Hex);
+ order.setUserId(user.getId());
+ order.setAmount(new BigDecimal(100));
+ orderMapper.insert(order);
+ }
+}