传送门:mybatis-plus官网
进入官网后找到“代码生成器(新)”。把里边的代码copy一下,在下边有个配置 ,可以自定义相关的配置。
传送门:代码生成器配置新
首先导入相关依赖
<!--代码生成器所需要的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
然后你可以自定义生成的模板,比如controller的。
模板的代码
package ${package.Controller};
//自定义要导入的包
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.zkw.common.Result;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
#if(${restControllerStyle})
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
/**
* <p>
* $!{table.comment} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end
//导入的代码模板
@Resource
private ${table.serviceName} ${table.entityPath}Service;
/**
* 新增或更新
*/
@PostMapping
public Result save(@RequestBody ${entity} ${table.entityPath}) {
${table.entityPath}Service.saveOrUpdate(${table.entityPath});
return Result.success();
}
/**
* 删除
*/
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id) {
${table.entityPath}Service.removeById(id);
return Result.success();
}
/**
* 批量删除
*/
@PostMapping("/del/batch")
public Result delBatch(@RequestBody List<Integer> ids){
return Result.success(${table.entityPath}Service.removeByIds(ids));
}
/**
* 查询所有用户
*/
@GetMapping("/all")
public Result findAll() {
return Result.success(${table.entityPath}Service.list());
}
/**
* 根据id查询用户
*/
@GetMapping("/{id}")
public Result findOne(@PathVariable Integer id) {
return Result.success(${table.entityPath}Service.getById(id));
}
/**
* 分页查询
*/
@GetMapping("/page")
public Result findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize) {
QueryWrapper<${entity}> queryWrapper = new QueryWrapper<${entity}>();
queryWrapper.orderByDesc("id");
return Result.success(${table.entityPath}Service.page(new Page<>(pageNum, pageSize),queryWrapper));
}
}
#end
配置代码,然后运行这个就可以了,就能直接生成controlle,service,serviceImpl,mapper,mapper.xml,entity
package com.zkw.utils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import java.sql.Types;
import java.util.Collections;
/**
* 代码生成器
*/
public class CodeGenerator {
public static void main(String[] args) {
generator();
}
private static void generator(){
FastAutoGenerator.create("jdbc:mysql://localhost:3306/customer?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "root", "123456")
.globalConfig(builder -> {
builder.author("zkw") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("D:\\ZkwJavaStudy\\SpringBootProject\\manage\\src\\main\\java\\"); // 指定输出目录
})
.dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.SMALLINT) {
// 自定义类型转换
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
}))
.packageConfig(builder -> {
builder.parent("com.zkw") // 设置父包名
.moduleName(null) // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\ZkwJavaStudy\\SpringBootProject\\manage\\src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("sys_course") // 设置需要生成的表名,每次只需要改这个表就可以了
.addTablePrefix("sys_", "c_")// 设置过滤表前缀
.entityBuilder().enableLombok() //添加lombok 注解
.controllerBuilder().enableHyphenStyle().enableRestStyle(); //开启controller的驼峰 并 添加RestController
})
// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}