隨筆(五)『SpringBoot & MybatisPlus 程式碼生成(CRUD)』
阿新 • • 發佈:2022-12-09
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);
}
}