EasyCode插件代码生成模板

 EasyCode插件代码生成模板

代码模板截图

EasyCode插件代码生成模板

 entity.java

##引入宏定义
$!define
$!init
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!autoImport
import lombok.Data;
##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
public class $!{tableInfo.name} {
  
#foreach($column in $tableInfo.fullColumn)
    #if(${column.comment})/**
    * ${column.comment}
    */#end

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

dao.java

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

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

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

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

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

    /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 影响行数
     */
    int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
    
    /**
     * 通过recordNo删除数据
     *
     * @param recordNo 记录编号
     * @return 影响行数
     */
    int deleteByRecordNo(String recordNo);
    
    /**
     * 修改数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 影响行数
     */
    int updateByRecordNo($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));


    /**
     * 通过recordNo查询单条数据
     *
     * @param recordNo 记录编号
     * @return 实例对象
     */
    $!{tableInfo.name} queryByRecordNo(String recordNo);

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

}

 service.java

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

##拿到主键
#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 $!{tableInfo.savePackageName}.pojo.dio.$!{tableInfo.name}DIO;
import java.util.List;
import com.powater.common.bean.PageInfoListResult;

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

   /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name})DIO 实例对象
     * @return 实例对象
     */
    String insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})DIO);
    
     /**
     * 通过recordNo删除数据
     *
     * @param recordNo 唯一标识
     * @return 是否成功
     */
    String deleteByRecordNo(String recordNo);

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

    /**
     * 通过recordNo查询单条数据
     *
     * @param recordNo 唯一标志
     * @return 实例对象
     */
    $!{tableInfo.name} queryByRecordNo(String recordNo);
    
    /**
     * 通过条件查询单条数据
     *
     * @param $tool.firstUpperCase($!{tableInfo.name})DQO 查询条件
     * @return 分页对象
     */
     PageInfoListResult<List<$!{tableInfo.name}DOO>> queryByCondition($!{tableInfo.name}DQO $tool.firstUpperCase($!{tableInfo.name})DQO);
     
}

 

serviceImpl.java

$!init
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!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}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import com.powater.common.bean.PageInfoListResult;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
 *
 * @author $!author
 * @since $!time.currTime()
 */
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} implements $!{tableInfo.name}Service {
@Autowired
private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao; @Override public $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return null; } @Override public String deleteByRecordNo(String recordNo) { return null; } @Override public String updateByRecordNo($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return null; } @Override public $!{tableInfo.name}DOO queryByRecordNo(String recordNo) { return null; } @Override public PageInfoListResult<List<$!{tableInfo.name}DOO>> queryByCondition($!{tableInfo.name}DQO $tool.firstUpperCase($!{tableInfo.name})DQO) { return null; } }

 controller.java

$!init
##定义初始变量
#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 org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

/**
 * $!{tableInfo.comment}($!{tableInfo.name})表控制层
 *
 * @author $!author
 * @since $!time.currTime()
 */
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
@Api(value = "$!{tableInfo.comment}相关接口", tags = "$!{tableInfo.comment}相关接口")
public class $!{tableName} {
  
    @Autowired
    private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
    
    
    @ApiOperation(value = "新增-修改",httpMethod = "POST")
    @PostMapping("/save")
    public CommonResult save(@RequestBody $!{tableInfo.name}DIO dio) {
        return null;
    }
    
    @ApiOperation(value = "删除",httpMethod = "GET")
    @GetMapping("/delete")
    public CommonResult delete(@RequestParam(value = "recordNo") String recordNo) {
        return null;
    }

    @ApiOperation(value = "根据唯一标志获取详情",httpMethod = "GET")
    @GetMapping("/detail")
    public CommonResult<$!{tableInfo.name}DOO> getDetail(@RequestParam(value = "recordNo") String recordNo) {
        return null;
    }
    
     @ApiOperation(value = "根据条件查询列表",httpMethod = "POST")
     @PostMapping("/page/list")
     public CommonResult< PageInfoListResult<List<$!{tableInfo.name}DOO>>> getPageListByCondition(@RequestBody $!{tableInfo.name}DQO dqo) {
        return null;
     }
    

}

 

mapper.xml

$!init
##引入mybatis支持
$!mybatisSupport

##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.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}.dao.$!{tableInfo.name}Dao">

    <resultMap id="BaseResultMap" type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
#foreach($column in $tableInfo.fullColumn)
        <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
    </resultMap>
    
    <resultMap id="DOOResultMap" type="$!{tableInfo.savePackageName}.pojo.doo.$!{tableInfo.name}DOO">
#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>
    
    <sql id="DOO_Column_List">
        #allSqlColumn()
           
    </sql>
    
     <!--新增所有列-->
 <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
        insert into $!{tableInfo.obj.name}
        <trim prefix="(" suffix=")" suffixOverrides="," >
#foreach($column in $tableInfo.fullColumn)
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ‘‘#end">
                `$!column.obj.name`,
            </if>
#end
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
#foreach($column in $tableInfo.fullColumn)
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ‘‘#end">
                #{$!column.name,jdbcType=$!column.ext.jdbcType},
            </if>
#end
        </trim>
    </insert>
    
    <!--通过recordNo修改数据-->
    <update id="updateByRecordNo">
        update $!{tableInfo.obj.name}
        <set>
#foreach($column in $tableInfo.otherColumn)
            <if test="$!column.name != null">
                $!column.obj.name = #{$!column.name},
            </if>
#end
        </set>
        where record_no = #{recordNo} and del_flag = 0
    </update>

    <!--通过recordNo逻辑删除-->
    <delete id="deleteByRecordNo">
        update $!{tableInfo.obj.name}
        set del_flag = 1 
        where record_no = #{recordNo} and del_flag = 0
    </delete>

    <!--查询单个-->
    <select id="queryByRecordNo" resultMap="BaseResultMap">
        select <include refid="Base_Column_List" />
        from $!tableInfo.obj.name
        where record_no = #{recordNo}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryByCondition" resultMap="DOOResultMap">
        select <include refid="Base_Column_List" />
        from $!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>

</mapper>

 doo.java

##引入宏定义
$!define
$!init

#set($tableName = $tool.append($tableInfo.name, "DOO"))
##使用宏定义设置回调(保存位置与文件后缀)
#save("/pojo/doo", "DOO.java")

##使用宏定义设置包后缀
#setPackageSuffix("pojo.doo")

##使用全局变量实现默认包导入
$!autoImport
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

##使用宏定义实现类注释信息
#tableComment("doo")
@Data
public class $!{tableName} {
  
#foreach($column in $tableInfo.fullColumn)
    #if(${column.comment})/**
    * ${column.comment}
    */#end

    @ApiModelProperty(value = "${column.comment}")
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end

}

dio.java

##引入宏定义
$!define
$!init

#set($tableName = $tool.append($tableInfo.name, "DIO"))
##使用宏定义设置回调(保存位置与文件后缀)
#save("/pojo/dio", "DIO.java")

##使用宏定义设置包后缀
#setPackageSuffix("pojo.dio")

##使用全局变量实现默认包导入
$!autoImport
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;


##使用宏定义实现类注释信息
#tableComment("dio")
@Data
@ApiModel(description = "$!{tableInfo.comment}-dio")
public class $!{tableName} {
  
#foreach($column in $tableInfo.fullColumn)
    #if(${column.comment})/**
    * ${column.comment}
    */#end
    
    @ApiModelProperty(value = "${column.comment}")
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end

#*
#foreach($column in $tableInfo.fullColumn)
##使用宏定义实现get,set方法
#getSetMethod($column)
#end
*#

}

dqo.java

##引入宏定义
$!define
$!init

#set($tableName = $tool.append($tableInfo.name, "DQO"))
##使用宏定义设置回调(保存位置与文件后缀)
#save("/pojo/dqo", "DQO.java")

##使用宏定义设置包后缀
#setPackageSuffix("pojo.dqo")

##使用全局变量实现默认包导入
$!autoImport
import lombok.Data;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

##使用宏定义实现类注释信息
#tableComment("dqo")
@Data
@ApiModel(description = "$!{tableInfo.comment}-dqo")
public class $!{tableName} {

    @ApiModelProperty(value = "当前页号")
    private Integer currentPage;
    
    @ApiModelProperty(value = "每页记录条数")
    private Integer pageSize;
}

全局配置——mybatisSupport

##针对Mybatis 进行支持,主要用于生成xml文件
#foreach($column in $tableInfo.fullColumn)
    ##储存列类型
    $tool.call($column.ext.put("sqlType", $tool.getField($column.obj.dataType, "typeName")))
    #if($tool.newHashSet("java.lang.String").contains($column.type))
        #set($jdbcType="VARCHAR")
    #elseif($tool.newHashSet("java.lang.Boolean", "boolean").contains($column.type))
        #set($jdbcType="BOOLEAN")
    #elseif($tool.newHashSet("java.lang.Byte", "byte").contains($column.type))
        #set($jdbcType="BYTE")
    #elseif($tool.newHashSet("java.lang.Integer", "int", "java.lang.Short", "short").contains($column.type))
        #set($jdbcType="INTEGER")
    #elseif($tool.newHashSet("java.lang.Long", "long").contains($column.type))
        #set($jdbcType="INTEGER")
    #elseif($tool.newHashSet("java.lang.Float", "float", "java.lang.Double", "double").contains($column.type))
        #set($jdbcType="NUMERIC")
    #elseif($tool.newHashSet("java.util.Date", "java.sql.Timestamp", "java.time.Instant", "java.time.LocalDateTime", "java.time.OffsetDateTime", "    java.time.ZonedDateTime").contains($column.type))
        #set($jdbcType="TIMESTAMP")
    #elseif($tool.newHashSet("java.sql.Date", "java.time.LocalDate").contains($column.type))
        #set($jdbcType="TIMESTAMP")
    #elseif($tool.newHashSet("java.math.BigDecimal").contains($column.type))
        #set($jdbcType="DECIMAL")
    #else
        ##其他类型
        #set($jdbcType="VARCHAR")
    #end
    $tool.call($column.ext.put("jdbcType", $jdbcType))
#end

##定义宏,查询所有列
#macro(allSqlColumn)#foreach($column in $tableInfo.fullColumn)$column.obj.name#if($velocityHasNext), #end#end#end

全局配置——init

##初始化区域

##去掉表的t_前缀
$!tableInfo.setName($tool.getClassName($tableInfo.obj.name.replaceFirst("tb_","")))

##参考阿里巴巴开发手册,POJO 类中布尔类型的变量,都不要加 is 前缀,否则部分框架解析会引起序列化错误
#foreach($column in $tableInfo.fullColumn)
#if($column.name.startsWith("is") && $column.type.equals("java.lang.Boolean"))
    $!column.setName($tool.firstLowerCase($column.name.substring(2)))
#end
#end

##实现动态排除列
#set($temp = $tool.newHashSet("testCreateTime", "otherColumn"))
#foreach($item in $temp)
    #set($newList = $tool.newArrayList())
    #foreach($column in $tableInfo.fullColumn)
        #if($column.name!=$item)
            ##带有反回值的方法调用时使用$tool.call来消除返回值
            $tool.call($newList.add($column))
        #end
    #end
    ##重新保存
    $tableInfo.setFullColumn($newList)
#end

##对importList进行篡改
#set($temp = $tool.newHashSet())
#foreach($column in $tableInfo.fullColumn)
    #if(!$column.type.startsWith("java.lang."))
        ##带有反回值的方法调用时使用$tool.call来消除返回值
        $tool.call($temp.add($column.type))
    #end
#end
##覆盖
#set($importList = $temp)

全局配置——author

dbx

使用注意事项

要根据实际情况修改代码,模板中使用到了swagger的注解 ,要导入相应的依赖。author为自定义的全局配置,在生成代码的时候如果要去除表的前缀,记得在全局配置中的init中进行相关的修改

 

EasyCode插件代码生成模板

上一篇:类型兼容性


下一篇:Geolocation 地理定位