旧的代码生成

记得导包,依赖如下


  1. <!-- mybatis-plus -->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-boot-starter</artifactId>
  5. <version>3.5.1</version>
  6. </dependency>
  7. <!-- 代码生成器 一个是新的代码生成器,一个是旧的问题-->
  8. <!-- <dependency>-->
  9. <!-- <groupId>com.baomidou</groupId>-->
  10. <!-- <artifactId>mybatis-plus-generator</artifactId>-->
  11. <!-- <version>3.4.1</version>-->
  12. <!-- </dependency>-->
  13. <dependency>
  14. <groupId>com.baomidou</groupId>
  15. <artifactId>mybatis-plus-generator</artifactId>
  16. <version>3.5.1</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.apache.velocity</groupId>
  20. <artifactId>velocity</artifactId>
  21. <version>1.7</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.apache.velocity</groupId>
  25. <artifactId>velocity-engine-core</artifactId>
  26. <version>2.3</version>
  27. </dependency>

代码


  1. /**
  2. * @description:
  3. * @author: HaHa
  4. * @time: 2022/4/19 20:04
  5. */
  6. public class AutoMain {
  7. public static void main(String[] args) {
  8. AutoGenerator generator = new AutoGenerator();
  9. // 设置全局配置
  10. GlobalConfig config = new GlobalConfig();
  11. String s = System.getProperty("user.dir");
  12. config.setOutputDir(s+"/src/main/java");
  13. config.setAuthor("mybatis自动生成");
  14. config.setOpen(false);
  15. config.setFileOverride(false); //是否文件覆盖
  16. config.setSwagger2(true);
  17. config.setServiceName("%sService"); //去掉Service前面的I
  18. config.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
  19. generator.setGlobalConfig(config);
  20. //2、设置数据源
  21. DataSourceConfig dsc = new DataSourceConfig();
  22. dsc.setUrl("jdbc:mysql://localhost:3306/admin-springboot?serverTimezone=UTC");
  23. dsc.setDriverName("com.mysql.cj.jdbc.Driver");
  24. dsc.setUsername("root");
  25. dsc.setPassword("123456789");
  26. dsc.setDbType(DbType.MYSQL);
  27. generator.setDataSource(dsc);
  28. //3、包的配置
  29. PackageConfig pc = new PackageConfig();
  30. pc.setModuleName("edu"); //模块名
  31. // 包:com.lu.edu
  32. pc.setParent("com.lu");
  33. pc.setEntity("entity");
  34. pc.setMapper("mapper");
  35. pc.setService("service");
  36. pc.setController("controller");
  37. generator.setPackageInfo(pc);
  38. //4、策略配置
  39. StrategyConfig strategy = new StrategyConfig();
  40. strategy.setInclude("sys_user"); // 设置要映射的表名
  41. //数据库表 映射到实体的命名策略
  42. strategy.setNaming(NamingStrategy.underline_to_camel);
  43. //数据库表字段 映射到实体的命名策略
  44. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  45. //生成实体时去掉表前缀
  46. strategy.setTablePrefix(pc.getModuleName() + "_");
  47. strategy.setEntityLombokModel(true); // 自动lombok;
  48. strategy.setLogicDeleteFieldName("deleted"); //逻辑字段,数据库字段里面有deteted,然后自动配置
  49. // 自动填充配置
  50. TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
  51. TableFill gmtModified = new TableFill("gmt_modified",FieldFill.INSERT_UPDATE);
  52. ArrayList<TableFill> tableFills = new ArrayList<>();
  53. tableFills.add(gmtCreate);
  54. tableFills.add(gmtModified);
  55. strategy.setTableFillList(tableFills);
  56. // 乐观锁
  57. strategy.setVersionFieldName("version");
  58. strategy.setRestControllerStyle(true);
  59. strategy.setControllerMappingHyphenStyle(true); //下划线相当于/
  60. generator.setStrategy(strategy);
  61. generator.execute();
  62. }
  63. }

部分代码详解

定义生成的实体类中日期类型

  • 默认生成日期格式为 LocalDateTimeLocalDate

  • 自定义添加类型 添加设置 gc.setDateType(DateType.ONLY_DATE); 已实现 Date 类型.

  • 可添加注解来实现前后端日期格式的转换

    1. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")

主键策略

  • 默认雪花算法
  1. @TableId(type = IdType.INPUT) 自行输入id
  2. @TableId(type = IdType.AUTO) 主键自增
  3. @TableId(type = IdType.ASSIGN_ID) 雪花算法
  4. @TableId(type = IdType.ASSIGN_UUID)

乐观锁

  • 参考即可

(91条消息) mybatis plus的乐观锁使用总结_霉男纸的博客-CSDN博客_mybatisplus乐观锁

(91条消息) mybatis-plus的乐观锁_zhuzai233的博客-CSDN博客

自定义的模板

新的代码生成器

更多详解看官网:

https://baomidou.com/pages/779a6e/#使用


  1. import com.baomidou.mybatisplus.generator.FastAutoGenerator;
  2. import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
  3. import com.baomidou.mybatisplus.generator.config.OutputFile;
  4. import java.util.Collections;
  5. /**
  6. * @description:
  7. * @author: HaHa
  8. * @time: 2022/4/29 16:54
  9. */
  10. public class AutoFast {
  11. public static void main(String[] args) {
  12. generate();
  13. }
  14. private static void generate() {
  15. // UTC是根据原子钟来计算时间,而GMT是根据地球的自转和公转来计算时间
  16. String url="jdbc:mysql://localhost:3306/admin-springboot?serverTimezone=GMT%2b8";
  17. String username="root";
  18. String password="123456789";
  19. String outputDir = System.getProperty("user.dir");
  20. String parentPackage="com";
  21. DataSourceConfig.Builder database = new DataSourceConfig.Builder(url, username, password);
  22. FastAutoGenerator.create(database)
  23. .globalConfig(builder -> {
  24. builder.author("雨同我")
  25. .enableSwagger()
  26. .fileOverride()
  27. .outputDir(outputDir+"/src/main/java/");
  28. })
  29. .packageConfig(builder -> {
  30. builder.parent(parentPackage)
  31. .moduleName(null)
  32. .pathInfo(Collections.singletonMap(OutputFile.mapperXml, outputDir+"/src/main/resources/mapper/"));
  33. })
  34. .strategyConfig(builder -> {
  35. builder.entityBuilder().enableLombok();
  36. // builder.mapperBuilder().enableMapperAnnotation().build(); //这个是在每个mapper上面添加@Mapper
  37. builder.controllerBuilder().enableHyphenStyle() // 开启驼峰转连字符
  38. .enableRestStyle(); // 开启生成@RestController 控制器
  39. builder.addInclude("sys_user") // 设置需要生成的表名
  40. .addTablePrefix("t_", "sys_"); // 设置过滤表前缀
  41. })
  42. //.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
  43. .execute();
  44. }
  45. }

mybatis-plus详解的更多相关文章

  1. 一、Mybatis配置详解

    Mybatis配置详解 XML配置文件层次结构 下图展示了mybatis-config.xml的全部配置元素 properties元素 properties是一个配置属性的元素,让我们能在配置文件的上 ...

  2. ORM框架对比以及Mybatis配置文件详解

    ORM框架对比以及Mybatis配置文件详解 0.数据库操作框架的历程 (1) JDBC ​ JDBC(Java Data Base Connection,java数据库连接)是一种用于执行SQL语句 ...

  3. myBatis foreach详解【转】

    MyBatis的foreach语句详解 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,ope ...

  4. MyBatis Generator 详解

    MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...

  5. MyBatis Generator 详解 【转来纯为备忘】

    版权声明:版权归博主所有,转载请带上本文链接!联系方式:abel533@gmail.com   目录(?)[+] MyBatis Generator中文文档 运行MyBatis Generator X ...

  6. Mybatis配置详解

    一.SqlSession的使用范围说明  1.SQLSessionFactoryBuilder   通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory, ...

  7. MyBatis Generator 详解(转)

    MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...

  8. MyBatis Geneator详解<转>

    MyBatis Geneator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中文版的文档的 ...

  9. Mybatis全面详解——上(学习总结)

    原文地址:https://blog.csdn.net/ITITII/article/details/79969447 一.什么是Mybatis 这里借用官网的一句话介绍什么是mybatis:MyBat ...

  10. MyBatis Generator 详解 专题

    idea中有plugin可提高效率: http://www.henryxi.com/use-idea-mybatis-plugin-generate-mapper-files eg: <?xml ...

随机推荐

  1. Java 对象头那点事

    概览 对象头 存放:关于堆对象的布局.类型.GC状态.同步状态和标识哈希码的基本信息.Java对象和vm内部对象都有一个共同的对象头格式. (后面做详细介绍) 实例数据 存放:类的数据信息,父类的信息 ...

  2. 出现bash: ifconfig:command not found的解决办法,即安装ifconfig命令(亲测有效)

    初装centos 7时,运行config报 command not found 错误, ifconfig命令是设置或显示网络接口的程序,可以显示出我们机器的网卡信息,可是有些时候最小化安装CentOS ...

  3. 使用python脚本+zabbix前端监控云联网底层TCP数据流所负载的链路质量,并在丢包时联动保存MTR记录

    背景 目前国内各家云联网跨区域数据传输,会将数据流通过哈希运算负载到不同的底层链路上,而底层链路质量差异较大,这种情况导致的现象就是,使用传统的icmp监控线路正常,但是业务一直不稳定,所以才有了使用 ...

  4. Jenkins安装详解

    一.Jenkins是什么 Jenkins是一个独立的开源自动化服务器,可用于自动执行与构建,测试,交付或者部署软件相关的各种任务,是跨平台持续集成和持续交付应用程序,提高工作效率.使用Jenkins不 ...

  5. Mac-Typora快捷键

    标题(大钢) command+(1-6)) 如:command+1,设置为一级标题 引用 快捷键:command+option+Q 或者:先">",后面直接加内容 二级引用: ...

  6. 随笔总结:8086CPU的栈顶超界问题

    我们学习编程都知道栈的超界限问题是非常严重的问题,他可能会覆盖掉其他数据,并且我们不知道这个数据是我们自己保存的用于其他用途的数据还是系统的数据,这样常常容易引发一连串的问题. 在学习汇编的时候,我们 ...

  7. Hbase——API操作

    1.判断表是否存在 public static boolean isTableExit(String tableName) throws IOException { // //获取配置文件信息 // ...

  8. 手把手教你使用Git管理你的软件代码

    什么是分布式版本控制系统?Git有哪些常用命令?什么是仓库?Git的操作区域包括哪些?Git有哪些常用对象(object)?git rebase和git merge的区别是什么?git reset,g ...

  9. SQL语句中LEFT JOIN ON WHERE和LEFT JOIN ON AND的区别

    先上代码: declare @tb table (name varchar(10),sex int) declare @tb2 table (name varchar(10),age int) ins ...

  10. Pandas复杂查询、数据类型转换、数据排序

    Pandas高级操作 1.复杂查询 (1)逻辑运算 以DataFrame其中一列进行逻辑计算,会产生一个对应的bool值组成的Series 于是我们可以利用返回的bool列表进行一系列的数据查询 (2 ...