前言
白嫖容易,创作不易,若本篇配置对你产生了帮助,烦请点赞支持(关注一下更好)
依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
插件配置
一般建立config包,新建一个配置类
一般添加插件直接使用
@Configuration
@MapperScan(value = "com.sucker.eduservice.mapper")
public class EduConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
自动填充
首先在实体类上需要添加注解@TableField(fill = FieldFill.INSERT)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@Version
@TableField(fill = FieldFill.INSERT)
private Integer version;
@TableLogic
private Integer deleted;
}
在新建handler包,新建MyMetaObjectHandler类,需要实现MetaObjectHandler
在其中进行自动填充配置
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("updateTime", new Date(), metaObject);
this.setFieldValByName("version", 1, metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
代码生成器配置
一般只写在test目录中,因为只使用一次
package com.sucker.demo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.junit.Test;
import java.util.Collections;
public class CodeGenerator {
@Test
public void test(){
FastAutoGenerator.create("jdbc:mysql://localhost:3306/guli_edu?useUnicode=true&characterEncoding=utf8",
"root", "123456")
.globalConfig(builder -> {
builder.author("sucker")
.enableSwagger()
.dateType(DateType.ONLY_DATE)
.fileOverride()
.outputDir("D:\\JavaCode\\gulimall\\guli_parent1\\service\\service_edu"+"\\src\\main\\java");
})
.packageConfig(builder -> {
builder.parent("com.sucker")
.moduleName("eduservice")
.controller("controller")
.entity("entity")
.service("service")
.mapper("mapper")
.xml("mapper")
.pathInfo(Collections.singletonMap(OutputFile.mapperXml,
"D:\\JavaCode\\gulimall\\guli_parent1\\service\\service_edu\\src\\main\\resources\\mapper"));
})
.strategyConfig(builder -> {
builder.addInclude("edu_teacher")
.addTablePrefix("t_", "c_")
.serviceBuilder()
.formatServiceFileName("%sService")
.entityBuilder()
.enableLombok()
.logicDeleteColumnName("is_deleted")
.enableTableFieldAnnotation()
.controllerBuilder()
.formatFileName("%sController")
.enableRestStyle()
.mapperBuilder()
.enableBaseResultMap()
.superClass(BaseMapper.class)
.formatMapperFileName("%sMapper")
.enableMapperAnnotation()
.formatXmlFileName("%sMapper");
})
.templateEngine(new VelocityTemplateEngine())
.execute();
}
}
|