Easycode是idea的一个插件,可以直接对数据的表生成entity,controller,service,dao,mapper,无需任何编码,简单而强大。

安装教程搜索一下就行,官网自带的模板功能所有欠缺,自定义的一个模板。

EasyCode Template属性_idea EasyCode模板

注意:

1.增,批量增,删,改,单查,筛选查询

2.lombok实现,如果没有lombok自己更改文件或安装lombok

一、dao.java模板

##定义初始变量
#set($tableName = $tool.append($, "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.$!{};
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

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

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

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

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

    	/**
     * 批量新增
     *
     * @param $!tool.firstLowerCase($!{}) 实例对象的集合
     * @return 影响行数
     */
	int batchInsert(List<$!{}> $!tool.firstLowerCase($!{}));

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

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

}

二、mapper.xml模板

##引入mybatis支持
$!mybatisSupport

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

    <resultMap type="$!{tableInfo.savePackageName}.entity.$!{}" id="$!{}Map">
#foreach($column in $tableInfo.fullColumn)
        <result property="$!" column="$!" jdbcType="$!column.ext.jdbcType"/>
#end
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        #allSqlColumn()
        
    </sql>

    <!--查询单个-->
    <select id="queryById" resultMap="$!{}Map">
        select
          <include refid="Base_Column_List" />
        from $!
        where $! = #{$!}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="$!{}Map">
        select
          <include refid="Base_Column_List" />
        from $!
        <where>
#foreach($column in $tableInfo.fullColumn)
            <if test="$! != null#if($column.type.equals("java.lang.String")) and $! != ''#end">
                and $! = #{$!}
            </if>
#end
        </where>
    </select>

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


     <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into $!{}(#foreach($column in $tableInfo.fullColumn)$!#if($velocityHasNext), #end#end)
        values 
        <foreach collection="list" item="stu" separator=",">
        (
            #foreach($column in $tableInfo.fullColumn)
            #{stu.$!{}}#if($velocityHasNext), #end#end
            
         )
         </foreach>
    </insert>

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

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from $!{} where $! = #{$!}
    </delete>

</mapper>

 三:entity.java模板

##引入宏定义
$!define

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

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

##使用全局变量实现默认包导入
$!autoImport
import java.io.Serializable;
import lombok.*;
##
##使用宏定义实现类注释信息
/**
 * $!{tableInfo.comment}($!{})实体类
 * @author shenning
 * @description 
 * @since $!time.currTime()
 */
@Getter
@Setter
@ToString
public class $!{} implements Serializable {
    private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)

    #if(${column.comment})/*** ${column.comment} */#end
    private $!{tool.getClsNameByFullName($column.type)} $!{};
##    private $!{tool.getClsNameByFullName($column.type)} $!{tool.firstUpperCase($)};
#end


##若没有使用lombok插件,该段不要注释,按照默认的模板
###foreach($column in $tableInfo.fullColumn)
####使用宏定义实现get,set方法
###getSetMethod($column)
###end

}

四:service.java模板

##定义初始变量
#set($tableName = $tool.append($, "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.$!{};
import java.util.List;
import com.github.pagehelper.PageInfo;



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

    //通过ID查询
    $!{} queryById($!pk.shortType $!);

    //通过实体作为筛选条件查询
    List<$!{}> queryAll($!{} $!tool.firstLowerCase($!{}));

    //新增数据
    int insert($!{} $!tool.firstLowerCase($!{}));

    //修改数据
    int updateById($!{} $!tool.firstLowerCase($!{}));

    //通过主键id删除数据
    int deleteById($!pk.shortType $!);

}

 五:serivceImpl.java

##定义初始变量
#set($tableName = $tool.append($, "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.$!{};
import $!{tableInfo.savePackageName}.dao.$!{}Dao;
import $!{tableInfo.savePackageName}.service.$!{}Service;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

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


 /**
 * $!{tableInfo.comment}($!{})表服务实现类
 * @author shenning
 * @description 
 * @since $!time.currTime()
 */
@Service
@Transactional 
public class $!{tableName} implements $!{}Service {
    @Autowired
    private $!{}Dao $!tool.firstLowerCase($!{})Dao;

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

    /**
     * 查询多条数据
     * @param $!tool.firstLowerCase($!{}) 实例对象
     * @return 对象列表
     */
    @Override
    public List<$!{}> queryAll($!{} $!tool.firstLowerCase($!{})) {
        //PageHelper.startPage(pageNum,pageSize);
        List<$!{}> dataList = $!{tool.firstLowerCase($!{})}Dao.queryAll($!tool.firstLowerCase($!{}));
        //PageInfo<$!{}> page = new PageInfo<$!{}>(dataList);
        return dataList;
    }

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

    /**
     * 修改数据
     * @param $!tool.firstLowerCase($!{}) 实例对象
     * @return 实例对象
     */
    @Override
    public int updateById($!{} $!tool.firstLowerCase($!{})) {
        return $!{tool.firstLowerCase($!{})}Dao.updateById($!tool.firstLowerCase($!{}));
    }

    /**
     * 通过主键id删除数据
     * @param $! 主键
     */
    @Override
    public int deleteById($!pk.shortType $!) {
        return $!{tool.firstLowerCase($!{})}Dao.deleteById($!);
    }
}

六:controller.java模板

##定义初始变量
#set($tableName = $tool.append($, "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}.service.$!{}Service;
import $!{tableInfo.savePackageName}.entity.$!{};
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import javax.annotation.Resource;

/**
 * $!{tableInfo.comment}($!{})表控制层
 * @author shenning
 * @description 
 * @since $!time.currTime()
 */
@RestController
@RequestMapping("$!tool.firstLowerCase($)")
public class $!{tableName} {
    /**
     * $!{tableInfo.comment}服务对象
     */
    @Autowired
    private $!{}Service $!tool.firstLowerCase($)Service;

    /**
     * 通过主键查询单条数据
     */
    @GetMapping("get$!tool.firstUpperCase($)One")
    public $!{} get$!tool.firstUpperCase($)One($!pk.shortType id) {
        return $!{tool.firstLowerCase($)}Service.queryById(id);
    }
    
    /**
     * 通过查询条件筛选数据
     */
    @GetMapping("get$!tool.firstUpperCase($)List")
    public List<$!{}> get$!tool.firstUpperCase($)List($!{} $!tool.firstLowerCase($!{})) {
         List<$!{}> getList=$!{tool.firstLowerCase($)}Service.queryAll($!tool.firstLowerCase($!{}));
         return getList;
    }
    
    /**
     * 新增数据$!{tableInfo.comment}
     */
    @PostMapping("add$!tool.firstUpperCase($)")
    public int add$!tool.firstUpperCase($!{})(@RequestBody $!{} $!tool.firstLowerCase($!{})) {
        int addState=$!{tool.firstLowerCase($)}Service.insert($!tool.firstLowerCase($!{}));
        return addState;
    }
    
    /**
     * 修改数据$!{tableInfo.comment}
     */
    @GetMapping("update$!tool.firstUpperCase($)")
    public int update$!tool.firstUpperCase($!{})(@RequestBody $!{} $!tool.firstLowerCase($!{})) {
         int updateState=$!{tool.firstLowerCase($)}Service.updateById($!tool.firstLowerCase($!{}));
         return updateState;
    }
    
    /**
     * 删除数据$!{tableInfo.comment}
     */
    @GetMapping("delete$!tool.firstUpperCase($)")
    public int delete$!tool.firstUpperCase($!{})($!pk.shortType id) {
         int deleteState=$!{tool.firstLowerCase($)}Service.deleteById(id);
         return deleteState;
    }
}