封装mybatis-plus
This commit is contained in:
parent
c144f66df5
commit
9de7b666db
49
axzo-common-datas/axzo-data-mybatis-plus/pom.xml
Normal file
49
axzo-common-datas/axzo-data-mybatis-plus/pom.xml
Normal file
@ -0,0 +1,49 @@
|
||||
<?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>
|
||||
<artifactId>axzo-common-datas</artifactId>
|
||||
<groupId>cn.axzo.framework.data</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>axzo-data-mybatis-plus</artifactId>
|
||||
<name>Axzo Common Data Mybatis Plus</name>
|
||||
|
||||
<dependencies>
|
||||
<!-- Compile -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
<!--spring-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--mybatis-plus-->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-extension</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--Optional-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -0,0 +1,56 @@
|
||||
package cn.axzo.framework.data.mybatisplus.config;
|
||||
|
||||
import cn.axzo.framework.data.mybatisplus.model.BaseEntity;
|
||||
import cn.axzo.framework.data.mybatisplus.model.BaseOwnEntity;
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Author: liyong.tian
|
||||
* @Date: 2022/11/4 11:34
|
||||
* @Description:
|
||||
*/
|
||||
public class EntityMetaObjectHandler implements MetaObjectHandler {
|
||||
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
Object entity = metaObject.getOriginalObject();
|
||||
if (entity instanceof BaseEntity) {
|
||||
//默认有值不覆盖
|
||||
this.fillStrategy(metaObject, "createAt", new Date());
|
||||
this.fillStrategy(metaObject, "updateAt", new Date());
|
||||
}
|
||||
|
||||
if (entity instanceof BaseOwnEntity) {
|
||||
//默认有值不覆盖
|
||||
this.fillStrategy(metaObject, "createBy", getAcctId());
|
||||
this.fillStrategy(metaObject, "updateBy", getAcctId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
Object entity = metaObject.getOriginalObject();
|
||||
if (entity instanceof BaseEntity) {
|
||||
//强制覆盖
|
||||
this.setFieldValByName("updateAt", new Date(), metaObject);
|
||||
}
|
||||
|
||||
if (entity instanceof BaseOwnEntity) {
|
||||
//强制覆盖
|
||||
this.setFieldValByName("updateBy", getAcctId(), metaObject);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户账户id
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Long getAcctId() {
|
||||
return 1L;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package cn.axzo.framework.data.mybatisplus.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* @Author: liyong.tian
|
||||
* @Date: 2022/11/4 10:40
|
||||
* @Description:
|
||||
*/
|
||||
@EnableTransactionManagement
|
||||
@Configuration
|
||||
public class MybatisPlusAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EntityMetaObjectHandler EntityMetaObjectHandler () {
|
||||
return new EntityMetaObjectHandler();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package cn.axzo.framework.data.mybatisplus.model;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.activerecord.Model;
|
||||
|
||||
/**
|
||||
* @Author: liyong.tian
|
||||
* @Date: 2022/11/4 10:29
|
||||
* @Description: 支持类
|
||||
*/
|
||||
public abstract class BaseDataEntity <T extends Model<T>> extends BaseEntity<T>{
|
||||
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package cn.axzo.framework.data.mybatisplus.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.extension.activerecord.Model;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Author: liyong.tian
|
||||
* @Date: 2022/11/4 10:26
|
||||
* @Description: 实体基础类
|
||||
*/
|
||||
@Data
|
||||
public abstract class BaseEntity<T extends Model<?>> extends Model<T> {
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 是否删除:0否 1是
|
||||
*/
|
||||
@TableField(value = "is_delete", fill = FieldFill.INSERT)
|
||||
private Integer isDelete = 0;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(value = "create_at", fill = FieldFill.INSERT)
|
||||
private Date createAt;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@TableField(value = "update_at", fill = FieldFill.INSERT_UPDATE)
|
||||
private Date updateAt;
|
||||
}
|
||||
|
||||
@ -0,0 +1,27 @@
|
||||
package cn.axzo.framework.data.mybatisplus.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.extension.activerecord.Model;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author: liyong.tian
|
||||
* @Date: 2022/11/4 10:30
|
||||
* @Description: 支持类
|
||||
*/
|
||||
@Data
|
||||
public abstract class BaseOwnEntity<T extends Model<T>> extends BaseDataEntity<T> {
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
protected Long createBy;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@TableField(fill = FieldFill.UPDATE)
|
||||
protected Long updateBy;
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
package cn.axzo.framework.data.mybatisplus;
|
||||
@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.axzo.framework.data.mybatisplus.config.MybatisPlusAutoConfiguration
|
||||
21
axzo-common-datas/pom.xml
Normal file
21
axzo-common-datas/pom.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?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>
|
||||
<artifactId>axzo-framework-commons</artifactId>
|
||||
<groupId>cn.axzo.framework</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>cn.axzo.framework.data</groupId>
|
||||
<artifactId>axzo-common-datas</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<name>Axzo Common Data Parent</name>
|
||||
|
||||
<modules>
|
||||
<module>axzo-data-mybatis-plus</module>
|
||||
</modules>
|
||||
</project>
|
||||
8
pom.xml
8
pom.xml
@ -33,6 +33,7 @@
|
||||
<module>axzo-common-webmvc</module>
|
||||
<module>axzo-common-autoconfigure</module>
|
||||
<module>axzo-common-jackson</module>
|
||||
<module>axzo-common-datas</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
@ -129,6 +130,13 @@
|
||||
<version>${axzo-commons.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- common datas -->
|
||||
<dependency>
|
||||
<groupId>cn.axzo.framework.data</groupId>
|
||||
<artifactId>axzo-data-mybatis-plus</artifactId>
|
||||
<version>${axzo-commons.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--common jackson-->
|
||||
<dependency>
|
||||
<groupId>cn.axzo.framework.jackson</groupId>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user