[feat] 集成mybatis-plus的测试demo
This commit is contained in:
parent
86ae9df482
commit
695335932c
36
mybatis-plus-test/pom.xml
Normal file
36
mybatis-plus-test/pom.xml
Normal file
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi</artifactId>
|
||||
<version>3.8.7</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>mybatis-plus-test</artifactId>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-admin</artifactId>
|
||||
<version>3.8.7</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,31 @@
|
||||
package com.ruoyi.mp.constant;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||
|
||||
/**
|
||||
* @author yexuejc
|
||||
* @class-name OrderStatus
|
||||
* @description
|
||||
* @date 2024/1/19 15:29
|
||||
*/
|
||||
public enum OrderStatus {
|
||||
/** 订单状态:默认:未支付 */
|
||||
NORMAL(0),
|
||||
/** 订单状态:已支付,待发货 */
|
||||
PAYED(1),
|
||||
/** 订单状态:已发货,待收货 */
|
||||
SHIPPED(2),
|
||||
/** 订单状态:完成,已收货 */
|
||||
RECEIVED(3),
|
||||
/** 订单状态:取消 */
|
||||
CANCEL(4),
|
||||
;
|
||||
|
||||
|
||||
@EnumValue
|
||||
public final int code;
|
||||
|
||||
OrderStatus(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.ruoyi.mp.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.Version;
|
||||
import com.ruoyi.common.core.domain.BaseEntity;
|
||||
|
||||
/**
|
||||
* 带乐观锁版本的BaseEntity
|
||||
*
|
||||
* @author yexuejc
|
||||
* @class-name BaseVersionEntity
|
||||
* @description
|
||||
* @date 2024/1/19 14:11
|
||||
*/
|
||||
public class BaseVersionEntity extends BaseEntity {
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public BaseVersionEntity setVersion(long version) {
|
||||
this.version = version;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
package com.ruoyi.mp.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.ruoyi.mp.constant.OrderStatus;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author yexuejc
|
||||
* @class-name UserOrder
|
||||
* @description
|
||||
* @date 2024/1/19 14:10
|
||||
*/
|
||||
public class UserOrder extends BaseVersionEntity {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@TableId(value = "order_id", type = IdType.AUTO)
|
||||
private Long orderId;
|
||||
private Long uid;
|
||||
private Long gid;
|
||||
|
||||
@TableField("order_time")
|
||||
private Date orderTime;
|
||||
private Long amount;
|
||||
@TableField("pay_type")
|
||||
private String payType;
|
||||
@TableField("goods_name")
|
||||
private String goodsName;
|
||||
|
||||
private OrderStatus status;
|
||||
|
||||
public OrderStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public UserOrder setStatus(OrderStatus status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Long getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public UserOrder setOrderId(Long orderId) {
|
||||
this.orderId = orderId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Long getUid() {
|
||||
return uid;
|
||||
}
|
||||
|
||||
public UserOrder setUid(Long uid) {
|
||||
this.uid = uid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Long getGid() {
|
||||
return gid;
|
||||
}
|
||||
|
||||
public UserOrder setGid(Long gid) {
|
||||
this.gid = gid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Date getOrderTime() {
|
||||
return orderTime;
|
||||
}
|
||||
|
||||
public UserOrder setOrderTime(Date orderTime) {
|
||||
this.orderTime = orderTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Long getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public UserOrder setAmount(Long amount) {
|
||||
this.amount = amount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getPayType() {
|
||||
return payType;
|
||||
}
|
||||
|
||||
public UserOrder setPayType(String payType) {
|
||||
this.payType = payType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getGoodsName() {
|
||||
return goodsName;
|
||||
}
|
||||
|
||||
public UserOrder setGoodsName(String goodsName) {
|
||||
this.goodsName = goodsName;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserOrder{" +
|
||||
"orderId=" + orderId +
|
||||
", uid=" + uid +
|
||||
", gid=" + gid +
|
||||
", orderTime=" + orderTime +
|
||||
", amount=" + amount +
|
||||
", payType='" + payType + '\'' +
|
||||
", goodsName='" + goodsName + '\'' +
|
||||
", status=" + status +
|
||||
", version=" + getVersion() +
|
||||
", create_by=" + getCreateBy() +
|
||||
", create_time=" + getCreateTime() +
|
||||
", update_by=" + getUpdateBy() +
|
||||
", update_time=" + getUpdateTime() +
|
||||
"} " ;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.ruoyi.mp.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.ruoyi.mp.domain.UserOrder;
|
||||
|
||||
/**
|
||||
* @author yexuejc
|
||||
* @class-name UserOrderMapper
|
||||
* @description
|
||||
* @date 2024/1/19 14:15
|
||||
*/
|
||||
public interface UserOrderMapper extends BaseMapper<UserOrder> {
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.ruoyi.mp.service;
|
||||
|
||||
import com.ruoyi.mp.constant.OrderStatus;
|
||||
import com.ruoyi.mp.domain.UserOrder;
|
||||
import com.ruoyi.mp.mapper.UserOrderMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author yexuejc
|
||||
* @class-name UserOrderService
|
||||
* @description
|
||||
* @date 2024/1/19 14:15
|
||||
*/
|
||||
@Service
|
||||
public class UserOrderService {
|
||||
|
||||
@Resource
|
||||
UserOrderMapper userOrderMapper;
|
||||
|
||||
public void add() {
|
||||
UserOrder entity = new UserOrder();
|
||||
entity.setUid(123L);
|
||||
entity.setGid(4885L);
|
||||
entity.setStatus(OrderStatus.NORMAL);
|
||||
entity.setOrderTime(new Date());
|
||||
entity.setAmount(15200L);
|
||||
entity.setPayType("W");
|
||||
entity.setGoodsName("测试商品");
|
||||
int row = userOrderMapper.insert(entity);
|
||||
if (row > 0) {
|
||||
System.out.println("添加成功");
|
||||
System.out.println(entity);
|
||||
} else {
|
||||
System.out.println("添加失败");
|
||||
}
|
||||
}
|
||||
|
||||
public UserOrder get(long orderId) {
|
||||
return userOrderMapper.selectById(orderId);
|
||||
}
|
||||
|
||||
public void update(UserOrder order) {
|
||||
int row = userOrderMapper.updateById(order);
|
||||
if (row > 0) {
|
||||
System.out.println("更新成功");
|
||||
} else {
|
||||
System.out.println("更新失败");
|
||||
}
|
||||
}
|
||||
}
|
18
mybatis-plus-test/src/main/resources/init.sql
Normal file
18
mybatis-plus-test/src/main/resources/init.sql
Normal file
@ -0,0 +1,18 @@
|
||||
CREATE TABLE `user_order`
|
||||
(
|
||||
`order_id` int NOT NULL AUTO_INCREMENT,
|
||||
`uid` int DEFAULT NULL,
|
||||
`gid` int DEFAULT NULL,
|
||||
`order_time` datetime DEFAULT NULL,
|
||||
`amount` int DEFAULT NULL,
|
||||
`status` int DEFAULT NULL,
|
||||
`pay_type` varchar(1) DEFAULT NULL,
|
||||
`goods_name` varchar(100) DEFAULT NULL,
|
||||
`version` bigint DEFAULT '0',
|
||||
`create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '创建者',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '' COMMENT '更新者',
|
||||
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
|
||||
`remark` varchar(500) DEFAULT NULL comment '备注',
|
||||
PRIMARY KEY (`order_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
24
mybatis-plus-test/src/test/java/com/ruoyi/mp/Config.java
Normal file
24
mybatis-plus-test/src/test/java/com/ruoyi/mp/Config.java
Normal file
@ -0,0 +1,24 @@
|
||||
package com.ruoyi.mp;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 按照实际情况设定自动获取操作者
|
||||
* @author yexuejc
|
||||
* @class-name Config
|
||||
* @description
|
||||
* @date 2024/1/19 15:46
|
||||
*/
|
||||
@Component
|
||||
public class Config {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MetaObjectHandler metaObjectHandler() {
|
||||
return new TestMetaObjectHandler();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.ruoyi.mp;
|
||||
|
||||
import com.ruoyi.web.core.config.MplusMetaObjectHandler;
|
||||
|
||||
/**
|
||||
* @author: yexuejc
|
||||
* @date: 2020-10-13 15:25:28
|
||||
*/
|
||||
public class TestMetaObjectHandler extends MplusMetaObjectHandler {
|
||||
@Override
|
||||
protected String getOperator() {
|
||||
return "test";
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.ruoyi.mp.service;
|
||||
|
||||
import com.ruoyi.RuoYiApplication;
|
||||
import com.ruoyi.mp.constant.OrderStatus;
|
||||
import com.ruoyi.mp.domain.UserOrder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* mybatis-plus
|
||||
* 乐观锁
|
||||
* 自动注入创建时间,操作者
|
||||
* 主键数据库自增
|
||||
*/
|
||||
@SpringBootTest(classes = RuoYiApplication.class)
|
||||
class UserOrderServiceTest {
|
||||
|
||||
@Resource
|
||||
UserOrderService userOrderService;
|
||||
|
||||
/***
|
||||
* 创建表数据
|
||||
*/
|
||||
@Test
|
||||
void add() {
|
||||
userOrderService.add();
|
||||
//15:49:29.640 [main] DEBUG c.r.m.m.U.insert - [debug,135] - ==> Preparing: INSERT INTO user_order ( uid, gid, order_time, amount, pay_type, goods_name, status, version, create_by, create_time, update_by, update_time ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
|
||||
//15:49:29.789 [main] DEBUG c.r.m.m.U.insert - [debug,135] - ==> Parameters: 123(Long), 4885(Long), 2024-01-19 15:49:29.628(Timestamp), 15200(Long), W(String), 测试商品(String), 0(Integer), 0(Long), test(String), 2024-01-19 15:49:29.638(Timestamp), test(String), 2024-01-19 15:49:29.639(Timestamp)
|
||||
//15:49:29.932 [main] DEBUG c.r.m.m.U.insert - [debug,135] - <== Updates: 1
|
||||
//添加成功
|
||||
//UserOrder{orderId=4, uid=123, gid=4885, orderTime=Fri Jan 19 15:49:29 CST 2024, amount=15200, payType='W', goodsName='测试商品', status=NORMAL, version=0, create_by=test, create_time=Fri Jan 19 15:49:29 CST 2024, update_by=test, update_time=Fri Jan 19 15:49:29 CST 2024}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新操作
|
||||
*/
|
||||
@Test
|
||||
void update() {
|
||||
UserOrder userOrder = userOrderService.get(4);
|
||||
System.out.println("更新前:" + userOrder);
|
||||
userOrder.setStatus(OrderStatus.PAYED);
|
||||
userOrderService.update(userOrder);
|
||||
userOrder = userOrderService.get(4);
|
||||
System.out.println("更新后:" + userOrder);
|
||||
//15:50:42.433 [main] DEBUG c.r.m.m.U.selectById - [debug,135] - ==> Preparing: SELECT order_id,uid,gid,order_time,amount,pay_type,goods_name,status,version,create_by,create_time,update_by,update_time FROM user_order WHERE order_id=?
|
||||
//15:50:42.436 [main] DEBUG c.r.m.m.U.selectById - [debug,135] - ==> Parameters: 4(Long)
|
||||
//15:50:42.449 [main] DEBUG c.r.m.m.U.selectById - [debug,135] - <== Total: 1
|
||||
//更新前:UserOrder{orderId=4, uid=123, gid=4885, orderTime=Fri Jan 19 15:49:30 CST 2024, amount=15200, payType='W', goodsName='测试商品', status=NORMAL, version=0, create_by=test, create_time=Fri Jan 19 15:49:30 CST 2024, update_by=test, update_time=Fri Jan 19 15:49:30 CST 2024}
|
||||
//15:50:42.463 [main] DEBUG c.r.m.m.U.updateById - [debug,135] - ==> Preparing: UPDATE user_order SET uid=?, gid=?, order_time=?, amount=?, pay_type=?, goods_name=?, status=?, version=?, create_by=?, create_time=?, update_by=?, update_time=? WHERE order_id=? AND version=?
|
||||
//15:50:42.473 [main] DEBUG c.r.m.m.U.updateById - [debug,135] - ==> Parameters: 123(Long), 4885(Long), 2024-01-19 15:49:30.0(Timestamp), 15200(Long), W(String), 测试商品(String), 1(Integer), 1(Long), test(String), 2024-01-19 15:49:30.0(Timestamp), test(String), 2024-01-19 15:49:30.0(Timestamp), 4(Long), 0(Long)
|
||||
//15:50:42.675 [main] DEBUG c.r.m.m.U.updateById - [debug,135] - <== Updates: 1
|
||||
//更新成功
|
||||
//15:50:42.676 [main] DEBUG c.r.m.m.U.selectById - [debug,135] - ==> Preparing: SELECT order_id,uid,gid,order_time,amount,pay_type,goods_name,status,version,create_by,create_time,update_by,update_time FROM user_order WHERE order_id=?
|
||||
//15:50:42.677 [main] DEBUG c.r.m.m.U.selectById - [debug,135] - ==> Parameters: 4(Long)
|
||||
//15:50:42.682 [main] DEBUG c.r.m.m.U.selectById - [debug,135] - <== Total: 1
|
||||
//更新后:UserOrder{orderId=4, uid=123, gid=4885, orderTime=Fri Jan 19 15:49:30 CST 2024, amount=15200, payType='W', goodsName='测试商品', status=PAYED, version=1, create_by=test, create_time=Fri Jan 19 15:49:30 CST 2024, update_by=test, update_time=Fri Jan 19 15:49:30 CST 2024}
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user