1、添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
2、工具类
/**
* mybatis-plus 代码生成工具
*/
public class CodeGeneratorUtil {
public static void main(String[] args) {
FastAutoGenerator.create(
"数据库URL",
"数据库username",
"数据库password")
.globalConfig(builder -> {
builder.author("baihua") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("D:\\ideaProject\\my_springboot\\src\\main\\java\\"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.baihua.modules") // 设置父包名
.moduleName("sys") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\ideaProject\\my_springboot\\src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("sys_user") // 设置需要生成的表名
.addTablePrefix("sys_"); // 设置过滤表前缀
})
// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
3、controller模板
package ${package.Controller};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baihua.common.utils.Result;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import java.util.Map;
import java.util.List;
import org.springframework.web.bind.annotation.RestController;
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
/**
* <p>
* $!{table.comment} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
@RestController
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
public class ${table.controllerName} {
@Autowired
private ${table.serviceName} ${table.entityPath}Service;
// 分页
@GetMapping("getPage")
public Result page(Map<String, Object> params) {
return ${table.entityPath}Service.getPage(params);
}
// 增加或修改
@PostMapping("saveOrUpdate")
public Result saveOrUpdate(@RequestBody ${entity} ${table.entityPath}) {
${table.entityPath}Service.saveOrUpdate(${table.entityPath});
return Result.success();
}
// 根据id获取
@GetMapping("getById/{id}")
public Result getById(@PathVariable("id") Long id) {
${entity} ${table.entityPath} = ${table.entityPath}Service.getById(id);
return Result.success(${table.entityPath});
}
// 删除
@PostMapping("deleteByIds")
public Result deleteByIds(@RequestBody List<Long> ids) {
${table.entityPath}Service.removeByIds(ids);
return Result.success();
}
}
4、service模板
package ${package.Service};
import ${package.Entity}.${entity};
import ${superServiceClassPackage};
import com.baihua.common.utils.Result;
import java.util.Map;
/**
* <p>
* $!{table.comment} 服务类
* </p>
*
* @author ${author}
* @since ${date}
*/
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {
Result getPage(Map<String, Object> params);
}
5、serviceImpl模板
package ${package.ServiceImpl};
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.google.common.primitives.Longs;
import com.baihua.common.utils.Result;
import java.util.Map;
import java.util.Optional;
/**
* <p>
* $!{table.comment} 服务实现类
* </p>
*
* @author ${author}
* @since ${date}
*/
@Service
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {
@Override
public Result getPage(Map<String, Object> params) {
Long pageNum = Optional.ofNullable((String) params.get("pageNum")).map(Longs::tryParse).orElse(1L);
Long pageSize = Optional.ofNullable((String) params.get("pageSize")).map(Longs::tryParse).orElse(10L);
QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
Page<${entity}> page = page(new Page<>(pageNum, pageSize), wrapper);
return Result.success(page);
}
}