mybatisPlus代码自动生成配置
package com.djz.mybatisplus;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class MybatisplusApplicationTests {
@Test
public void testGenerator() {
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java")
.setAuthor("djz")
.setOpen(false)
.setFileOverride(false)
.setDateType(DateType.ONLY_DATE)
.setSwagger2(true)
.setIdType(IdType.AUTO)
.setServiceName("%sService")
.setBaseResultMap(true)
.setBaseColumnList(true);
mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL)
.setDriverName("com.mysql.jdbc.Driver")
.setUrl("jdbc:mysql://127.0.0.1:3306/rhl?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC")
.setUsername("rhl")
.setPassword("070499");
mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig();
pc.setParent("com.djz")
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("pojo")
.setXml("mapper");
mpg.setPackageInfo(pc);
TableFill createFill = new TableFill("create_time", FieldFill.INSERT);
TableFill updateFill = new TableFill("update_time", FieldFill.INSERT_UPDATE);
List<TableFill> tableFills = new ArrayList();
tableFills.add(createFill);
tableFills.add(updateFill);
StrategyConfig sc = new StrategyConfig();
sc.setInclude("books")
.setEntityLombokModel(true)
.setLogicDeleteFieldName("deleted")
.setTableFillList(tableFills)
.setVersionFieldName("version")
.setRestControllerStyle(true)
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel);
mpg.setStrategy(sc);
mpg.execute();
}
}
自动填充配置
package com.djz.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime",new Date(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}
乐观锁配置
package com.djz.config;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@MapperScan("com.djz.mapper")
@Configuration
public class MyBatisPlusConfig {
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor();
}
@Bean
public ISqlInjector sqlInjector(){
return new LogicSqlInjector();
}
@Bean
@Profile({"dev","test"})
public PerformanceInterceptor performanceInterceptor(){
PerformanceInterceptor performanceInterceptor=new PerformanceInterceptor();
performanceInterceptor.setMaxTime(5000);
performanceInterceptor.setFormat(true);
return performanceInterceptor;
}
}