MyBatis Plus Generator
<!-- pom --><dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.0</version></dependency><dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version></dependency>
import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.core.toolkit.StringPool;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.*;import com.baomidou.mybatisplus.generator.config.po.TableInfo;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import java.util.ArrayList;import java.util.List;/** * @author fengyuhao * @Description */public class MpGenerator { final static String dirPath = System.getProperty("user.dir") + "/src/main/java"; private static String[] tables = new String[]{"userdemo"}; // 配置生成指定的表 private static DbType dbType = DbType.POSTGRE_SQL; private static String url = "jdbc:postgresql://localhost:5432/mybatisplus?useUnicode=true&" + "characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&" + "transformedBitIsBoolean=true&serverTimezone=GMT%2B8"; private static String UserName = "postgres"; private static String PassWord = "953598751"; private static String DriverName = "org.postgresql.Driver";// private static DbType dbType = DbType.MYSQL;// private static String UserName = "root";// private static String PassWord = "123456";// private static String DriverName = "com.mysql.cj.jdbc.Driver";// private static String url ="jdbc:mysql://localhost:3306/map3d?characterEncoding=utf8&serverTimezone=UTC"; public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(dirPath); gc.setAuthor("fyh"); gc.setOpen(false); // 不打开文件夹 gc.setFileOverride(true); //是否覆盖 gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false gc.setEnableCache(false);// XML 二级缓存 gc.setBaseResultMap(true);// XML ResultMap gc.setBaseColumnList(true);// XML columList // 自定义文件命名,注意 %s 会自动填充表实体属性! gc.setMapperName("%sDao");// gc.setXmlName("%sMapper"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setControllerName("%sController"); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(dbType); dsc.setDriverName(DriverName); dsc.setUsername(UserName);//?serverTimezone=GMT%2B8 dsc.setPassword(PassWord); dsc.setUrl(url); mpg.setDataSource(dsc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意 //strategy.setTablePrefix(new String[] { "tb_", "tsys_" });// 此处可以修改表前缀 strategy.setEntityLombokModel(true);// 自动添加 lombok 注解 strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setInclude(tables); // 需要生成的表 // strategy.setExclude(new String[]{"test"}); // 排除生成的表 // 自定义实体父类 // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity"); // 自定义实体,公共字段 // strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); // 自定义 mapper 父类 // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper"); // 自定义 service 父类 strategy.setSuperServiceClass("com.baomidou.mybatisplus.extension.service.IService"); // 自定义 service 实现类父类 strategy.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl"); // 自定义 controller 父类 // strategy.setSuperControllerClass("com.baomidou.demo.TestController"); // 【实体】是否生成字段常量(默认 false) // public static final String ID = "test_id"; // strategy.setEntityColumnConstant(true); // 【实体】是否为构建者模型(默认 false) // public User setName(String name) {this.name = name; return this;}// strategy.setEntityBuilderModel(true); mpg.setStrategy(strategy); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 自定义输出配置 List<FileOutConfig> focList = new ArrayList<>(); // 自定义配置优先于默认配置生效 String mapperXmlPath = "/templates/mapper.xml.vm"; focList.add(new FileOutConfig(mapperXmlPath) { @Override public String outputFile(TableInfo tableInfo) { // 自定义xml 文件名和生成路径 return System.getProperty("user.dir") + "/src/main/resources/mapper/" + tableInfo.getEntityName()+"Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.cug.seckill")// .setModuleName("module") .setController("controller") .setEntity("entity") .setMapper("mapper") .setService("service") .setServiceImpl("service.impl");// pc.setXml("mapper"); mpg.setPackageInfo(pc); // 执行生成 mpg.execute(); }}