MybatisPlus代码生成器相关代码备份

CodeGenerate.java代码

package com.example.springbootweb.utils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import java.util.Collections;

/**
 * mp代码生成器
 * by SisyphusTang
 * @since 2023-3-18
 */
public class CodeGenerator {

    public static void main(String[] args) {
        generate();
    }

    private static void generate() {
        FastAutoGenerator.create("数据库地址", "account", "pwd")
                .globalConfig(builder -> {
                    builder.author("SisyphusTang") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("项目绝对地址\\src\\main\\java\\"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.example.springbootweb") // 设置父包名
                            .moduleName(null) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "项目绝对地址\\src\\main\\resources\\mapper\\")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder().enableLombok();
//                    builder.mapperBuilder().enableMapperAnnotation().build();
                    builder.controllerBuilder().enableHyphenStyle()  // 开启驼峰转连字符
                            .enableRestStyle();  // 开启生成@RestController 控制器
                    builder.addInclude("user") // 设置需要生成的表名
                            .addTablePrefix("t_", "sys_"); // 设置过滤表前缀
                })
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();

    }
}


对应的controller类模板

package ${package.Controller};
//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

/**
 * <p>
 * $!{table.comment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
#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();
    }

    //更新  还是要单独写一下 上面的savaOrUpdate方法是有一些问题的
    @PutMapping
    public Result<?> update(@RequestBody ${entity} ${table.entityPath}) {
        ${table.entityPath}Service.updateById(${table.entityPath});
        return Result.success();
    }

    //根据id删除用户
    @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();
    }

    //根据id查询用户
    @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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值