spring-bootmybatis-plus集成+代码⽣成器⾃定义controller模
maven依赖
spring boot 和 thymeleaf 结合 controller 返回的控制器路径不能以/ 开头
否则运⾏没问题,发布之后就不到⽂件
<mybatisplus-spring-boot-starter.version>1.0.4</mybatisplus-spring-boot-starter.version>
<mybatisplus.version>2.1.0</mybatisplus.version>
<!-- mybatis-plus begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>${mybatisplus-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<!-- 模板引擎 代码⽣成 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<!-- mybatis-plus end -->
myabtis配置类
#MyBatis
mybatis-plus:
mapper-locations: classpath:/mapper/*l
#实体扫描,多个package⽤逗号或者分号分隔
typeAliasesPackage: ity
global-config:
#主键类型  0:"数据库ID⾃增", 1:"⽤户输⼊ID",2:"全局唯⼀ID (数字类型唯⼀ID)", 3:"全局唯⼀ID UUID";    id-type: 0
#字段策略 0:"忽略判断",1:"⾮ NULL 判断"),2:"⾮空判断"
field-strategy: 2
#驼峰下划线转换
db-column-underline: true
#刷新mapper 调试神器
refresh-mapper: true
#数据库⼤写下划线转换
#capital-mode: true
#序列接⼝实现类配置
#key-generator: com.
#逻辑删除配置
#logic-delete-value: 0
#logic-not-delete-value: 1
#⾃定义填充策略接⼝实现
#meta-object-handler: com.baomidou.springboot.MyMetaObjectHandler
#⾃定义SQL注⼊器
#sql-injector: com.
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
#不加这个查询数据为空时,字段将被隐藏
call-setters-on-nulls: true
@EnableTransactionManagement
@Configuration
@MapperScan("com.dcy.mapper*")
public class MybatisPlusConfig {
/**
* mybatis-plus SQL执⾏效率插件【⽣产环境可以关闭】
*/
@Bean
@Profile({"dev","test"})// 设置 dev test 环境开启
public PerformanceInterceptor performanceInterceptor() {
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
/*<!-- SQL 执⾏性能分析,开发环境使⽤,线上不推荐。 maxTime 指的是 sql 最⼤执⾏时长 -->*/        //performanceInterceptor.setMaxTime(1000);
/*<!--SQL是否格式化 默认false-->*/
//performanceInterceptor.setFormat(true);
return performanceInterceptor;
}
public class MpGenerator {
/**
* <p>
* MySQL ⽣成演⽰
* </p>
*/
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
// 选择 freemarker 引擎,默认 Veloctiy
// mpg.setTemplateEngine(new FreemarkerTemplateEngine());
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir("E://test//");
gc.setFileOverride(true);
gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML ⼆级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(true);// XML columList
//gc.setKotlin(true);//是否⽣成 kotlin 代码
gc.setAuthor("董春⾬");
// ⾃定义⽂件命名,注意 %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.setTypeConvert(new MySqlTypeConvert(){
// ⾃定义数据库表字段类型转换【可选】
@Override
public DbColumnType processTypeConvert(String fieldType) {
System.out.println("转换类型:" + fieldType);
/
/ 注意!!processTypeConvert 存在默认类型转换,如果不是你要的效果请⾃定义返回、⾮如下直接返回。                return super.processTypeConvert(fieldType);
}
});
dsc.setDriverName("sql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/nxxsba?characterEncoding=utf8");
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[] { "app_certificate" }); // 需要⽣成的表
// strategy.setExclude(new String[]{"test"}); // 排除⽣成的表
// ⾃定义实体⽗类
// 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.setEntityBuilderModel(true);
mpg.setStrategy(strategy);
/
/ 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.dcy");
pc.setController("controller");
pc.setEntity("model");
mpg.setPackageInfo(pc);
// 注⼊⾃定义配置,可以在 VM 中使⽤ cfg.abc 【可⽆】  ${cfg.abc}
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("abc", Config().getGlobalConfig().getAuthor() + "-mp");
this.setMap(map);
}
};
// ⾃定义 xxListIndex.html ⽣成
List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
focList.add(new FileOutConfig("/templatesMybatis/list.html.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
// ⾃定义输⼊⽂件名称
return "E://test//html//" + EntityName() + "ListIndex.html";
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// ⾃定义  xxAdd.html ⽣成
focList.add(new FileOutConfig("/templatesMybatis/add.html.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
// ⾃定义输⼊⽂件名称
return "E://test//html//" + EntityName() + "Add.html";
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
//  ⾃定义 xxUpdate.html⽣成
focList.add(new FileOutConfig("/templatesMybatis/update.html.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
// ⾃定义输⼊⽂件名称
return "E://test//html//" + EntityName() + "Update.html";
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 关闭默认 xml ⽣成,调整⽣成 ⾄ 根⽬录
/*TemplateConfig tc = new TemplateConfig();
tc.setXml(null);
mpg.setTemplate(tc);*/
// ⾃定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下⾯内容修改,
// 放置⾃⼰项⽬的 src/main/resources/templates ⽬录下, 默认名称⼀下可以不配置,也可以⾃定义模板名称        TemplateConfig tc = new TemplateConfig();
tc.setController("/templatesMybatis/controller.java.vm");
tc.setService("/templatesMybatis/service.java.vm");
免费模板生成器tc.setServiceImpl("/templatesMybatis/serviceImpl.java.vm");
tc.setEntity("/templatesMybatis/entity.java.vm");
tc.setMapper("/templatesMybatis/mapper.java.vm");
tc.setXml("/l.vm");
// 如上任何⼀个模块如果设置 空 OR Null 将不⽣成该模块。
mpg.setTemplate(tc);
// 执⾏⽣成
// 打印注⼊设置【可⽆】
}
}
模板⽂件  ⾃定义controller模板⽂件  comtroller.java.vm
package ${package.Controller};