mybatis-generate配置
### mybatis-generate配置
#設(shè)置作者
author=zhangchq
#此處為本項目src所在路徑(代碼生成器輸出路徑)
OutputDir=
#login
OutputCommon=/frame-common
#provider
OutputProvider=/frame-provider
#java
OutputJava=/src/main/java
#resources
OutputResource=/src/main/resources
#mapper.xml的生成位置
OutputDirXml=
#自定義包路徑
package=/com/atlp/project
#包路徑配置
parentPath=com.atlp.project
#數(shù)據(jù)庫地址
url=jdbc:mysql://192.168.1.205:3306/frame?useUnicode=true&characterEncoding=utf-8&tinyInt1isBit=false
userName=root
password=root123
pom引入jar

MybatisGenerator自動生成
package com.atlp.generate;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
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 com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
/**
* @Author: zhangchq
* @CreateTime: 2019年03月08日 16時23分
* @Decription: MybatisGenerator自動生成實體對象
*/
public class MybatisGenerator {
? ? public static void main(String[] args) throws InterruptedException {
? ? ? ? // 項目路徑地址
? ? ? ? String projectAddress = System.getProperty("user.dir");
? ? ? ? // 需要生成實體的表
? ? ? ? String[] includeTables = new String[]{"atlp_b_com_auth", "atlp_b_com_dept", "atlp_b_com_emp", "atlp_b_com_menu", "atlp_b_com_role"};
? ? ? ? //用來獲取Mybatis-Plus.properties文件的配置信息
? ? ? ? final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus");
? ? ? ? // 代碼生成器
? ? ? ? AutoGenerator mpg = new AutoGenerator();
? ? ? ? // 全局配置
? ? ? ? GlobalConfig gc = new GlobalConfig();
? ? ? ? gc.setAuthor(rb.getString("author"));
? ? ? ? gc.setOpen(false);
? ? ? ? gc.setFileOverride(true);
? ? ? ? gc.setEntityName("%sEntity");
? ? ? ? gc.setMapperName("%sMapper");
? ? ? ? gc.setXmlName("%sMapper");
? ? ? ? gc.setIdType(IdType.AUTO);
? ? ? ? gc.setBaseResultMap(true);
? ? ? ? gc.setBaseColumnList(true);
? ? ? ? gc.setSwagger2(true);
? ? ? ? gc.setActiveRecord(true);
? ? ? ? mpg.setGlobalConfig(gc);
? ? ? ? // 數(shù)據(jù)源配置
? ? ? ? DataSourceConfig dsc = new DataSourceConfig();
? ? ? ? dsc.setDbType(DbType.MYSQL);
? ? ? ? dsc.setUrl(rb.getString("url"));
? ? ? ? dsc.setDriverName("com.mysql.jdbc.Driver");
? ? ? ? dsc.setUsername(rb.getString("userName"));
? ? ? ? dsc.setPassword(rb.getString("password"));
? ? ? ? mpg.setDataSource(dsc);
? ? ? ? // 包配置
? ? ? ? PackageConfig pc = new PackageConfig();
? ? ? ? pc.setParent(rb.getString("parentPath"));
? ? ? ? pc.setEntity("entity");
? ? ? ? pc.setMapper("mapper");
? ? ? ? mpg.setPackageInfo(pc);
? ? ? ? // 自定義配置
? ? ? ? InjectionConfig cfg = new InjectionConfig() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void initMap() {
? ? ? ? ? ? ? ? // to do nothing
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? List<FileOutConfig> focList = new ArrayList<>();
? ? ? ? // 自定義 entity.java 輸出目錄
? ? ? ? focList.add(new FileOutConfig("/templates/entity.java.ftl") {
? ? ? ? ? ? @Override
? ? ? ? ? ? public String outputFile(TableInfo tableInfo) {
? ? ? ? ? ? ? ? return projectAddress + rb.getString("OutputCommon") + rb.getString("OutputJava")
? ? ? ? ? ? ? ? ? ? ? ? + rb.getString("package") + "/entity/" + tableInfo.getEntityName() + StringPool.DOT_JAVA;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? // 自定義 mapper.java 輸出目錄
? ? ? ? focList.add(new FileOutConfig("/templates/mapper.java.ftl") {
? ? ? ? ? ? @Override
? ? ? ? ? ? public String outputFile(TableInfo tableInfo) {
? ? ? ? ? ? ? ? return projectAddress + rb.getString("OutputProvider") + rb.getString("OutputJava")
? ? ? ? ? ? ? ? ? ? ? ? + rb.getString("package") + "/mapper/" + tableInfo.getMapperName() + StringPool.DOT_JAVA;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? // 自定義 mapper.xml 輸出目錄
? ? ? ? focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
? ? ? ? ? ? @Override
? ? ? ? ? ? public String outputFile(TableInfo tableInfo) {
? ? ? ? ? ? ? ? // 自定義輸入文件名稱
? ? ? ? ? ? ? ? return projectAddress + rb.getString("OutputProvider") + rb.getString("OutputResource")
? ? ? ? ? ? ? ? ? ? ? ? + "/mapper/" + tableInfo.getEntityName() + StringPool.DOT_XML;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? cfg.setFileOutConfigList(focList);
? ? ? ? mpg.setCfg(cfg);
? ? ? ? mpg.setTemplate(new TemplateConfig().setXml(null));
? ? ? ? // 配置模板
? ? ? ? TemplateConfig templateConfig = new TemplateConfig();
? ? ? ? // 配置自定義輸出模板
? ? ? ? //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會根據(jù)使用的模板引擎自動識別
? ? ? ? templateConfig.setController(null);
? ? ? ? templateConfig.setServiceImpl(null);
? ? ? ? templateConfig.setService(null);
? ? ? ? templateConfig.setMapper(null);
? ? ? ? templateConfig.setXml(null);
? ? ? ? templateConfig.setEntity(null);
? ? ? ? mpg.setTemplate(templateConfig);
? ? ? ? // 策略配置
? ? ? ? StrategyConfig strategy = new StrategyConfig();
? ? ? ? strategy.setNaming(NamingStrategy.underline_to_camel);
? ? ? ? strategy.setColumnNaming(NamingStrategy.underline_to_camel);
? ? ? ? strategy.setEntityLombokModel(true);
? ? ? ? strategy.setSuperEntityClass("org.atlp.base.BaseEntity");
? ? ? ? strategy.setInclude(includeTables);
? ? ? ? mpg.setStrategy(strategy);
? ? ? ? mpg.setTemplateEngine(new FreemarkerTemplateEngine());
? ? ? ? mpg.execute();
? ? }
}