第一步 安装依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
第二步 编写启动文件
public class CodeGenerator {
public static void main(String[] args) {
generate();
}
private static void generate(){
FastAutoGenerator.create("jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2b8", "root", "password")
.globalConfig(builder -> builder
.author("quan")
.outputDir(Paths.get(System.getProperty("user.dir")) + "\\src\\main\\java\\")
.commentDate("yyyy-MM-dd")
)
.packageConfig(builder -> builder
.parent("com.example.springboot_vue")
.moduleName(null)
.pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\vueProjectSystem\\springboot_vue\\src\\main\\resources\\mapper\\"))
)
.strategyConfig(builder ->{
builder.mapperBuilder().enableFileOverride();
builder.serviceBuilder().enableFileOverride();
builder.controllerBuilder().enableFileOverride();
builder.entityBuilder().enableFileOverride().enableLombok();
builder.mapperBuilder().enableMapperAnnotation().build();
builder.controllerBuilder().enableHyphenStyle()
.enableRestStyle();
builder.addInclude("sys_user")
.addTablePrefix("t_", "c_","sys");
}
)
.execute();
}
}
第三步 编写controller模板,自动生成controller代码(文件名为controller.java.vm)
package ${package.Controller};
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Entity}.ErrorCode;
import ${package.Entity}.Pages;
import ${package.Entity}.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import $!{package.Service}.$!{table.serviceName};
import ${package.Entity}.${entity};
#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
@Autowired
private ${table.serviceName} ${table.entityPath}Service;
@GetMapping("/search")
public Result search(@RequestParam Integer currentPage,@RequestParam Integer pageSize,@RequestParam String username,@RequestParam String email,@RequestParam String address){
QueryWrapper<${entity}> queryWrapper=new QueryWrapper<>();
if(!username.isBlank()){
queryWrapper.like("username",username);
}
if(!email.isBlank()){
queryWrapper.like("email",email);
}
if(!address.isBlank()){
queryWrapper.like("address",address);
}
IPage<${entity}> page = new Page<>(currentPage, pageSize);
IPage<${entity}> ${table.entityPath}Page=${table.entityPath}Service.page(page,queryWrapper);
List<${entity}> ${table.entityPath}List = ${table.entityPath}Page.getRecords();
long total = ${table.entityPath}Page.getTotal();
Pages pages=new Pages(total,${table.entityPath}List);
return Result.success(pages);
}
@PostMapping("/saveOrUpdate")
public Result saveOrUpdate(@RequestBody ${entity} ${table.entityPath}){
Boolean code=${table.entityPath}Service.saveOrUpdate(${table.entityPath});
if(code==true){
return Result.success();
}else{
return Result.error(ErrorCode.INSERT_ERROR.getCode(), ErrorCode.INSERT_ERROR.getMsg());
}
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id){
QueryWrapper<${entity}> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("id",id);
Boolean code=${table.entityPath}Service.remove(queryWrapper);
if(code==true){
return Result.success();
}else{
return Result.error(ErrorCode.REMOVE_ERROR.getCode(), ErrorCode.REMOVE_ERROR.getMsg());
}
}
@DeleteMapping("/ids/{ids}")
public Result deleteByIds(@PathVariable Integer[] ids){
List<Integer> list=new ArrayList<>();
for(Integer id:ids){
list.add(id);
}
Boolean code=${table.entityPath}Service.removeBatchByIds(list);
if(code==true){
return Result.success();
}else{
return Result.error(ErrorCode.PATCH_REMOVE_ERROR.getCode(), ErrorCode.PATCH_REMOVE_ERROR.getMsg());
}
}
}
#end
附:
1.Result类
package com.example.springboot_vue.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Result {
private int code;
private String msg;
private Object data;
public static Result success(){
return new Result(200,"success",null);
}
public static Result success(Object data){
return new Result(200,"success",data);
}
public static Result error(int code,String msg){
return new Result(code,msg,null);
}
}
2.ErrorCode类
package com.example.springboot_vue.entity;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public enum ErrorCode {
SYSTEM_ERROR(1001,"系统错误"),
INSERT_ERROR(1002,"插入失败"),
REMOVE_ERROR(1003,"删除失败"),
PATCH_REMOVE_ERROR(1004,"批量删除失败");
private int code;
private String msg;
public int getCode(){
return this.code;
}
public void setCode(int code){
this.code=code;
}
public String getMsg(){
return this.msg;
}
public void setMsg(String msg){
this.msg=msg;
}
}
3.Pages类
package com.example.springboot_vue.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pages<T> {
private long total;
private List<T> data;
}