首页 > 其他分享 >Mybatis-Plus自动生成代码,自定义Controller

Mybatis-Plus自动生成代码,自定义Controller

时间:2023-05-26 11:03:43浏览次数:47  
标签:Service 自定义 builder Controller import Mybatis table public entityPath


MP网址:https://baomidou.com/pages/779a6e/#%E4%BD%BF%E7%94%A8

直接copy官网代码修改成自己的:

private void generate() {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8", "root", "P@ss123.")
                .globalConfig(builder -> {
                    builder.author("qingxi") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("/Users/qingxi/Code/qingxi/springboot/boot-mybatis/src/main/java/"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.example.bootmybatis") // 设置父包名
                            .moduleName("") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "/Users/qingxi/Code/qingxi/springboot/boot-mybatis/src/main/resources/mapper/")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder().enableLombok();
                    builder.mapperBuilder().enableMapperAnnotation().build();
                    builder.controllerBuilder().enableHyphenStyle()  // 开启驼峰转连字符
                            .enableRestStyle();  // 开启生成@RestController 控制器
                    builder.addInclude("t_user") // 设置需要生成的表名
                            .addTablePrefix("t_"); // 设置过滤表前缀
                })
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();

    }

在test里面运行:

Mybatis-Plus自动生成代码,自定义Controller_intellij-idea


运行效果:

Mybatis-Plus自动生成代码,自定义Controller_intellij-idea_02


直接生成了User表的全部代码。

Mybatis-Plus自动生成代码,自定义Controller_spring_03


自定义Controller模板:

package ${package.Controller};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import $!{package.Service}.$!{table.serviceName};
import ${package.Entity}.${entity};

#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});
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return ${table.entityPath}Service.removeById(id);
    }

    @GetMapping
    public List<${entity}> findAll() {
        return ${table.entityPath}Service.list();
    }

    @GetMapping("/{id}")
    public List<${entity}> findOne(@PathVariable Integer id) {
        return ${table.entityPath}Service.list();
    }

    @GetMapping("/page")
    public Page<${entity}> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        return ${table.entityPath}Service.page(new Page<>(pageNum, pageSize));
    }

}

#end

效果:UserController.java

Mybatis-Plus自动生成代码,自定义Controller_#if_04


标签:Service,自定义,builder,Controller,import,Mybatis,table,public,entityPath
From: https://blog.51cto.com/u_16130732/6354401

相关文章

  • 如何在Angular应用程序中插入自定义 CSS?这里有答案!
    KendoUIforAngular是专用于Angular开发的专业级Angular组件,telerik致力于提供纯粹的高性能AngularUI组件,无需任何jQuery依赖关系。KendoUIR12023正式版下载Telerik_KendoUI产品技术交流群:726377843    欢迎一起进群讨论为什么需要在Angular应用程序中插入自定义C......
  • MyBatis源码之SqlSession的解读
    MyBatis源码之SqlSession的解读1、SqlSession的简单使用首先先说一下SqlSession是什么?SqlSession其实就是对JDBC中Connection的一个封装,简化了对数据库的操作。当你获取到一个SqlSession的时候其实就是获取到一个数据库连接,可以进行数据库操作,SqlSession顾名思义就是一次与数据......
  • Springboot+Mybatisplus+ClickHouse集成
    核心依赖引入<dependency><groupId>ru.yandex.clickhouse</groupId><artifactId>clickhouse-jdbc</artifactId><version>0.1.53</version></dependency><!--Mybati......
  • 框架技术 ---- Mybatis介绍
    内容管理       三层架构       JDBC的缺陷、       框架framework       Mybatis       mybatis主配置文件       Mybatis映射文件mapper           select标签       mybatis简单使用         ......
  • Java自定义注解(Annontation)
    (一)注解简介注解(Annontation),Java5引入的新特性,位于java.lang.annotation包中。提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata)与程序元素(类、方法、成员变量等)进行关联。是一种说明、配置、描述性的信息,与具体业务无关,也不会影响正常的业务逻辑。但我们可以用反......
  • 自定义频率类
    自定义频率类classSuperThrottle(BaseThrottle):VISIT_RECORD={}def__init__(self):self.history=Nonedefallow_request(self,request,view):#自己写逻辑,判断是否超频#(1)取出访问者ip#(2)判断当前ip不在访问字典......
  • mybatis-config详解
    mybatis-config详解5.1属性、全局设置、别名5.1.1本章目标学会使用XML配置文件配置Mybatis5.1.2本节目标详细了解每个属性的作用5.1.3mybatis-config.xml介绍以下是mybatis官网关于配置文件的说明*注意:配置项的顺序不能颠倒,如果颠倒了它们的顺序,在MyBatis的自启动阶段会发生异......
  • mybatis-plus控制台打印SQL
    方法一、在application.yml添加配置#mybatis-plus配置控制台打印完整带参数SQL语句mybatis-plus:configuration:log-impl:org.apache.ibatis.logging.stdout.StdOutImpl方法二、mapper增加日志等级为debuglogging:level:com.chz.mapper:debug ......
  • 多选自定义事件
    <template><view@click="onSelect(item,index)"v-for="(item,index)inlist"> //当选择的等于当前id才显示<imagev-if="selected.indexOf(item)>=0"></image><imagev-else></image></view><......
  • 普加项目管理中间件示例之二:自定义表格列
    自定义表格列示例demo/diyColumns.html上文介绍了标准列,这里介绍一下自定义列。正如标准列是一些预设好的对象,自定义列也是一个对象。支持多种数据类型的列:String、Number、Boolean、Date、Array等支持多种单元格编辑器:TextBox、Spinner、CheckBox、DatePicker、ComboBox、TreeSel......