项目结构
- 该生成器代码是用于多个模块之间的生成
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c7245e548395fd32bfc4f709707da9e5.png)
BasePojo
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BasePojo {
@TableId(type = IdType.AUTO)
private Integer id;
@JsonIgnore
@TableField(exist = false)
private Integer page =1;
@JsonIgnore
@TableField(exist = false)
private Integer limit=5;
@JsonIgnore
@TableField(exist = false)
private Integer withPage =1;
@TableField("active")
private Integer active;
@TableField("create_time")
private LocalDateTime createTime;
@TableField("update_time")
private LocalDateTime updateTime;
}
CodeGenerator
public class CodeGenerator {
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir") + "/";
String projectName = "admin";
String pojoName = "pojo";
gc.setOutputDir(projectPath + "/src/main/java");
gc.setFileOverride(true);
gc.setAuthor("liuyong");
gc.setOpen(false);
mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/adms?useSSL=false&serverTimezone=Asia/Shanghai");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("654321");
mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig();
pc.setParent("com.neuedu");
pc.setEntity("pojo");
pc.setService("service");
pc.setMapper("mapper");
pc.setServiceImpl("service.impl");
pc.setController("controller");
Map<String, String> pathInfo = new HashMap<>();
pathInfo.put("entity_path", projectPath + pojoName + "/src/main/java/com/neuedu/pojo");
pathInfo.put("mapper_path", projectPath + projectName + "/src/main/java/com/neuedu/mapper");
pathInfo.put("service_path", projectPath + projectName + "/src/main/java/com/neuedu/service");
pathInfo.put("service_impl_path", projectPath + projectName + "/src/main/java/com/neuedu/service/impl");
pathInfo.put("controller_path", projectPath + projectName + "/src/main/java/com/neuedu/controller");
pc.setPathInfo(pathInfo);
mpg.setPackageInfo(pc);
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
}
};
String templatePath = "/templates/mapper.xml.ftl";
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + projectName + "/src/main/resources/com/neuedu/mapper"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("controllerInit.java");
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass(BasePojo.class);
strategy.setSuperEntityColumns("id", "active", "update_time", "create_time");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix("sys");
strategy.setEntityTableFieldAnnotationEnable(true);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
controllerInit.java.ftl
package ${package.Controller};
import java.util.Map;
import javax.annotation.Resource;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import com.neuedu.util.CommonResult;
import com.neuedu.util.InitUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
private final Logger logger = LoggerFactory.getLogger(${table.controllerName}.class);
@Resource
private ${table.serviceName} i${entity}Service;
@GetMapping
public CommonResult getList(@RequestParam Map<String, Object> param) {
InitUtil.initPage(param);
int num = Integer.parseInt(param.get("page").toString());
int limit = Integer.parseInt(param.get("limit").toString());
QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
InitUtil.initEq(param, wrapper, "active");
IPage<${entity}> page = new Page<>(num, limit);
return CommonResult.success(i${entity}Service.page(page, wrapper));
}
@GetMapping("/{id}")
public CommonResult getById(@PathVariable int id) {
${entity} i${entity} = i${entity}Service.getById(id);
if (i${entity} == null) {
return CommonResult.failed();
}
return CommonResult.success(i${entity});
}
@GetMapping("/check/{name}")
public CommonResult checkName(@PathVariable String name,@RequestParam String value){
QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
wrapper.eq(name, value);
if (i${entity}Service.getOne(wrapper) != null){
return CommonResult.failed();
}
return CommonResult.success();
}
@PostMapping()
public CommonResult save(@RequestBody ${entity} i${entity}) {
if (i${entity}Service.save(i${entity})){
return CommonResult.successMsg("添加成功");
}
return CommonResult.failed("添加失败");
}
@PutMapping("/{id}")
public CommonResult update(@RequestBody ${entity} i${entity}, @PathVariable int id) {
i${entity}.setId(id);
if (i${entity}Service.updateById(i${entity})){
return CommonResult.successMsg("修改成功");
}
return CommonResult.failed("修改失败");
}
@PutMapping("/back/{id}")
public CommonResult back(@PathVariable int id) {
${entity} i${entity} = new ${entity}();
i${entity}.setId(id);
i${entity}.setActive(1);
if (i${entity}Service.updateById(i${entity})){
return CommonResult.successMsg("激活成功");
}
return CommonResult.failed("激活失败");
}
@PutMapping("/del/{id}")
public CommonResult loginDel(@PathVariable int id) {
${entity} i${entity} = new ${entity}();
i${entity}.setId(id);
i${entity}.setActive(0);
if (i${entity}Service.updateById(i${entity})){
return CommonResult.successMsg("设置失效成功");
}
return CommonResult.failed("设置失效失败");
}
@PutMapping("/batchDel")
public CommonResult LoginBatchDel(@RequestParam String ids) {
${entity} i${entity} = new ${entity}();
i${entity}.setActive(0);
String[] idList = ids.split(",");
for (String id : idList) {
i${entity}.setId(Integer.parseInt(id));
i${entity}Service.updateById(i${entity});
}
return CommonResult.successMsg("批量失效成功");
}
@DeleteMapping("/{id}")
public CommonResult delById(@PathVariable int id) {
if (i${entity}Service.removeById(id)){
return CommonResult.successMsg("删除成功");
}
return CommonResult.failed("删除失败");
}
}
</#if>
InitUtil
package com.neuedu.util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.StringUtils;
import java.util.Map;
public class InitUtil {
public static void initPage(Map<String, Object> param) {
if (StringUtils.isEmpty(param.get("page"))) {
param.put("page", 1);
}
if (StringUtils.isEmpty(param.get("limit"))) {
param.put("limit", 5);
}
}
public static void initLike(Map<String, Object> param, QueryWrapper wrapper, String ...str){
for(String s:str){
if(!StringUtils.isEmpty(param.get(s))){
wrapper.like(s, param.get(s));
}
}
}
public static void initEq(Map<String, Object> param,QueryWrapper wrapper, String ...str){
for(String s:str){
if(!StringUtils.isEmpty(param.get(s))){
wrapper.eq(s, param.get(s));
}
}
}
}
CommonResult
package com.neuedu.util;
import lombok.Data;
import java.io.Serializable;
@Data
public class CommonResult implements Serializable {
private Integer code;
private String message;
private Object obj;
private CommonResult(Integer code, String message, Object obj) {
this.code = code;
this.message = message;
this.obj = obj;
}
public static CommonResult success(Object obj)
{
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),obj);
}
public static CommonResult success()
{
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),null);
}
public static CommonResult successMsg(String msg)
{
return new CommonResult(ResultCode.SUCCESS.getCode(),msg,null);
}
public static CommonResult failed()
{
return new CommonResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(),null);
}
public static CommonResult failed(String msg)
{
return new CommonResult(ResultCode.FAILED.getCode(),msg,null);
}
}
ResultCode
import lombok.Getter;
@Getter
public enum ResultCode {
SUCCESS(200,"操作成功"),
FAILED(500,"操作失败"),
;
private Integer code;
private String message;
ResultCode(Integer code, String message) {
this.code = code;
this.message = message;
}
}
问题
- idea 修改文件类型: 在
File→Settings->File Types
里进行配置 - idea 运行main方法时,没找到字节码文件
删除idea生成的配置文件,然后pom右击选择reload project