CodeGenerate.java代码
package com.example.springbootweb.utils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import java.util.Collections;
public class CodeGenerator {
public static void main(String[] args) {
generate();
}
private static void generate() {
FastAutoGenerator.create("数据库地址", "account", "pwd")
.globalConfig(builder -> {
builder.author("SisyphusTang")
.enableSwagger()
.fileOverride()
.outputDir("项目绝对地址\\src\\main\\java\\");
})
.packageConfig(builder -> {
builder.parent("com.example.springbootweb")
.moduleName(null)
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "项目绝对地址\\src\\main\\resources\\mapper\\"));
})
.strategyConfig(builder -> {
builder.entityBuilder().enableLombok();
builder.controllerBuilder().enableHyphenStyle()
.enableRestStyle();
builder.addInclude("user")
.addTablePrefix("t_", "sys_");
})
.execute();
}
}
对应的controller类模板
package ${package.Controller};
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ${package.Parent}.common.config.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
#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 boolean save(@RequestBody ${entity} ${table.entityPath}) {
## return ${table.entityPath}Service.saveOrUpdate(${table.entityPath});
## }
##
@PostMapping
public Result<?> save(@RequestBody ${entity} ${table.entityPath}) {
${table.entityPath}Service.save(${table.entityPath});
return Result.success();
}
@PutMapping
public Result<?> update(@RequestBody ${entity} ${table.entityPath}) {
${table.entityPath}Service.updateById(${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 boolean deleteBatch(@RequestBody List<Integer> ids) {
return ${table.entityPath}Service.removeByIds(ids);
}
@GetMapping
public List<${entity}> findAll() {
return ${table.entityPath}Service.list();
}
@GetMapping("/{id}")
public ${entity} findOne(@PathVariable Integer id) {
return ${table.entityPath}Service.getById(id);
}
@GetMapping("/page")
public Page<${entity}> findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("id");
return ${table.entityPath}Service.page(new Page<>(pageNum, pageSize), queryWrapper);
}
}
#end