美文网首页
Mybatis自动生成实体对象MybatisGenerator

Mybatis自动生成实体对象MybatisGenerator

作者: 彩笺w尺素 | 来源:发表于2020-04-16 15:44 被阅读0次

    mybatis-generate配置

    ### mybatis-generate配置

    #设置作者

    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

    #数据库地址

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

            // 数据源配置

            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, 会根据使用的模板引擎自动识别

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

        }

    }

    相关文章

      网友评论

          本文标题:Mybatis自动生成实体对象MybatisGenerator

          本文链接:https://www.haomeiwen.com/subject/gzarvhtx.html