easycode-自定义的模板-类型对应问题

一、遇到的问题
1、mysql数据库中有些字段没有生成到

在图形工具中修改了表结构 ,增加了字段,这个时候要在idea中刷新下数据库

在这里插入图片描述

2、数据库中有tinyint 类型的字段,生成代码后mapper.xml中jdbcType总是BYTE,但是mybatis中并没有BYTE。

在这里插入图片描述
在这里插入图片描述3、生成代码存放位置

在这里插入图片描述 

(1)前面是包路径,后面是生成文件的存放路径,存放路径是不能后退的,在window的cmd中…/表示进到上级目录,这个在这没用,文件的存放路径只能是你选择的目录的子目录。
(2)还有一个问题是在为多个表生成代码时,生成的代码位置会错乱,跟选择的不一样,出现这样的问题主要是我之前我单表测试生成与多表生成时选择的path路径不一样 ,这种情况勾选“统一配置”就好了,多表生成建议把这个勾上。

4、最好有一个专门生成代码的目录,然后再将生成好的代码复制到项目中
这样的话可能会导致一些包没有导入(用我的模板),在idea中设置自动导包,不用一个个去alt + enter.

在这里插入图片描述

将上面四个勾选框都勾上,这样idea就会自动帮你导包(有些可能不会自动导,但是alt + enter一个后,该文件其他未导入的都会自动帮你导入)

二、自定义模板
说明:$!module是引用定义的全局变量,模板后会贴上全局变量的配置,连续的两个##表示注释1、entity.java

$!init
##引入宏定义
$!define
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity/$!module/", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity.$!module")

##使用全局变量实现默认包导入
$!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
 
}

2、dao.java

##定义初始变量
$!init
$!define

#save("/dao/$!module/", "Dao.java")

##使用宏定义设置包后缀
#setPackageSuffix("dao.$!module")

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;

#set($time=$!time.currTime())
#set($time=$time.substring(0,11))
#set($time=$time.replace("-","/"))
/**
 * <p>
 * $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
 * </p>
 * 
 * @author:$!author
 * @date:$!time
 */
@Mapper
public interface $!{tableInfo.name}Dao {

    /**
     * 通过唯一标识查询单条数据
     *
     * @param recordNo 唯一标识
     * @return 实例对象
     */
    $!{tableInfo.name} queryByRecordNo(String recordNo);

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

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

    /**
     * 通过唯一标识删除数据(物理删除)
     *
     * @param recordNo 唯一标识
     * @return 影响行数
     */
    int deleteByRecordNo(String recordNo);
    
    /**
     * 通过唯一标识删除数据(真实删除)
     *
     * @param recordNo 唯一标识
     * @return 影响行数
     */
    int realDeleteByRecordNo(String recordNo);
    
    /**
     * 批量新增
     * @param recordList 实例列表
     * @return 影响行数
     */
    int batchInsert(@Param("recordList") List<$!{tableInfo.name}> recordList);
    
    /**
     * 根据条件查询列表
     * @param dqo 查询条件
     * @return 实例列表
     */
    List<$!{tableInfo.name}> selectListByCondition($!{tableInfo.name}Dqo dqo);
    
}


3、service.java

$!init
$!define

#save("/service/intefaces/$!module/", "Service.java")
#setPackageSuffix("service.intefaces.$!module")

import com.powater.common.bean.PageInfoListResult;

#set($time=$!time.currTime())
#set($time=$time.substring(0,11))
#set($time=$time.replace("-","/"))
/**
 * <p>
 * $!{tableInfo.comment}($!{tableInfo.name})表服务接口
 * </p>
 * 
 * @author:$!author
 * @date:$!time
 */
public interface $!{tableInfo.name}Service {

    /**
     * 新增或修改 ——$!{tableInfo.comment}——记录
     *
     * @param dio 前端输入信息
     * @return 新增或修改结果
     */
    String addOrUpdate($!{tableInfo.name}Dio dio);
    
     /**
     * 逻辑删除 ——$!{tableInfo.comment}——记录
     *
     * @param recordNo 唯一标识
     * @return 删除结果
     */
    String del(String recordNo);
    
     /**
     * 条件查询 ——$!{tableInfo.comment}——记录
     *
     * @param dqo 查询条件
     * @return 查询的列表数据
     */
    PageInfoListResult<$!{tableInfo.name}Doo> queryList($!{tableInfo.name}Dqo dqo);

}


4、serviceImpl.java

##初始化定义
$!init
$!define

#save("/service/impl/$!module/", "ServiceImpl.java")
#setPackageSuffix("service.impl.$!module")


import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.powater.common.bean.PageInfoListResult;

#set($time=$!time.currTime())
#set($time=$time.substring(0,11))
#set($time=$time.replace("-","/"))
/**
 * <p>
 * $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
 * </p>
 * 
 * @author:$!author
 * @date:$!time
 */
@Service("/$!tool.firstLowerCase($!{tableInfo.name})Service")
@Transactional(rollbackFor = Exception.class)
public class $!{tableInfo.name}ServiceImpl implements $!{tableInfo.name}Service {

    @Autowired
    private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
    
    
    @Override
    public String addOrUpdate($!{tableInfo.name}Dio dio) {
        
        return null;
    }
    
    @Override
    public String del(String recordNo) {
    
        return null;
    }
    
    @Override
    public PageInfoListResult<$!{tableInfo.name}Doo> queryList($!{tableInfo.name}Dqo dqo) {
    
        return null;
    }

}


5、controller.java

##定义初始变量
$!init
$!define

#save("/controller/$!module/", "Controller.java")
#setPackageSuffix("controller.$!module")


import com.powater.common.bean.PageInfoListResult;
import com.powater.common.util.CommonResult;
import org.springframework.stereotype.Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

#set($time=$!time.currTime())
#set($time=$time.substring(0,11))
#set($time=$time.replace("-","/"))
/**
 * <p>
 * $!{tableInfo.comment}($!{tableInfo.name})表控制层
 * </p>
 * 
 * @author:$!author
 * @date:$!time
 */
@RestController
@RequestMapping("/soil/api/$!module/$!tool.firstLowerCase($tableInfo.name)")
@Api(description = "$!{tableInfo.name}Controller", tags = "$!{tableInfo.comment} —— 相关接口")
public class $!{tableInfo.name}Controller {

    @Autowired
    private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
    
    
    @ApiOperation(value = "新增-修改",httpMethod = "POST")
    @PostMapping(value = "/addOrUpdate")
    public CommonResult addOrUpdate(@RequestBody $!{tableInfo.name}Dio dio) {
         
        return null;
    }
    
    @ApiOperation(value = "删除",httpMethod = "GET")
    @GetMapping(value = "/del")
    public CommonResult del(@RequestParam(value = "recordNo") String recordNo) {
         
        return null;
    }
    
    @ApiOperation(value = "根据条件查询列表",httpMethod = "POST")
    @PostMapping(value = "/list")
    public CommonResult<PageInfoListResult<$!{tableInfo.name}Doo>> queryList(@RequestBody $!{tableInfo.name}Dqo dqo) {
         
        return null;
    }

}


6、mapper.xml

##引入mybatis支持
$!mybatisSupport
##初始化定义
$!init
$!define

##设置保存名称与保存位置
#save("/mapper/$!module","Dao.xml")
<?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.$!module.$!{tableInfo.name}Dao">

    <resultMap  id="BaseResultMap" type="$!{tableInfo.savePackageName}.entity.$!module.$!{tableInfo.name}">
#foreach($column in $tableInfo.fullColumn)
        <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
    </resultMap>
    
    <!-- 伪列 -->
    <sql id="Base_Column_List">
#foreach($column in $tableInfo.fullColumn)
        `$!column.obj.name`,
#end
    </sql>

    
    <!-- 通过ID查询单条数据 -->
    <select id="queryByRecordNo" resultMap="BaseResultMap">
        select
          <include refid="Base_Column_List"/>
        from $!tableInfo.obj.name 
        where record_no = #{recordNo} and del_flag = 0
    </select>
    
    <!-- 新增数据 -->
    <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
        insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)`$!column.obj.name`#if($velocityHasNext), #end#end)
        values (#foreach($column in $tableInfo.fullColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
    </insert>
    
    <!-- 新增选择列 -->
    <insert id="insertSelective" 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>
  
    <!-- 修改数据 -->
    <update id="update">
        update $!{tableInfo.obj.name}
        <set>
#foreach($column in $tableInfo.otherColumn)
            #if ($column.name.equals("recordNo"))#else
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
                `$!column.obj.name` = #{$!column.name},
            </if>
             #end
#end
            version = version + 1
        </set>
        where record_no = #{recordNo} and del_flag = 0 and version = #{version}
    </update>

    <!-- 通过主键删除数据(物理删除) -->
    <delete id="realDeleteByRecordNo">
        delete from $!{tableInfo.obj.name}  where record_no = #{recordNo}
    </delete>
    
    <!-- 通过主键删除数据(真实删除) -->
    <delete id="deleteByRecordNo">
        update $!{tableInfo.obj.name} set del_flag = 1 where record_no = #{recordNo} and del_flag = 0
    </delete>
    
  <!-- 批量新增 -->
  <insert id="batchInsert" parameterType="java.util.List">
        insert into $!{tableInfo.obj.name} (
        #foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end
        
        )
    values
    <foreach collection="recordList" index="index" item="item" separator=",">
      (
      #foreach($column in $tableInfo.fullColumn)#{item.$!{column.name}}#if($velocityHasNext), #end#end
      
      )
    </foreach>
  </insert>
  
  <select id="selectListByCondition" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from $!{tableInfo.obj.name}
        where del_flag = 0
  </select>
  
</mapper>


7、doo.java

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

##使用宏定义设置回调(保存位置与文件后缀)
#save("/pojo/doo/$!module", "Doo.java")

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

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

##使用宏定义实现类注释信息
#tableComment(" —— 列表信息")
@Data
@ApiModel(value = "$!{tableInfo.comment} —— 列表信息")
public class $!{tableInfo.name}Doo  {
##    private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
    
    @ApiModelProperty(value = "$!{column.comment}")
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
    #end
    
}


8、dio.java

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

##使用宏定义设置回调(保存位置与文件后缀)
#save("/pojo/dio/$!module", "Dio.java")

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

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

##使用宏定义实现类注释信息
#tableComment(" —— 输入信息")
@Data
@ApiModel(value = "$!{tableInfo.comment} —— 输入信息")
public class $!{tableInfo.name}Dio  {
##    private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
    
    @ApiModelProperty(value = "$!{column.comment}")
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
    #end
    
}


9、dqo.java

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

##使用宏定义设置回调(保存位置与文件后缀)
#save("/pojo/dqo/$!module", "Dqo.java")

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

##使用全局变量实现默认包导入
$!autoImport
import lombok.Data;
import com.powater.soilWaterManager.pojo.dqo.BaseDqo;

##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
public class $!{tableInfo.name}Dqo extends BaseDqo  {

}
说明:模板这里要说明的有两个
(一) #save,这个是默认模板中的一个宏定义,相当于一个函数,第一个参数是文件存放路径 "/"表示根路径的时候就是(问题3)中选择的path,第二个就是类名后缀,比如想要生成TestDao.java,这个时候参数就是“Dao.java”。
(二)#setPackageSuffix,这个是用于设置包路径的后缀,默认会使用(问题3)中选择的package,比如参数是entity,则java类中的package就是 你选择的包(没有则是.) + entity

三、全局变量(大多都是默认的)在这里插入图片描述
1、init

##初始化区域

 

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

##参考阿里巴巴开发手册,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)


2、define

##(Velocity宏定义)

##定义设置表名后缀的宏定义,调用方式:#setTableSuffix("Test")
#macro(setTableSuffix $suffix)
    #set($tableName = $!tool.append($tableInfo.name, $suffix))
#end

##定义设置包名后缀的宏定义,调用方式:#setPackageSuffix("Test")
#macro(setPackageSuffix $suffix)
#if($suffix!="")package #end#if($tableInfo.savePackageName!="")$!{tableInfo.savePackageName}.#{end}$!suffix;
#end

##定义直接保存路径与文件名简化的宏定义,调用方式:#save("/entity", ".java")
#macro(save $path $fileName)
    $!callback.setSavePath($tool.append($tableInfo.savePath, $path))
    $!callback.setFileName($tool.append($tableInfo.name, $fileName))
#end

##定义表注释的宏定义,调用方式:#tableComment("注释信息")
#macro(tableComment $desc)
/**
 * $!{tableInfo.comment}($!{tableInfo.name})$desc
 *
 * @author $!author
 * @since $!time.currTime()
 */
#end

##定义GET,SET方法的宏定义,调用方式:#getSetMethod($column)
#macro(getSetMethod $column)

    public $!{tool.getClsNameByFullName($column.type)} get$!{tool.firstUpperCase($column.name)}() {
        return $!{column.name};
    }

    public void set$!{tool.firstUpperCase($column.name)}($!{tool.getClsNameByFullName($column.type)} $!{column.name}) {
        this.$!{column.name} = $!{column.name};
    }
#end


3、autoImport

##自动导入包(仅导入实体属性需要的包,通常用于实体类)
#foreach($import in $importList)
import $!import;
#end
1
2
3
4
4、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="TINYINT")
    #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="BIGINT")
    #elseif($tool.newHashSet("java.lang.Float", "float", "java.lang.Double", "double").contains($column.type))
        #set($jdbcType="DOUBLE")
    #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

4、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="TINYINT")
    #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="BIGINT")
    #elseif($tool.newHashSet("java.lang.Float", "float", "java.lang.Double", "double").contains($column.type))
        #set($jdbcType="DOUBLE")
    #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


5、author (这个是自己定义的)

ningyewansui


6、module (这个是自己定义的)

tb


最后:根据自己需求来修改模板,模仿default分组的模板来修改,可以参考插件给的说明

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值