mark CodeGenerator
基础权限开发框架 BMS = Spring boot + Mybatis plus + Shiro
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
package com.xj.admin;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.util.HashMap;import java.util.Map;import java.util.Properties;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.DataSourceConfig;import com.baomidou.mybatisplus.generator.config.GlobalConfig;import com.baomidou.mybatisplus.generator.config.PackageConfig;import com.baomidou.mybatisplus.generator.config.StrategyConfig;import com.baomidou.mybatisplus.generator.config.TemplateConfig;import com.baomidou.mybatisplus.generator.config.rules.DbType;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;/*** <p>* 代码生成器演示* </p>*/public class CodeGenerator {public static void main(String[] args) {AutoGenerator mpg = new AutoGenerator();// 全局配置GlobalConfig gc = new GlobalConfig();gc.setOutputDir("D://");gc.setFileOverride(true);gc.setActiveRecord(true);gc.setEnableCache(true);// XML 二级缓存gc.setBaseResultMap(true);// XML ResultMapgc.setBaseColumnList(false);// XML columListgc.setAuthor("xj");// 自定义文件命名,注意 %s 会自动填充表实体属性!// gc.setMapperName("%sDao");// gc.setXmlName("%sDao");// gc.setServiceName("MP%sService");// gc.setServiceImplName("%sServiceDiy");// gc.setControllerName("%sAction");mpg.setGlobalConfig(gc);// 数据源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setDbType(DbType.MYSQL);dsc.setDriverName(readValue("spring.datasource.driver-class-name"));dsc.setUsername(readValue("spring.datasource.username"));dsc.setPassword(readValue("spring.datasource.password"));dsc.setUrl(readValue("spring.datasource.url"));mpg.setDataSource(dsc);// 策略配置StrategyConfig strategy = new StrategyConfig();//strategy.setTablePrefix("bmd_");// 此处可以修改为您的表前缀strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略strategy.setInclude(new String[] { "tb_dict" }); // 需要生成的表// strategy.setExclude(new String[]{"test"}); // 排除生成的表// 字段名生成策略strategy.setDbColumnUnderline(true);// 自定义实体父类// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");// 自定义实体,公共字段// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });// 自定义 mapper 父类// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");// 自定义 service 父类// strategy.setSuperServiceClass("com.baomidou.demo.TestService");// 自定义 service 实现类父类// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");// 自定义 controller 父类// strategy.setSuperControllerClass("com.baomidou.demo.TestController");// 【实体】是否生成字段常量(默认 false)// public static final String ID = "test_id";// strategy.setEntityColumnConstant(true);// 【实体】是否为构建者模型(默认 false)// public User setName(String name) {this.name = name; return this;}// strategy.setEntityBuliderModel(true);mpg.setStrategy(strategy);// 包配置PackageConfig pc = new PackageConfig();pc.setParent("com.xj.common.bussiness");pc.setModuleName("dict");mpg.setPackageInfo(pc);// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {Map<String, Object> map = new HashMap<String, Object>();map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");this.setMap(map);}};mpg.setCfg(cfg);// 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/template 下面内容修改,// 放置自己项目的 src/main/resources/template 目录下, 默认名称一下可以不配置,也可以自定义模板名称TemplateConfig tc = new TemplateConfig();tc.setController("/template/controller.vm");// tc.setEntity("...");// tc.setMapper("...");// tc.setXml("...");// tc.setService("...");// tc.setServiceImpl("...");mpg.setTemplate(tc);// 执行生成mpg.execute();// 打印注入设置System.err.println(mpg.getCfg().getMap().get("abc"));}//根据key读取valuepublic static String readValue(String key) {String filePath =CodeGenerator.class.getClassLoader().getResource("application.properties").getPath();Properties props = new Properties();try {InputStream in = new BufferedInputStream (new FileInputStream(filePath));props.load(in);String value = props.getProperty (key);System.out.println(key+value);return value;} catch (Exception e) {e.printStackTrace();return null;}}}