EasyCode整合mybatis-plus的配置,包含DTO、Request以及MVC三层结构

文章目录

  • entity
  • mapper.java
  • mapper.xml
  • service
  • serviceImpl
  • controller
  • RespDTO
  • CreateRequest
  • UpdateRequest
  • PageRequest
  • SpringCloud项目使用方法
  • 生成通用实体类
  • 生成mapper以及service
  • 生成Controller

这篇文章不教你如何安装和使用EasyCode,只是贴出可以使用的配置。
具体EasyCode的使用可以查看其它的文章。

entity

##导入宏定义
$!{define.vm}

##保存文件(宏定义)
#save("/entity", ".java")

##包路径(宏定义)
#setPackageSuffix("entity")

##自动导入包(全局变量)
$!{autoImport.vm}

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Builder;

##表注释(宏定义)
#tableComment("表实体类")

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class $!{tableInfo.name}  {
#foreach($column in $tableInfo.pkColumn)
    #if(${column.comment})//${column.comment}#end
@TableId
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end

#foreach($column in $tableInfo.otherColumn)
    #if(${column.comment})//${column.comment}#end

    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end

}


}

mapper.java

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("Mapper")

##保存文件(宏定义)
#save("/mapper", "Mapper.java")
##包路径(宏定义)
#setPackageSuffix("mapper")

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};

##表注释(宏定义)
#tableComment("表数据库访问层")
@Mapper
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {

}


mapper.xml

##引入mybatis支持
$!mybatisSupport

##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper">
    <!-- 结果集 -->
    <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}Map">
#foreach($column in $tableInfo.fullColumn)
        <result property="$!{column.name}" column="$!{column.obj.name}" jdbcType="$!{column.ext.jdbcType}"/>
#end
    </resultMap>
    
    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        #allSqlColumn()
    </sql>
    
</mapper>



service

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("Service")

##保存文件(宏定义)
#save("/service", "Service.java")

##包路径(宏定义)
#setPackageSuffix("service")


import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.dto.$!{tableInfo.name}RespDTO;
import $!{tableInfo.savePackageName}.req.$!{tableInfo.name}CreateRequest;
import $!{tableInfo.savePackageName}.req.$!{tableInfo.name}PageRequest;
import $!{tableInfo.savePackageName}.req.$!{tableInfo.name}UpdateRequest;

##表注释(宏定义)
#tableComment("表服务接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {
    
    $!{tableInfo.name}RespDTO get$!{tableInfo.name}ById(Long $!{tableInfo.name}Id);

    PageResponse<$!{tableInfo.name}RespDTO> pageQuery$!{tableInfo.name}($!{tableInfo.name}PageRequest requestParam);

    $!{tableInfo.name}RespDTO update$!{tableInfo.name}($!{tableInfo.name}UpdateRequest updateRequest);

    Boolean delete$!{tableInfo.name}ById(Long $!{tableInfo.name}Id);

    Boolean batchDelete$!{tableInfo.name}(List<Long> ids);

    $!{tableInfo.name}RespDTO create$!{tableInfo.name}($!{tableInfo.name}CreateRequest createRequest);
}


serviceImpl

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")

##保存文件(宏定义)
#save("/service/Impl", "ServiceImpl.java")

##包路径(宏定义)
#setPackageSuffix("service.Impl")

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import $!{tableInfo.savePackageName}.dto.$!{tableInfo.name}RespDTO;
import $!{tableInfo.savePackageName}.req.$!{tableInfo.name}CreateRequest;
import $!{tableInfo.savePackageName}.req.$!{tableInfo.name}PageRequest;
import $!{tableInfo.savePackageName}.req.$!{tableInfo.name}UpdateRequest;


##表注释(宏定义)
#tableComment("表服务实现类")
@Service
@Slf4j
@RequiredArgsConstructor
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
 
    private final $!{tableInfo.name}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper;

    private final LogClient logClient;

    @Override
    public $!{tableInfo.name}RespDTO get$!{tableInfo.name}ById(Long $!{tableInfo.name}Id) {
        return null;
    }

    @Override
    public PageResponse<$!{tableInfo.name}RespDTO> pageQuery$!{tableInfo.name}($!{tableInfo.name}PageRequest requestParam) {
        return null;
    }

    @Override
    public $!{tableInfo.name}RespDTO update$!{tableInfo.name}($!{tableInfo.name}UpdateRequest updateRequest) {
        return null;
    }

    @Override
    public Boolean delete$!{tableInfo.name}ById(Long $!{tableInfo.name}Id) {
        return null;
    }

    @Override
    public Boolean batchDelete$!{tableInfo.name}(List<Long> ids) {
        return null;
    }

    @Override
    public $!{tableInfo.name}RespDTO create$!{tableInfo.name}($!{tableInfo.name}CreateRequest createRequest) {
        return null;
    }
}

controller

##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Controller")
##保存文件(宏定义)
#save("/controller", "Controller.java")
##包路径(宏定义)
#setPackageSuffix("controller")
##定义服务名
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
##定义实体对象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))


import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;



import org.springframework.web.bind.annotation.*;
import java.util.List;

import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import $!{tableInfo.savePackageName}.dto.$!{tableInfo.name}RespDTO;
import $!{tableInfo.savePackageName}.req.$!{tableInfo.name}CreateRequest;
import $!{tableInfo.savePackageName}.req.$!{tableInfo.name}PageRequest;
import $!{tableInfo.savePackageName}.req.$!{tableInfo.name}UpdateRequest;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;


import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.Executors;



##表注释(宏定义)
#tableComment("表控制层[不建议修改,如果有新增的方法,写在子类中]")

@VLog
@RestController
@RequestMapping("/v1/$!{entityName}")
@RequiredArgsConstructor
public class $!{tableName} {
  

    $!{tableInfo.name}Service $!{serviceName};   
  
   /**
     * 按照ID查询
     * @param $!{entityName}Id
     * @return
     */
    @GetMapping("")
    public Result<$!{tableInfo.name}RespDTO> get$!{tableInfo.name}ById(@Validated @RequestParam(name = "$!{entityName}Id") @NotNull(message = "$!{entityName}Id Can not be null") Long $!{entityName}Id) {
        $!{tableInfo.name}RespDTO result = $!{entityName}Service.get$!{tableInfo.name}ById($!{entityName}Id);
        return Result.ok(result);
    }

    /**
     * 带条件分页查询
     * @param requestParam
     * @return
     */
    @GetMapping("/page")
    public Result<PageResponse<$!{tableInfo.name}RespDTO>> pageQuery$!{tableInfo.name}(@Validated $!{tableInfo.name}PageRequest requestParam) {
        return Result.ok($!{entityName}Service.pageQuery$!{tableInfo.name}(requestParam));
    }

    /**
     * 基于ID修改
     * @param updateRequest
     * @return
     */
    @PutMapping("")
    public Result<$!{tableInfo.name}RespDTO> update$!{tableInfo.name}(@Validated @RequestBody $!{tableInfo.name}UpdateRequest updateRequest){
       return Result.ok($!{entityName}Service.update$!{tableInfo.name}(updateRequest));
    }

    /**
     * 基于ID修改
     * @param $!{entityName}Id
     * @return
     */
    @DeleteMapping("")
    public Result<Boolean> delete$!{tableInfo.name}ById(@RequestParam @Validated Long $!{entityName}Id){
        return Result.ok($!{entityName}Service.delete$!{tableInfo.name}ById($!{entityName}Id));
    }

    /**
     * 根据ID批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete$!{tableInfo.name}(@RequestBody List<Long> ids){
        return Result.ok($!{entityName}Service.batchDelete$!{tableInfo.name}(ids));
    }

    /**
     * 创建
     * @param createRequest
     * @return
     */
    @PostMapping("")
    public Result<$!{tableInfo.name}RespDTO> create$!{tableInfo.name}(@RequestBody @Valid $!{tableInfo.name}CreateRequest createRequest){
        return Result.ok($!{entityName}Service.create$!{tableInfo.name}(createRequest));

    }
  
}


RespDTO

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("RespDTO")
##保存文件(宏定义)
#save("/dto", "RespDTO.java")
##包路径(宏定义)
#setPackageSuffix("dto")
##自动导入包(全局变量)
$!{autoImport.vm}

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Builder;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class $!{tableName}  {

}

CreateRequest

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("CreateRequest")
##保存文件(宏定义)
#save("/req", "CreateRequest.java")
##包路径(宏定义)
#setPackageSuffix("req")

##自动导入包(全局变量)
$!{autoImport.vm}

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Builder;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class $!{tableName}  {

}

UpdateRequest

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("UpdateRequest")
##保存文件(宏定义)
#save("/req", "UpdateRequest.java")
##包路径(宏定义)
#setPackageSuffix("req")

##自动导入包(全局变量)
$!{autoImport.vm}

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Builder;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class $!{tableName}  {

}

PageRequest

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("PageRequest")
##保存文件(宏定义)
#save("/req", "PageRequest.java")
##包路径(宏定义)
#setPackageSuffix("req")

##自动导入包(全局变量)
$!{autoImport.vm}

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Builder;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class $!{tableName}  {

}

SpringCloud项目使用方法

在SpringCloud项目下,我的不同模块存放不同的类。
EasyCode整合mybatis-plus的配置,包含DTO、Request以及MVC三层结构_第1张图片
比如common存放通用实体类,core存放mapper以及service,web则存放controller层代码。
那么使用EasyCode生成代码的时候。方法如下:

生成通用实体类

EasyCode整合mybatis-plus的配置,包含DTO、Request以及MVC三层结构_第2张图片

生成mapper以及service

EasyCode整合mybatis-plus的配置,包含DTO、Request以及MVC三层结构_第3张图片

生成Controller

EasyCode整合mybatis-plus的配置,包含DTO、Request以及MVC三层结构_第4张图片

你可能感兴趣的:(java,mybatis)