Compare commits
2 Commits
main
...
mybatis-pl
Author | SHA1 | Date | |
---|---|---|---|
695335932c | |||
86ae9df482 |
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}
|
||||
|
||||
}
|
||||
}
|
9
pom.xml
9
pom.xml
@ -30,6 +30,7 @@
|
||||
<poi.version>4.1.2</poi.version>
|
||||
<velocity.version>2.3</velocity.version>
|
||||
<jwt.version>0.9.1</jwt.version>
|
||||
<mybatis-plus.version>3.5.5</mybatis-plus.version>
|
||||
</properties>
|
||||
|
||||
<!-- 依赖声明 -->
|
||||
@ -45,6 +46,13 @@
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<!--mybatis-plus配置:适配SpringBoot2-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>${mybatis-plus.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 阿里数据库连接池 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
@ -180,6 +188,7 @@
|
||||
<module>ruoyi-quartz</module>
|
||||
<module>ruoyi-generator</module>
|
||||
<module>ruoyi-common</module>
|
||||
<module>mybatis-plus-test</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
@ -0,0 +1,96 @@
|
||||
package com.ruoyi.web.core.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 注入公共字段自动填充,任选注入方式即可
|
||||
* <h2>
|
||||
* 继承使用
|
||||
* </h2>
|
||||
* <p>
|
||||
* 继承后<i>重写 {@link #getOperator()}</i> 和 <i>重新注入 Bean</i>
|
||||
* </p>
|
||||
* <pre>
|
||||
* @author yexuejc
|
||||
* @Bean
|
||||
* public MyMetaObjectHandler metaObjectHandler() {
|
||||
* return new MyMetaObjectHandler();
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public class MplusMetaObjectHandler implements MetaObjectHandler {
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
protected String crtTimeColumn = "createTime";
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
protected String crtByColumn = "createBy";
|
||||
/**
|
||||
* 最后操作时间
|
||||
*/
|
||||
protected String updateTimeColumn = "updateTime";
|
||||
/**
|
||||
* 最后操作人
|
||||
*/
|
||||
protected String updateByColumn = "updateBy";
|
||||
|
||||
/**
|
||||
* 设置操作者
|
||||
* <p>
|
||||
* 应设置登录账号为操作者,这里由每个工程具体实现
|
||||
* </p>
|
||||
* <p>
|
||||
* 返回null时,会查找consumerId作为操作者,找不到会设置默认值
|
||||
* <br/>
|
||||
* 返回"" 时,会存""
|
||||
* </p>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected String getOperator() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
Object crtTime = metaObject.getValue(crtTimeColumn);
|
||||
if (crtTime == null) {
|
||||
this.strictInsertFill(metaObject, crtTimeColumn, Date.class, new Date());
|
||||
}
|
||||
|
||||
Object crtBy = metaObject.getValue(crtByColumn);
|
||||
if (crtBy == null) {
|
||||
this.strictInsertFill(metaObject, crtByColumn, String.class, getOperator());
|
||||
}
|
||||
|
||||
Object mdfyTime = metaObject.getValue(updateTimeColumn);
|
||||
if (mdfyTime == null) {
|
||||
this.strictInsertFill(metaObject, updateTimeColumn, Date.class, new Date());
|
||||
}
|
||||
|
||||
Object mdfyBy = metaObject.getValue(updateByColumn);
|
||||
if (mdfyBy == null) {
|
||||
this.strictInsertFill(metaObject, updateByColumn, String.class, getOperator());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
Object mdfyTime = metaObject.getValue(updateTimeColumn);
|
||||
if (mdfyTime == null) {
|
||||
this.strictInsertFill(metaObject, updateTimeColumn, Date.class, new Date());
|
||||
}
|
||||
|
||||
Object mdfyBy = metaObject.getValue(updateByColumn);
|
||||
if (mdfyBy == null) {
|
||||
this.strictInsertFill(metaObject, updateByColumn, String.class, getOperator());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.ruoyi.web.core.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* MybatisPlus 配置文件
|
||||
*
|
||||
* @author maxf
|
||||
* @version 1.0
|
||||
* @ClassName MybatisPlusConfig
|
||||
* @Description
|
||||
* @date 2018/12/13 11:34
|
||||
*/
|
||||
//Spring boot方式
|
||||
@EnableTransactionManagement
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
/**
|
||||
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
//新的分页插件
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
//乐观锁插件
|
||||
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MetaObjectHandler metaObjectHandler() {
|
||||
return new MplusMetaObjectHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入sql注入器
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DefaultSqlInjector sqlInjector() {
|
||||
return new DefaultSqlInjector();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.ruoyi.web.core.config;
|
||||
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
|
||||
/**
|
||||
* @author: yexuejc
|
||||
* @date: 2020-10-13 15:25:28
|
||||
*/
|
||||
public class RuoyiMetaObjectHandler extends MplusMetaObjectHandler {
|
||||
@Override
|
||||
protected String getOperator() {
|
||||
return SecurityUtils.getUsername();
|
||||
}
|
||||
}
|
@ -97,8 +97,8 @@ token:
|
||||
# 令牌有效期(默认30分钟)
|
||||
expireTime: 30
|
||||
|
||||
# MyBatis配置
|
||||
mybatis:
|
||||
# MyBatis配置(plus)
|
||||
mybatis-plus:
|
||||
# 搜索指定包别名
|
||||
typeAliasesPackage: com.ruoyi.**.domain
|
||||
# 配置mapper的扫描,找到所有的mapper.xml映射文件
|
||||
|
@ -39,6 +39,19 @@
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<!--移除mybatis的依赖-->
|
||||
<exclusion>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<groupId>org.mybatis</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!--mybatis-plus-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 自定义验证注解 -->
|
||||
|
@ -4,6 +4,9 @@ import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
@ -13,32 +16,37 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class BaseEntity implements Serializable
|
||||
{
|
||||
public class BaseEntity implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 搜索值 */
|
||||
/** 搜索值 该字段数据库不需要*/
|
||||
@TableField(exist = false)
|
||||
@JsonIgnore
|
||||
private String searchValue;
|
||||
|
||||
/** 创建者 */
|
||||
/** 创建者 新增时自动填充*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private String createBy;
|
||||
|
||||
/** 创建时间 */
|
||||
/** 创建时间 新增时自动填充*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
/** 更新者 */
|
||||
/** 更新者 新增和修改时自动填充*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private String updateBy;
|
||||
|
||||
/** 更新时间 */
|
||||
/** 更新时间 新增和修改时自动填充*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date updateTime;
|
||||
|
||||
/** 备注 */
|
||||
private String remark;
|
||||
|
||||
/** 请求参数 */
|
||||
/** 请求参数 该字段数据库不需要*/
|
||||
@TableField(exist = false)
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
private Map<String, Object> params;
|
||||
|
||||
|
@ -11,6 +11,7 @@ import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
@ -30,6 +31,7 @@ import com.ruoyi.common.utils.StringUtils;
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "ruoyi.mybatis.enable", matchIfMissing = false)
|
||||
public class MyBatisConfig
|
||||
{
|
||||
@Autowired
|
||||
|
275
update.md
Normal file
275
update.md
Normal file
@ -0,0 +1,275 @@
|
||||
ruoyi mybatis-plus版
|
||||
------------------
|
||||
### 改造
|
||||
#### 依赖配置
|
||||
> 1. [pom.xml](pom.xml)
|
||||
```xml
|
||||
<mybatis-plus.version>3.5.5</mybatis-plus.version>
|
||||
<!--mybatis-plus配置:适配SpringBoot2-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>${mybatis-plus.version}</version>
|
||||
</dependency>
|
||||
```
|
||||
> 2.增加mybatis-plus [ruoyi-admin/pom.xml](ruoyi-admin/pom.xml)
|
||||
```xml
|
||||
<!-- pagehelper 分页插件 -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<!--移除mybatis的依赖-->
|
||||
<exclusion>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<groupId>org.mybatis</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!--mybatis-plus-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</dependency>
|
||||
```
|
||||
#### 代码改修
|
||||
> 1.关掉mybatis的配置 [MyBatisConfig.java](ruoyi-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java)
|
||||
```java
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "ruoyi.mybatis.enable", matchIfMissing = false)
|
||||
public class MyBatisConfig
|
||||
```
|
||||
> 2.追加mybatisPlus的配置,覆盖mybatis的配置: [MybatisPlusConfig.java](ruoyi-admin/src/main/java/com/ruoyi/web/core/config/MybatisPlusConfig.java)
|
||||
<details>
|
||||
<summary>点我展开看代码</summary>
|
||||
<pre><code>
|
||||
package com.ruoyi.web.core.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* MybatisPlus 配置文件
|
||||
*
|
||||
* @author maxf
|
||||
* @version 1.0
|
||||
* @ClassName MybatisPlusConfig
|
||||
* @Description
|
||||
* @date 2018/12/13 11:34
|
||||
*/
|
||||
//Spring boot方式
|
||||
@EnableTransactionManagement
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
/**
|
||||
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
//新的分页插件
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
//乐观锁插件
|
||||
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MetaObjectHandler metaObjectHandler() {
|
||||
return new MplusMetaObjectHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入sql注入器
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DefaultSqlInjector sqlInjector() {
|
||||
return new DefaultSqlInjector();
|
||||
}
|
||||
|
||||
}
|
||||
</code></pre>
|
||||
</details>
|
||||
|
||||
>公共字段的自动填充处理(1):[BaseEntity.java](ruoyi-common/src/main/java/com/ruoyi/common/core/domain/BaseEntity.java)
|
||||
```java
|
||||
/** 搜索值 该字段数据库不需要*/
|
||||
@TableField(exist = false)
|
||||
@JsonIgnore
|
||||
private String searchValue;
|
||||
|
||||
/** 创建者 新增时自动填充*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private String createBy;
|
||||
|
||||
/** 创建时间 新增时自动填充*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
/** 更新者 新增和修改时自动填充*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private String updateBy;
|
||||
|
||||
/** 更新时间 新增和修改时自动填充*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date updateTime;
|
||||
|
||||
/** 备注 */
|
||||
private String remark;
|
||||
|
||||
/** 请求参数 该字段数据库不需要*/
|
||||
@TableField(exist = false)
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
private Map<String, Object> params;
|
||||
```
|
||||
|
||||
>公共字段的自动填充处理(2): [MplusMetaObjectHandler.java](ruoyi-admin/src/main/java/com/ruoyi/web/core/config/MplusMetaObjectHandler.java)
|
||||
<details>
|
||||
<summary>点我展开看代码</summary>
|
||||
<pre><code>
|
||||
package com.ruoyi.web.core.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 注入公共字段自动填充,任选注入方式即可
|
||||
* <h2>
|
||||
* 继承使用
|
||||
* </h2>
|
||||
* <p>
|
||||
* 继承后<i>重写 {@link #getOperator()}</i> 和 <i>重新注入 Bean</i>
|
||||
* </p>
|
||||
* <pre>
|
||||
* @author yexuejc
|
||||
* @Bean
|
||||
* public MyMetaObjectHandler metaObjectHandler() {
|
||||
* return new MyMetaObjectHandler();
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public class MplusMetaObjectHandler implements MetaObjectHandler {
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
protected String crtTimeColumn = "createTime";
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
protected String crtByColumn = "createBy";
|
||||
/**
|
||||
* 最后操作时间
|
||||
*/
|
||||
protected String updateTimeColumn = "updateTime";
|
||||
/**
|
||||
* 最后操作人
|
||||
*/
|
||||
protected String updateByColumn = "updateBy";
|
||||
|
||||
/**
|
||||
* 设置操作者
|
||||
* <p>
|
||||
* 应设置登录账号为操作者,这里由每个工程具体实现
|
||||
* </p>
|
||||
* <p>
|
||||
* 返回null时,会查找consumerId作为操作者,找不到会设置默认值
|
||||
* <br/>
|
||||
* 返回"" 时,会存""
|
||||
* </p>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected String getOperator() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
Object crtTime = metaObject.getValue(crtTimeColumn);
|
||||
if (crtTime == null) {
|
||||
this.strictInsertFill(metaObject, crtTimeColumn, Date.class, new Date());
|
||||
}
|
||||
|
||||
Object crtBy = metaObject.getValue(crtByColumn);
|
||||
if (crtBy == null) {
|
||||
this.strictInsertFill(metaObject, crtByColumn, String.class, getOperator());
|
||||
}
|
||||
|
||||
Object mdfyTime = metaObject.getValue(updateTimeColumn);
|
||||
if (mdfyTime == null) {
|
||||
this.strictInsertFill(metaObject, updateTimeColumn, Date.class, new Date());
|
||||
}
|
||||
|
||||
Object mdfyBy = metaObject.getValue(updateByColumn);
|
||||
if (mdfyBy == null) {
|
||||
this.strictInsertFill(metaObject, updateByColumn, String.class, getOperator());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
Object mdfyTime = metaObject.getValue(updateTimeColumn);
|
||||
if (mdfyTime == null) {
|
||||
this.strictInsertFill(metaObject, updateTimeColumn, Date.class, new Date());
|
||||
}
|
||||
|
||||
Object mdfyBy = metaObject.getValue(updateByColumn);
|
||||
if (mdfyBy == null) {
|
||||
this.strictInsertFill(metaObject, updateByColumn, String.class, getOperator());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</code></pre>
|
||||
</details>
|
||||
|
||||
> 4.自动获取操作者的实现(需要注入为bean): [RuoyiMetaObjectHandler.java](ruoyi-admin/src/main/java/com/ruoyi/web/core/config/RuoyiMetaObjectHandler.java)
|
||||
```
|
||||
package com.ruoyi.web.core.config;
|
||||
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
|
||||
/**
|
||||
* @author: yexuejc
|
||||
* @date: 2020-10-13 15:25:28
|
||||
*/
|
||||
public class RuoyiMetaObjectHandler extends MplusMetaObjectHandler {
|
||||
@Override
|
||||
protected String getOperator() {
|
||||
return SecurityUtils.getUsername();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 配置
|
||||
> mybatis的配置改为mybatis-plus的配置:ruoyi-admin/src/main/resources/application.yml
|
||||
```properties
|
||||
# MyBatis配置(plus)
|
||||
mybatis-plus:
|
||||
# 搜索指定包别名
|
||||
typeAliasesPackage: com.ruoyi.**.domain
|
||||
# 配置mapper的扫描,找到所有的mapper.xml映射文件
|
||||
mapperLocations: classpath*:mapper/**/*Mapper.xml
|
||||
# 加载全局的配置文件
|
||||
configLocation: classpath:mybatis/mybatis-config.xml
|
||||
```
|
||||
### 多出的功能
|
||||
* [mybatis-plus-test](mybatis-plus-test) 集成mybatis-plus后的测试demo
|
||||
> 先创建demo表,[init.sql](mybatis-plus-test/src/main/resources/init.sql);执行参照[UserOrderServiceTest.java](mybatis-plus-test/src/test/java/com/ruoyi/mp/service/UserOrderServiceTest.java)
|
Loading…
x
Reference in New Issue
Block a user