[feat] 集成mybatis-plus的测试demo

This commit is contained in:
2024-01-19 16:12:15 +08:00
parent 86ae9df482
commit 695335932c
10 changed files with 400 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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() +
"} " ;
}
}

View File

@@ -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> {
}

View File

@@ -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("更新失败");
}
}
}