1. 导入相应的jar包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--模板引擎 还有相应的mysql包之类的-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
2. 创建mybatis-plus的配置类
@EnableTransactionManagement
@Configuration
@MapperScan("cn.itsource.hrm.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
3. 配置yml文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1010/eureka/
instance:
prefer-ip-address: true
instance-id: xxx-user-service
server:
port: 2010
spring:
application:
name: xxx-user-service
datasource:
username: ******
password: ******
url: jdbc:mysql:///****
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
mapper-locations: classpath:cn/itsource/hrm/mapper/*Mapper.xml
4.创建配置文件 在resources中mybatiesplus-config.properties
OutputDir=D:/JavaEEIdeaWorkspace/***/***-systemmanage-parent/***-systemmanage-service-2010/src/main/java
OutputDirXml=D:/JavaEEIdeaWorkspace/***-parent/***-systemmanage-parent/***-systemmanage-service-2010/src/main/resources
OutputDirBase=D:/JavaEEIdeaWorkspace/***-parent/***-systemmanage-parent/***-systemmanage-common/src/main/java
author=user
parent=cn.itsource.hrm
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///***-systemmanage
jdbc.user=****
jdbc.pwd=****
5. 创建一个代码生成模板的主类
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.*;
public class GenteratorCode {
public static void main(String[] args) throws InterruptedException {
ResourceBundle rb = ResourceBundle.getBundle("mybatiesplus-config");
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(rb.getString("OutputDir"));
gc.setFileOverride(true);
gc.setActiveRecord(true);
gc.setEnableCache(false);
gc.setBaseResultMap(true);
gc.setBaseColumnList(false);
gc.setAuthor(rb.getString("author"));
mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setTypeConvert(new MySqlTypeConvert());
dsc.setDriverName(rb.getString("jdbc.driver"));
dsc.setUsername(rb.getString("jdbc.user"));
dsc.setPassword(rb.getString("jdbc.pwd"));
dsc.setUrl(rb.getString("jdbc.url"));
mpg.setDataSource(dsc);
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix(new String[] { "t_" });
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setInclude(new String[]{"***,"***"});
mpg.setStrategy(strategy);
PackageConfig pc = new PackageConfig();
pc.setParent(rb.getString("parent"));
pc.setController("web.controller");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setEntity("domain");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
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);
}
};
List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
focList.add(new FileOutConfig("/templates/controller.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return rb.getString("OutputDir")+ "/cn/itsource/hrm/web/controller/" + tableInfo.getEntityName() + "Controller.java";
}
});
focList.add(new FileOutConfig("/templates/query.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return rb.getString("OutputDirBase")+ "/cn/itsource/hrm/query/" + tableInfo.getEntityName() + "Query.java";
}
});
focList.add(new FileOutConfig("/templates/entity.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return rb.getString("OutputDirBase")+ "/cn/itsource/hrm/domain/" + tableInfo.getEntityName() + ".java";
}
});
focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return rb.getString("OutputDirXml")+ "/cn/itsource/hrm/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
TemplateConfig tc = new TemplateConfig();
tc.setService("/templates/service.java.vm");
tc.setServiceImpl("/templates/serviceImpl.java.vm");
tc.setMapper("/templates/mapper.java.vm");
tc.setEntity(null);
tc.setController(null);
tc.setXml(null);
mpg.setTemplate(tc);
mpg.execute();
}
}