Mybatis自動生成實體對象MybatisGenerator

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

mybatis-plus-generator

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();

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容