首页 > 其他分享 >Mybatis-Plus代码生成器

Mybatis-Plus代码生成器

时间:2023-08-18 11:13:36浏览次数:29  
标签:代码生成 mybatisplus baomidou strategy Plus Mybatis import com gc

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、 Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。 测试:

package com.aiit.mybatisplusdemo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
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 Global {
   public static void main(String[] args) {
// 需要构建一个 代码自动生成器 对象
       AutoGenerator mpg = new AutoGenerator();
// 配置策略
// 1、全局配置
       GlobalConfig gc = new GlobalConfig();
       String projectPath = System.getProperty("user.dir");
       gc.setOutputDir(projectPath + "/src/main/java");
       gc.setAuthor("TJL");//作者
       gc.setOpen(false);
       gc.setFileOverride(false); // 是否覆盖
       gc.setServiceName("%sService"); // 去Service的I前缀
       gc.setIdType(IdType.ID_WORKER);
       gc.setDateType(DateType.ONLY_DATE);
       gc.setSwagger2(true);
       mpg.setGlobalConfig(gc);
//2、设置数据源
       DataSourceConfig dsc = new DataSourceConfig();
       dsc.setUrl("jdbc:mysql://localhost:3306/springbootdemo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");//数据库源
       dsc.setDriverName("com.mysql.cj.jdbc.Driver");
       dsc.setUsername("root");//账号
       dsc.setPassword("root");//密码
       dsc.setDbType(DbType.MYSQL);  //数据库类型
       mpg.setDataSource(dsc);
//3、包的配置
       PackageConfig pc = new PackageConfig();
       pc.setModuleName("blog"); //模块名
       pc.setParent("com.aiit.mybatisplusdemo"); //java下的包名
       pc.setEntity("entity");
       pc.setMapper("mapper");
       pc.setService("service");
       pc.setController("controller");
       mpg.setPackageInfo(pc);
//4、策略配置
       StrategyConfig strategy = new StrategyConfig();
       strategy.setInclude("user"); // 设置要映射的表名
       strategy.setNaming(NamingStrategy.underline_to_camel);
       strategy.setColumnNaming(NamingStrategy.underline_to_camel);
       strategy.setEntityLombokModel(true); // 自动lombok;
       strategy.setLogicDeleteFieldName("deleted");
// 自动填充配置
       TableFill gmtCreate = new TableFill("create_time",FieldFill.INSERT);
       TableFill gmtModified = new TableFill("update_time",FieldFill.INSERT_UPDATE);
       ArrayList<TableFill> tableFills = new ArrayList<>();
       tableFills.add(gmtCreate);
       tableFills.add(gmtModified);
       strategy.setTableFillList(tableFills);
// 乐观锁
       strategy.setVersionFieldName("version");
       strategy.setRestControllerStyle(true);
       strategy.setControllerMappingHyphenStyle(true);
       mpg.setStrategy(strategy);
       mpg.execute(); //执行
  }
}
 

标签:代码生成,mybatisplus,baomidou,strategy,Plus,Mybatis,import,com,gc
From: https://www.cnblogs.com/ShaPii/p/17639859.html

相关文章

  • MyBatis Mapper映射处理CLOB和BLOB类型
    ​Mybatis的MapperXML映射文件应该处理数据库字段类型为CLOB和BLOB类型的数据呢?首先我们先看下CLOB和BLOB这两种数据类型的介绍。介绍使用Mybatis时涉及到两种特殊类型的处理,分别是Blob(BinaryLargeObject)和Clob(CharacterLargeObject)。Blob表示二进制大对象字段,而Clob则表示......
  • 学习ruoyi-flowable-plus
    20230817,学习1小时30分钟1.后端启动后没有生成工作流相关表,例如ACT_表,导致启动失败(flowable初始化建表失败问题):原因:需要增加配置database-schema:scottflowable:#关闭定时任务jobasync-executor-activate:false#库与数据库表结构不一致时,会自动将数据库表结构......
  • 关于Mybatis 和 Hibernate 持久层框架之间的区别
    首先,Mybatis和Hibernate都是ORM持久层框架不同点在于,MyBatis是半自动的,它需要开发人员自己手动编写SQL语句。一、MybatisMyBatis支持通过XML或注解的方式来配置需要运行的SQL语句,并且,最终由框架本身将Java对象和SQL语句映射生成最终执行的SQL,执行后,再将结果映射......
  • mybatis-plus+nacos配置中心和服务发现保姆级教程
    默认你已经看了我的Mybatis-Plus+Mysql的教程,现在有了一个简单的项目如下(之前的教程: https://www.cnblogs.com/leafstar/p/17638741.html)1.下载nacao,我这里下的是2.1.0版本提供一下我用的版本(链接:https://pan.baidu.com/s/1AVmZIhx4b0euzctJhWUlTQ?pwd=1234提取码:1234......
  • SpringBoot+Mybatis-Plus+Mysql的保姆级搭建
    本文通过简单的示例代码和说明,让读者能够了解Mybatis-Plus+Mysql的简单使用必须说明的是,本文有部分内容是为了后续的微服务写的,所以如果只想用Mybatis-Plus的话,直接使用bank1项目即可 1.新建父项目,选用springinitializr即可,可以删除其他文件,仅仅留下pom文件当成父pom。修改......
  • MyBatis resultMap中collection过滤空字段
    在使用MyBatis查询数据时,返回值可以定义为resultMap。如果返回的对象中有列表,还可以使用collection标签进行定义。此时,如果不想某些字段为空的数据加入列表,可以使用notNullColumn属性进行定义:<resultMapid="resultMapDemo"type="返回值类型"><idproperty="id"column=......
  • mybatis自定义拦截器@Intercepts
    mybatis:自定义实现拦截器插件Interceptor-知乎(zhihu.com) 11.插件机制Interceptor|一灰灰Learning(hhui.top)......
  • IDEA社区版+SpringBoot+MyBatisPLus+MySQL实现数据库的保存、查询、修改操作
    一、概述使用IDEA社区+SpringBoot+MyBatisPlus+MySQL实现数据的保存修改与查询。主要记录一下踩坑过程。注意事项:1.社区版IDEA并不能直接创建SpringBoot工程,所以我采用的方式是在Spring官网上,让其帮助我创建一个,创建好后,直接下载。//参考案例https://blog.csd......
  • 谈谈你对Mybatis的认识/了解?
    概念:Mybatis是一个开源免费轻量级,基于Java语言、半自动ORM、持久层的框架作用:封装了JDBC,通过SOL语句建立实体类和关系表的映射关系,让我们操作数据库更加简单、方便、高效使用步骤:1)导包导入Mybatis相关依赖包 2)配置数据库连接yml3)代码实体类OMapper接......
  • mybatis 参数赋值及类型解析
    基本类型处理器configuration对象初始化的时候会创建TypeHandlerRegistry,构造方法里指定了默认类型处理。基本类型常见的数据库类型都又对应的解析器。TypeHandlerRegistry类typeHandlerMap属性存储了javaType和类型TypeHandler之间的映射关系。这里的mapkey值就是javaType对应......