<>一、注解
在数据库中加入create_time 和update_time字段
方法一:数据库级别的操作(开发中不建议)
方法二:代码级别的操作,在实体类中注释
@TableField(fill = FieldFill.INSERT) private Date createTime; @TableField(fill
= FieldFill.UPDATE) private Date updateTime;
设置为自增长
@TableId(type = IdType.AUTO) private Long id;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KBqFI5Ef-1644742291260)(MyBatisPlus.assets/image-20220213122854906.png)]
注意需要数据库也需要一致!
自动生成成功!
漂亮
<>二、代码模板
package com.tao; 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 java.util.ArrayList; //代码自动生成 public class TaoCode { public static void
main(String[] args) { // 需要构造一个代码生成器对象 AutoGenerator mpg = new AutoGenerator();
// 配置策略 // 1、全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath =
System.getProperty("user.dir"); gc.setOutputDir(projectPath+
"/kuang-mp/src/main/java"); gc.setAuthor("TT"); gc.setOpen(false); gc.
setFileOverride(true); gc.setServiceName("%sService");//去Service的I前缀 gc.
setIdType(IdType.ID_WORKER); gc.setDateType(DateType.ONLY_DATE); gc.setSwagger2(
true);//自动配置swagger2 mpg.setGlobalConfig(gc); //2配置数据源 DataSourceConfig dsc =
new DataSourceConfig(); dsc.setUrl(
"jdbc:mysql://localhost:3306/ry-vue?useSSL=false&useUnicode=ture&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull"
); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.
setPassword("123456"); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc);
//3包的配置 PackageConfig pc = new PackageConfig(); pc.setModuleName("ruyi"); pc.
setParent("com.tao"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.
setService("service"); pc.setController("controller"); mpg.setPackageInfo(pc);
//4、策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude(
"gen_table","gen_table_column","sys_user_post"); //设置要映射的表名 strategy.setNaming(
NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.
underline_to_camel); TableFill createTime = new TableFill("create_time",
FieldFill.INSERT); TableFill updateTime = new TableFill("update_time", FieldFill
.INSERT_UPDATE); ArrayList<TableFill> tableFills = new ArrayList<>(); tableFills
.add(createTime); tableFills.add(updateTime); strategy.setTableFillList(
tableFills); //格式 strategy.setRestControllerStyle(true); strategy.
setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy); mpg.execute();
//执行 } }
gy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy); mpg.execute();//执行 }
}