zoukankan      html  css  js  c++  java
  • MyBatis-plus 代码自动生成器

    MyBatis-plus  代码自动生成器

    1.添加pom文件依赖

      <!-- Mybatis-Plus  自动生成实体类-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus</artifactId>
                <version>2.0.6</version>
            </dependency>
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity</artifactId>
                <version>1.7</version>
            </dependency>

    2.新建CustomGenerator.java 文件

    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.config.*;
    import com.baomidou.mybatisplus.generator.config.rules.DbType;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    
    /**
     * Created by ehomeud on 2017/4/26.
     */
    
    public class CustomGenerator{
        public static void main(String[] args) throws InterruptedException {
            AutoGenerator mpg = new AutoGenerator();
    
            // 全局配置
            GlobalConfig gc = new GlobalConfig();
            gc.setOutputDir("E://");
            gc.setFileOverride(true);
            gc.setActiveRecord(true);
            gc.setEnableCache(true);// XML 二级缓存
            gc.setBaseResultMap(true);// XML ResultMap
            gc.setBaseColumnList(true);// XML columList
            gc.setAuthor("杨永生");
    
            // 自定义文件命名,注意 %s 会自动填充表实体属性!
             gc.setMapperName("%sDao");
             gc.setXmlName("%sMapper");
             gc.setServiceName("%sService");
             gc.setServiceImplName("%sServiceImap");
             gc.setControllerName("%sController");
            mpg.setGlobalConfig(gc);
    
            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setDbType(DbType.MYSQL);
            /*dsc.setTypeConvert(new MySqlTypeConvert(){
                // 自定义数据库表字段类型转换【可选】
                @Override
                public DbColumnType processTypeConvert(String fieldType) {
                    System.out.println("转换类型:" + fieldType);
                    return super.processTypeConvert(fieldType);
                }
            });*/
    
            dsc.setDriverName("com.mysql.jdbc.Driver");
            dsc.setUrl("jdbc:mysql://localhost:3306/hiione?useUnicode=true&amp;characterEncoding=UTF-8&amp;generateSimpleParameterMetadata=true");
            dsc.setUsername("yys");
            dsc.setPassword("123456");
            mpg.setDataSource(dsc);
    
            // 策略配置
            StrategyConfig strategy = new StrategyConfig();
            // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
            // strategy.setTablePrefix(new String[] { "tlog_", "tsys_" });// 此处可以修改为您的表前缀
            strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
            // strategy.setInclude(new String[] { "user" }); // 需要生成的表
            // strategy.setExclude(new String[]{"test"}); // 排除生成的表
            mpg.setStrategy(strategy);
    
            // 包配置
            PackageConfig pc = new PackageConfig();
            pc.setParent("com.yys");
            pc.setModuleName("test");
            mpg.setPackageInfo(pc);
    
            // 执行生成
            mpg.execute();
        }
    
    }

    3.直接运行CustomGenerator.java 文件就能得到我们想要的MyBatis所需要的文件

     补充: 

    链接那个地方有可能报错, 相应位置替换成下面代码就行了

     dsc.setDriverName("com.mysql.cj.jdbc.Driver");
     dsc.setUrl("jdbc:mysql://localhost:3306/umbrella?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT");
           

    版权声明: 本文有 ```...裥簞點 发表于 bloghome博客

    转载声明: 可自由转载、引用,但需要属名作者且注明文章出处。

    文章链接: https://www.bloghome.com.cn/user/yysblog

    官网地址:http://mp.baomidou.com

    精品原创,谢谢打赏

  • 相关阅读:
    CentOS7-samba文件共享服务
    centos7-vsftpd文件服务器
    linux用户和权限管理
    linux程序安装及包管理
    linux文件查找-find命令
    linux文本编辑器-VIM基本使用方法
    linux文本处理工具及正则表达式
    linux目录结构及文件管理
    linux基本命令及使用方法
    巴什博奕----取完者负的分析
  • 原文地址:https://www.cnblogs.com/yysbolg/p/6768194.html
Copyright © 2011-2022 走看看