使用MybatisPlus代码生成

在真实项目开发中我们的服务模块,一般都要进行数据库操作,并且每个domain都有crud,需多次写重复代码。我们使用MybatisPlus,就不用写重复代码,并且还有模板的功能,可以一键生成daomin,query,mapper接口,mapper.xml,service,controller,非常好用。

它本身就是一个工具,一般单独用一个模块来生成代码;

单独新建模块代码生成器

1.创建代码生成器模块,导入mybatis-plus-boot、mybatis-plus-generator、velocity、mysql(8.0)

<properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
        <!--代码生成模式插件  3.0.3以后需要手动设置依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>
        <!-- 模板引擎 依赖,MyBatis-Plus 支持 Velocity-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.1</version>
        </dependency>
        <!--数据库连接依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
    </dependencies>

2.创建准备好的模板 controller.java.vm 、query.java.vm 拷贝到resources /templates目录下(直接用不用改)

package ${
   package.Controller};

import ${
   package.Service}.${
   table.serviceName};
import ${
   package.Entity}.${
   entity};
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.validation.Valid;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.itsource.pojo.query.PageQueryWrapper;
import cn.itsource.result.JSONResult;
import cn.itsource.result.PageResult;

@Tag(name = "$!{table.comment}",description = "$!{table.comment}")
@RestController
@RequestMapping("/manager/${table.entityPath}")
public class ${
   entity}Controller{
   

    @Autowired
    public ${
   table.serviceName} ${
   table.entityPath}Service;

    @Operation( summary= "保存${entity}",description = "基础对象保存接口")
    @Parameter(name = "${table.entityPath}",description = "保存的对象",required = true)
    @PostMapping
    public JSONResult save(@RequestBody @Valid ${
   entity} ${
   table.entityPath}){
   
        return JSONResult.success(${
   table.entityPath}Service.save(${
   table.entityPath}));
    }

    @Operation( summary= "修改${entity}",description = "基础对象修改接口")
    @Parameter(name = "${table.entityPath}",description = "修改的对象",required = true)
    @PutMapping
    public JSONResult update(@RequestBody  @Valid ${
   entity} ${
   table.entityPath}){
   
        return JSONResult.success(${
   table.entityPath}Service.updateById(${
   table.entityPath}));
    }

    //删除对象
    @Operation( summary= "删除${entity}",description = "基础对象删除接口,采用状态删除")
    @Parameter(name = "id",description = "删除的对象ID",required = true)
    @DeleteMapping(value="/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
   
        return JSONResult.success(${
   table.entityPath}Service.removeById(id));
    }

    //获取对象
    @Operation( summary= "获取${entity}",description = "基础对象获取接口")
    @Parameter(name = "id",description = "查询的对象ID",required = true)
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id){
   
        return JSONResult.success(${
   table.entityPath}Service.getById(id));
    }

    //获取列表:PageQueryWrapper作为通用的查询对象
    @Operation( summary= "查询${entity}列表",description = "基础对象列表查询,不带分页")
    @Parameter(name = "query",description = "查询条件",required = true)
    @PostMapping(value = "/list")
    public JSONResult list(@RequestBody PageQueryWrapper<${
   entity}> query){
   
        QueryWrapper<${
   entity}> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        return JSONResult.success(${
   table.entityPath}Service.list(wrapper));
    }

    //分页查询
    @Operation( summary= "查询${entity}分页列表",description = "基础对象列表查询,带分页")
    @Parameter(name = "query",description = "查询条件,需要指定分页条件",required = true)
    @PostMapping(value = "/pagelist")
    public JSONResult page(@RequestBody PageQueryWrapper<${
   entity}> query){
   
        //分页查询
        Page<${
   entity}> page = new Page<${
   entity}>(query.getPage(),query.getRows());
        QueryWrapper<${
   entity}> wrapper = new QueryWrapper<>();
        //实体类作为查询条件
        wrapper.setEntity(query.getQuery());
        //分页查询
        page = ${
   table.entityPath}Service.page(page,wrapper);
        //返回结果
        return JSONResult.success(new PageResult<${
   entity}>(page.getTotal(),page.getRecords()));
    }

}

package ${
   package.Entity};

#foreach($pkg in ${
   table.importPackages})
import ${
   pkg};
#end
#if(${
   swagger2})
import io.swagger.v3.oas.annotations.media.Schema;
#end
#if(${
   entityLombokModel})
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import com.fasterxml.jackson.annotation.JsonFormat;

#end

/**
 * <p>
 * $!{table.comment}
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
#if(${
   entityLombokModel})
@Data
  #if(${
   superEntityClass})
@EqualsAndHashCode(callSuper = true)
  #else
@EqualsAndHashCode(callSuper = false)
  #end
@Accessors(chain = true)
#end
#if(${
   table.convert})
@TableName("${table.name}")
#end
#if(${
   swagger2})
@Schema(name = "${entity}对象", description = "$!{table.comment}")
#end
#if(${
   superEntityClass})
public class ${
   entity} extends ${
   superEntityClass}#if(${
   activeRecord})<${
   entity}>#end {
   
#elseif(${
   activeRecord})
public class ${
   entity} extends Model<${
   entity}> {
   
#else
public class ${
   entity} implements Serializable {
   
#end

#if(${
   entitySerialVersionUID})
    private static final long serialVersionUID=1L;
#end
## ----------  BEGIN 字段循环遍历  ----------
#foreach($field in ${
   table.fields})
#if(${
   field.keyFlag})
#set($keyPropertyName=${
   field.propertyName})
#end
  #if(${
   swagger2})

    @Schema(name = "${field.propertyName}", description = "${field.comment}")
  #else
    #if("$!field.comment" != "")
    /**
     * ${field.comment}
     */
  #end
#end
#if(${
   field.keyFlag})
## 主键
    @JsonFormat(shape = JsonFormat.Shape.STRING)
  #if(${
   field.keyIdentityFlag})
    @TableId(value = "${field.name}", type = IdType.AUTO)
  #elseif(!$null.isNull(${
   idType}) && "$!idType" != "")
    @TableId(value = "${field.name}", type = IdType.${
   idType})
  #elseif(${
   field.convert})
    @TableId("${field.name}")
  #end
## 普通字段
#elseif(${
   field.fill})
## -----   存在字段填充设置   -----
  #if(${
   field.convert})
    @TableField(value = "${field.name}", fill = FieldFill.${
   field.fill})
  #else
    @TableField(fill = FieldFill.${
   field.fill})
  #end
#elseif(${
   field.convert})
    @TableField("${field.name}")
#end
#if(${
   versionFieldName}==${
   field.name})
    @Version
#end
#if(${<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值