idea整合EasyCode基于lombok和swagger自定义模板

idea整合EasyCode见:https://blog.csdn.net/weixin_42201180/article/details/107511798

1. 实体类entity模板

##引入宏定义
$!define

##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")

##使用宏定义设置包后缀
#setPackageSuffix("entity")

##使用全局变量实现默认包导入
$!autoImport
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
 * @author: $!author
 * @date: $!time.currTime()
 * @description: $!{tableInfo.comment}($!{tableInfo.name})实体类
 */
@Getter
@Setter
@ApiModel("#if($!{tableInfo.comment})$!{tableInfo.comment}实体类#end")
public class $!{
    
    tableInfo.name} implements Serializable {
    
    
    private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
    
    @ApiModelProperty("#if(${column.comment})${column.comment}#end")
    private $!{
    
    tool.getClsNameByFullName($column.type)} $!{
    
    column.name};
#end

}

2. 控制层controller模板

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{
    
    tableInfo.savePackageName}.#{
    
    end}controller;

import $!{
    
    tableInfo.savePackageName}.entity.$!{
    
    tableInfo.name};
import $!{
    
    tableInfo.savePackageName}.service.$!{
    
    tableInfo.name}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: $!author
 * @date: $!time.currTime()
 * @description: $!{tableInfo.comment}($!{tableInfo.name})表控制层
 */
@Api(tags = "#if($!{tableInfo.comment})$!{tableInfo.comment}API#end")
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{
    
    tableName} {
    
    
    
    @Resource
    private $!{
    
    tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据")
    @GetMapping("/selectOne")
    public $!{
    
    tableInfo.name} selectOne($!pk.shortType id) {
    
    
        return this.$!{
    
    tool.firstLowerCase($tableInfo.name)}Service.queryById(id);
    }

}

3. 服务接口service模板

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))

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

#if($tableInfo.savePackageName)package $!{
    
    tableInfo.savePackageName}.#{
    
    end}service;

import $!{
    
    tableInfo.savePackageName}.entity.$!{
    
    tableInfo.name};
import java.util.List;

/**
 * @author: $!author
 * @date: $!time.currTime()
 * @description: $!{tableInfo.comment}($!{tableInfo.name})表服务接口
 */
public interface $!{
    
    tableName} {
    
    

    /**
     * 通过ID查询单条数据
     *
     * @param $!pk.name 主键
     * @return 实例对象
     */
    $!{
    
    tableInfo.name} queryById($!pk.shortType $!pk.name);

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<$!{
    
    tableInfo.name}> queryAllByLimit(int offset, int limit);

    /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 实例对象
     */
    $!{
    
    tableInfo.name} insert($!{
    
    tableInfo.name} $!tool.firstLowerCase($!{
    
    tableInfo.name}));

    /**
     * 修改数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 实例对象
     */
    $!{
    
    tableInfo.name} update($!{
    
    tableInfo.name} $!tool.firstLowerCase($!{
    
    tableInfo.name}));

    /**
     * 通过主键删除数据
     *
     * @param $!pk.name 主键
     * @return 是否成功
     */
    boolean deleteById($!pk.shortType $!pk.name);

}

4. 服务实现类serviceImpl模板

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))

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

#if($tableInfo.savePackageName)package $!{
    
    tableInfo.savePackageName}.#{
    
    end}service.impl;

import $!{
    
    tableInfo.savePackageName}.entity.$!{
    
    tableInfo.name};
import $!{
    
    tableInfo.savePackageName}.mapper.$!{
    
    tableInfo.name}Mapper;
import $!{
    
    tableInfo.savePackageName}.service.$!{
    
    tableInfo.name}Service;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: $!author
 * @date: $!time.currTime()
 * @description: $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
 */
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{
    
    tableName} implements $!{
    
    tableInfo.name}Service {
    
    
    @Resource
    private $!{
    
    tableInfo.name}Mapper $!tool.firstLowerCase($!{
    
    tableInfo.name})Mapper;

    /**
     * 通过ID查询单条数据
     *
     * @param $!pk.name 主键
     * @return 实例对象
     */
    @Override
    public $!{
    
    tableInfo.name} queryById($!pk.shortType $!pk.name) {
    
    
        return this.$!{
    
    tool.firstLowerCase($!{
    
    tableInfo.name})}Mapper.queryById($!pk.name);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<$!{
    
    tableInfo.name}> queryAllByLimit(int offset, int limit) {
    
    
        return this.$!{
    
    tool.firstLowerCase($!{
    
    tableInfo.name})}Mapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 实例对象
     */
    @Override
    public $!{
    
    tableInfo.name} insert($!{
    
    tableInfo.name} $!tool.firstLowerCase($!{
    
    tableInfo.name})) {
    
    
        this.$!{
    
    tool.firstLowerCase($!{
    
    tableInfo.name})}Mapper.insert($!tool.firstLowerCase($!{
    
    tableInfo.name}));
        return $!tool.firstLowerCase($!{
    
    tableInfo.name});
    }

    /**
     * 修改数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 实例对象
     */
    @Override
    public $!{
    
    tableInfo.name} update($!{
    
    tableInfo.name} $!tool.firstLowerCase($!{
    
    tableInfo.name})) {
    
    
        this.$!{
    
    tool.firstLowerCase($!{
    
    tableInfo.name})}Mapper.update($!tool.firstLowerCase($!{
    
    tableInfo.name}));
        return this.queryById($!{
    
    tool.firstLowerCase($!{
    
    tableInfo.name})}.get$!tool.firstUpperCase($pk.name)());
    }

    /**
     * 通过主键删除数据
     *
     * @param $!pk.name 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById($!pk.shortType $!pk.name) {
    
    
        return this.$!{
    
    tool.firstLowerCase($!{
    
    tableInfo.name})}Mapper.deleteById($!pk.name) > 0;
    }
}

5. 数据库访问层dao模板

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Mapper"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/mapper"))

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

#if($tableInfo.savePackageName)package $!{
    
    tableInfo.savePackageName}.#{
    
    end}mapper;

import $!{
    
    tableInfo.savePackageName}.entity.$!{
    
    tableInfo.name};
import org.apache.ibatis.annotations.Param;
import java.util.List;

/**
 * @author: $!author
 * @date: $!time.currTime()
 * @description: $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
 */
public interface $!{
    
    tableName} {
    
    

    /**
     * 通过ID查询单条数据
     *
     * @param $!pk.name 主键
     * @return 实例对象
     */
    $!{
    
    tableInfo.name} queryById($!pk.shortType $!pk.name);

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<$!{
    
    tableInfo.name}> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


    /**
     * 通过实体作为筛选条件查询
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 对象列表
     */
    List<$!{
    
    tableInfo.name}> queryAll($!{
    
    tableInfo.name} $!tool.firstLowerCase($!{
    
    tableInfo.name}));

    /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 影响行数
     */
    int insert($!{
    
    tableInfo.name} $!tool.firstLowerCase($!{
    
    tableInfo.name}));

    /**
     * 批量新增数据(MyBatis原生foreach方法)
     *
     * @param entities List<$!{tableInfo.name}> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(@Param("entities") List<$!{
    
    tableInfo.name}> entities);

    /**
     * 批量新增或按主键更新数据(MyBatis原生foreach方法)
     *
     * @param entities List<$!{tableInfo.name}> 实例对象列表
     * @return 影响行数
     */
    int insertOrUpdateBatch(@Param("entities") List<$!{
    
    tableInfo.name}> entities);

    /**
     * 修改数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 影响行数
     */
    int update($!{
    
    tableInfo.name} $!tool.firstLowerCase($!{
    
    tableInfo.name}));

    /**
     * 通过主键删除数据
     *
     * @param $!pk.name 主键
     * @return 影响行数
     */
    int deleteById($!pk.shortType $!pk.name);

}
    

6. 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>

    <!--查询单个-->
    <select id="queryById" resultMap="$!{tableInfo.name}Map">
        select
          #allSqlColumn()

        from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
        where $!pk.obj.name = #{$!pk.name}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="$!{tableInfo.name}Map">
        select
          #allSqlColumn()

        from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
        limit #{offset}, #{limit}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="$!{tableInfo.name}Map">
        select
          #allSqlColumn()

        from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
        <where>
#foreach($column in $tableInfo.fullColumn)
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
                and $!column.obj.name = #{$!column.name}
            </if>
#end
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
        insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
        values (#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
    </insert>

    <insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
        insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
        insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
        </foreach>
        on duplicate key update
         #foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name) #if($velocityHasNext), #end#end
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}
        <set>
#foreach($column in $tableInfo.otherColumn)
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
                $!column.obj.name = #{$!column.name},
            </if>
#end
        </set>
        where $!pk.obj.name = #{$!pk.name}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
    </delete>

</mapper>
    

猜你喜欢

转载自blog.csdn.net/weixin_42201180/article/details/111624091