mybatisplus多张关联表新增
时间: 2023-12-13 18:34:34 浏览: 73
mybatis-plus-join是mybatis plus的一个多表插件
以下是Mybatis Plus多张关联表新增的示例代码:
1.定义实体类
```java
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String orderNo;
private Long userId;
private User user;
private List<OrderItem> orderItems;
// 省略getter和setter方法
}
public class OrderItem implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Long orderId;
private Long productId;
private Integer quantity;
private Product product;
// 省略getter和setter方法
}
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private BigDecimal price;
// 省略getter和setter方法
}
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String password;
// 省略getter和setter方法
}
```
2.定义Mapper接口
```java
public interface OrderMapper extends BaseMapper<Order> {
int insertOrder(Order order);
}
public interface OrderItemMapper extends BaseMapper<OrderItem> {
int insertOrderItem(OrderItem orderItem);
}
public interface ProductMapper extends BaseMapper<Product> {
// 省略其他方法
}
public interface UserMapper extends BaseMapper<User> {
// 省略其他方法
}
```
3.定义Mapper.xml文件
```xml
<!-- OrderMapper.xml -->
<mapper namespace="com.example.mapper.OrderMapper">
<insert id="insertOrder" parameterType="com.example.entity.Order">
insert into `order` (order_no, user_id) values (#{orderNo}, #{userId})
</insert>
</mapper>
<!-- OrderItemMapper.xml -->
<mapper namespace="com.example.mapper.OrderItemMapper">
<insert id="insertOrderItem" parameterType="com.example.entity.OrderItem">
insert into order_item (order_id, product_id, quantity) values (#{orderId}, #{productId}, #{quantity})
</insert>
</mapper>
```
4.定义Service层
```java
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Autowired
private ProductMapper productMapper;
@Autowired
private UserMapper userMapper;
@Override
@Transactional
public void addOrder(Order order) {
// 插入订单
orderMapper.insertOrder(order);
Long orderId = order.getId();
// 插入订单项
List<OrderItem> orderItems = order.getOrderItems();
for (OrderItem orderItem : orderItems) {
orderItem.setOrderId(orderId);
orderItemMapper.insertOrderItem(orderItem);
}
}
}
```
5.调用Service层方法进行新增操作
```java
Order order = new Order();
order.setOrderNo("202201010001");
order.setUserId(1L);
User user = userMapper.selectById(order.getUserId());
order.setUser(user);
List<OrderItem> orderItems = new ArrayList<>();
OrderItem orderItem1 = new OrderItem();
orderItem1.setProductId(1L);
orderItem1.setQuantity(2);
Product product1 = productMapper.selectById(orderItem1.getProductId());
orderItem1.setProduct(product1);
orderItems.add(orderItem1);
OrderItem orderItem2 = new OrderItem();
orderItem2.setProductId(2L);
orderItem2.setQuantity(1);
Product product2 = productMapper.selectById(orderItem2.getProductId());
orderItem2.setProduct(product2);
orderItems.add(orderItem2);
order.setOrderItems(orderItems);
orderService.addOrder(order);
```
阅读全文