在MyBatis Plus的代码自动生成器基础上做了一定的修改和优化,配置更加清晰、易懂
相关jar包引入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis.plus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatis.plus.version}</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>${velocity.version}</version> </dependency>
|
MP默认使用的是velocity模板引擎,所以还是要引入这个包的,如果是使用其他的模板引擎,如FreeMarker,则可以引入FreeMarker的包
引入了mybatis-plus-boot-starter和mybatis-plus的包后,不要再引入mybatis-spring-boot-starter相关的包,以免引起jar包冲突
配置数据库连接信息
在配置文件中配置数据库连接的信息,代码生成器中直接读取配置文件的信息,不需要重复配置
具体实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| @Component public class PostgreSQLGenerator implements EnvironmentAware {
@Autowired private Environment env;
private String driverClassName; private String username; private String password; private String url;
public PostgreSQLGenerator() {
}
private void init() { this.driverClassName = env.getProperty("spring.datasource.driverClassName"); this.username = env.getProperty("spring.datasource.username"); this.password = env.getProperty("spring.datasource.password"); this.url = env.getProperty("spring.datasource.url"); }
public void generator(String[] tables, Integer result) { AutoGenerator generator = new AutoGenerator(); generator.setGlobalConfig(globalConfig()); generator.setDataSource(dataSourceConfig()); generator.setStrategy(strategyConfig(tables)); generator.setPackageInfo(packageConfig());
generator.execute(); }
private GlobalConfig globalConfig() { GlobalConfig gc = new GlobalConfig(); gc.setOutputDir("D://mybatis//"); gc.setFileOverride(true); gc.setActiveRecord(true); gc.setEnableCache(false); gc.setBaseResultMap(true); gc.setBaseColumnList(false); gc.setAuthor("wangxinglei"); return gc; }
private TemplateConfig templateConfig() { return null; }
private DataSourceConfig dataSourceConfig() { init();
DataSourceConfig dsc = new DataSourceConfig(); dsc.setSchemaname("public"); dsc.setDbType(DbType.POSTGRE_SQL); dsc.setTypeConvert(new PostgreSqlTypeConvert()); dsc.setDbQuery(new PostgreSqlQuery()); dsc.setDriverName(driverClassName); dsc.setUsername(username); dsc.setPassword(password); dsc.setUrl(url); return dsc; }
private StrategyConfig strategyConfig(String[] tables) { StrategyConfig strategy = new StrategyConfig(); strategy.setTablePrefix(new String[]{"tbl_"}); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setInclude(tables);
strategy.setSuperMapperClass("com.baomidou.mybatisplus.mapper.BaseMapper"); strategy.setSuperServiceClass("com.baomidou.mybatisplus.service.IService"); strategy.setSuperServiceImplClass("com.baomidou.springwind.service.impl.ServiceImpl"); return strategy; }
private PackageConfig packageConfig() { PackageConfig pc = new PackageConfig(); pc.setParent("com.lemon.rabbit"); pc.setController("controller"); pc.setEntity("model"); pc.setMapper("mapper"); pc.setXml("mapping"); pc.setService("service"); pc.setServiceImpl("service.impl"); return pc; }
private InjectionConfig injectionConfig(Integer result) { InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { Map<String, Object> map = new HashMap<>(); map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); this.setMap(map); } }; List<FileOutConfig> focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/dto.java" + ((1 == result) ? ".ftl" : ".vm")) { @Override public String outputFile(TableInfo tableInfo) { return "D://test/my_" + tableInfo.getEntityName() + ".java"; } }); cfg.setFileOutConfigList(focList); return cfg; }
@Override public void setEnvironment(Environment environment) { this.env = environment; } }
|
使用
在单元测试中直接调用postgreSQLGenerator的generator方法即可,第一个参数填入对应的表名,可以写一个,也可以写多个,然后执行用例即可生成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @RunWith(SpringRunner.class) @SpringBootTest public class DatabaseGeneratorTest {
@Autowired private PostgreSQLGenerator postgreSQLGenerator;
@Before public void setup() {
}
@Test public void contextLoads() throws Exception{ String[] tables = new String[]{"tbl_city"}; Integer result = 1; postgreSQLGenerator.generator(tables, 1); }
}
|