mysql逆向工程(mybatis plus)

1.添加依赖
<!--mybatisplus,加入该jar后,就不再需要mybatis-xxx.jar,mybatis-spring-xxx.jar了,它们会以依赖包的形式被自动维护-->
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>${mybatisplus.version}</version>
</dependency>

<!--模板引擎-->
<dependency>
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity-engine-core</artifactId>
  <version>${velocity.version}</version>
</dependency>


2.添加配置文件
/resources/mybatis-plus.properties

#此处为本项目src所在路径(代码生成器输出路径),注意一定是当前项目所在的目录哟
OutputDir=D:/project/supplychain_api/src/main/java
#mapper.xml SQL映射文件目录
OutputDirXml=D:/project/supplychain_api/src/test/resources
#设置作者
author=smj
#自定义包路径
parent=com.zt

#数据库连接信息
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://47.96.22.250:33066/ybt_saas?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
mysql.user=dbsql
mysql.pwd=$027@mydbsql

 

3.添加测试类
/generator/GenteratorCode.java

public class GenteratorCode {

  public static void main(String[] args) throws InterruptedException {
    //用来获取mybatis-plus.properties文件的配置信息
    final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus");
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    gc.setOutputDir(rb.getString("OutputDir"));
    gc.setFileOverride(true);
    gc.setActiveRecord(true);
    gc.setEnableCache(false);// XML 二级缓存
    gc.setBaseResultMap(true);// XML ResultMap
    gc.setBaseColumnList(true);// XML columList
    gc.setAuthor(rb.getString("author"));
    // 自定义文件命名,注意 %s 会自动填充表实体属性!
    gc.setMapperName("%sMapper");
    gc.setXmlName("%sMapper");
    gc.setServiceName("I%sService");
    gc.setServiceImplName("%sServiceImpl");
    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(rb.getString("mysql.driver"));
    dsc.setUrl(rb.getString("mysql.url"));
    dsc.setUsername(rb.getString("mysql.user"));
    dsc.setPassword(rb.getString("mysql.pwd"));
    mpg.setDataSource(dsc);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
    //strategy.setTablePrefix(new String[] { "SYS_" });// 此处可以修改为您的表前缀
    strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
    strategy.setInclude(new String[] {"jf_order"}); // 需要生成的表
    //strategy.setExclude(new String[]{"test"}); // 排除生成的表
    mpg.setStrategy(strategy);

    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent(rb.getString("parent"));
    // pc.setModuleName("tbldept");//模块名称,单独生成模块时使用!!!!!!!!!!!
    pc.setController("controller");
    pc.setService("service");
    pc.setServiceImpl("service.impl");
    pc.setEntity("entity");
    pc.setMapper("mapper");
    mpg.setPackageInfo(pc);

    // 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
    InjectionConfig cfg = new InjectionConfig() {
      @Override
      public void initMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb");
        this.setMap(map);
      }
    };

    // 调整 xml 生成目录演示
    List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
    focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
      @Override
      public String outputFile(TableInfo tableInfo) {
        return rb.getString("OutputDirXml")+ "/mybatis/mappers/" + tableInfo.getEntityName() + "Mapper.xml";
      }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);

    // 关闭默认 xml 生成,调整生成 至 根目录
    TemplateConfig tc = new TemplateConfig();
    tc.setXml(null);
    mpg.setTemplate(tc);

    // 执行生成
    mpg.execute();
  }
}

 

3.运行测试类

 

posted @ 2019-10-31 15:57  smj1990  阅读(504)  评论(0编辑  收藏  举报