easycode自定义模板转载
easycode自定義模板
程序員s 2020-09-25 11:07:26 ?916 ?已收藏 6
分類專欄: -----【idea】 文章標簽: easycode自定義模板 easycode模板 easycode插件模板 idea EasyCode模板 easycode使用.
版權
Easycode是idea的一個插件,可以直接對數據的表生成entity,controller,service,dao,mapper,無需任何編碼,簡單而強大。
安裝教程搜索一下就行,官網自帶的模板功能所有欠缺,自定義的一個模板。
注意:
1.增,批量增,刪,改,單查,篩選查詢
2.lombok實現,如果沒有lombok自己更改文件或安裝lombok
一、dao.java模板
##定義初始變量
#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 org.apache.ibatis.annotations.Mapper;
import java.util.List;
?
/**
?* $!{tableInfo.comment}($!{tableInfo.name})表數據庫訪問層
?* @author shenning
?* @description?
?* @since $!time.currTime()
?*/
@Mapper
public interface $!{tableName} {
?
? ? /**
? ? ?* 通過ID查詢單條數據
? ? ?*
? ? ?* @param $!pk.name 主鍵
? ? ?* @return 實例對象
? ? ?*/
? ? $!{tableInfo.name} queryById($!pk.shortType $!pk.name);
?
? ?
? ? /**
? ? ?* 通過實體作為篩選條件查詢
? ? ?*
? ? ?* @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}));
?
? ? ?? ?/**
? ? ?* 批量新增
? ? ?*
? ? ?* @param $!tool.firstLowerCase($!{tableInfo.name}) 實例對象的集合
? ? ?* @return 影響行數
? ? ?*/
?? ?int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name}));
?
? ? /**
? ? ?* 修改數據
? ? ?*
? ? ?* @param $!tool.firstLowerCase($!{tableInfo.name}) 實例對象
? ? ?* @return 影響行數
? ? ?*/
? ? int updateById($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
?
? ? /**
? ? ?* 通過主鍵刪除數據
? ? ?*
? ? ?* @param $!pk.name 主鍵
? ? ?* @return 影響行數
? ? ?*/
? ? int deleteById($!pk.shortType $!pk.name);
?
}
二、mapper.xml模板
##引入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 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>
?
? ? <!--查詢單個-->
? ? <select id="queryById" resultMap="$!{tableInfo.name}Map">
? ? ? ? select
? ? ? ? ? <include refid="Base_Column_List" />
? ? ? ? from $!tableInfo.obj.name
? ? ? ? where $!pk.obj.name = #{$!pk.name}
? ? </select>
?
? ? <!--通過實體作為篩選條件查詢-->
? ? <select id="queryAll" resultMap="$!{tableInfo.name}Map">
? ? ? ? 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>
?
? ? <!--新增所有列-->
? ? <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="batchInsert">
? ? ? ? insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end)
? ? ? ? values?
? ? ? ? <foreach collection="list" item="stu" separator=",">
? ? ? ? (
? ? ? ? ? ? #foreach($column in $tableInfo.fullColumn)
? ? ? ? ? ? #{stu.$!{column.name}}#if($velocityHasNext), #end#end
? ? ? ? ? ??
? ? ? ? ?)
? ? ? ? ?</foreach>
? ? </insert>
?
? ? <!--通過主鍵修改數據-->
? ? <update id="updateById">
? ? ? ? update $!{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.name} where $!pk.obj.name = #{$!pk.name}
? ? </delete>
?
</mapper>
?三:entity.java模板
##引入宏定義
$!define
##使用宏定義設置回調(保存位置與文件后綴)
#save("/entity", ".java")
##使用宏定義設置包后綴
#setPackageSuffix("entity")
##使用全局變量實現默認包導入
$!autoImport
import java.io.Serializable;
import lombok.*;
##
##使用宏定義實現類注釋信息
/**
?* $!{tableInfo.comment}($!{tableInfo.name})實體類
?* @author shenning
?* @description?
?* @since $!time.currTime()
?*/
@Getter
@Setter
@ToString
public class $!{tableInfo.name} 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)} $!{column.name};
## ? ?private $!{tool.getClsNameByFullName($column.type)} $!{tool.firstUpperCase($column.name)};
#end
?
##若沒有使用lombok插件,該段不要注釋,按照默認的模板
###foreach($column in $tableInfo.fullColumn)
####使用宏定義實現get,set方法
###getSetMethod($column)
###end
?
}
四:service.java模板
##定義初始變量
#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;
import com.github.pagehelper.PageInfo;
?
?
?
?/**
?* $!{tableInfo.comment}($!{tableInfo.name})表服務接口
?* @author shenning
?* @description?
?* @since $!time.currTime()
?*/
public interface $!{tableName} {
?
? ? //通過ID查詢
? ? $!{tableInfo.name} queryById($!pk.shortType $!pk.name);
?
? ? //通過實體作為篩選條件查詢
? ? List<$!{tableInfo.name}> queryAll($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
?
? ? //新增數據
? ? int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
?
? ? //修改數據
? ? int updateById($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
?
? ? //通過主鍵id刪除數據
? ? int deleteById($!pk.shortType $!pk.name);
?
}
?五:serivceImpl.java
##定義初始變量
#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}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}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}($!{tableInfo.name})表服務實現類
?* @author shenning
?* @description?
?* @since $!time.currTime()
?*/
@Service
@Transactional?
public class $!{tableName} implements $!{tableInfo.name}Service {
? ? @Autowired
? ? private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
?
? ? /**
? ? ?* 通過ID查詢單條數據
? ? ?* @param $!pk.name 主鍵
? ? ?* @return 實例對象
? ? ?*/
? ? @Override
? ? public $!{tableInfo.name} queryById($!pk.shortType $!pk.name) {
? ? ? ? return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.queryById($!pk.name);
? ? }
?
? ? /**
? ? ?* 查詢多條數據
? ? ?* @param $!tool.firstLowerCase($!{tableInfo.name}) 實例對象
? ? ?* @return 對象列表
? ? ?*/
? ? @Override
? ? public List<$!{tableInfo.name}> queryAll($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
? ? ? ? //PageHelper.startPage(pageNum,pageSize);
? ? ? ? List<$!{tableInfo.name}> dataList = $!{tool.firstLowerCase($!{tableInfo.name})}Dao.queryAll($!tool.firstLowerCase($!{tableInfo.name}));
? ? ? ? //PageInfo<$!{tableInfo.name}> page = new PageInfo<$!{tableInfo.name}>(dataList);
? ? ? ? return dataList;
? ? }
?
? ? /**
? ? ?* 新增數據
? ? ?* @param $!tool.firstLowerCase($!{tableInfo.name}) 實例對象
? ? ?* @return 實例對象
? ? ?*/
? ? @Override
? ? public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
? ? ? ? return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name}));
? ? }
?
? ? /**
? ? ?* 修改數據
? ? ?* @param $!tool.firstLowerCase($!{tableInfo.name}) 實例對象
? ? ?* @return 實例對象
? ? ?*/
? ? @Override
? ? public int updateById($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
? ? ? ? return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.updateById($!tool.firstLowerCase($!{tableInfo.name}));
? ? }
?
? ? /**
? ? ?* 通過主鍵id刪除數據
? ? ?* @param $!pk.name 主鍵
? ? ?*/
? ? @Override
? ? public int deleteById($!pk.shortType $!pk.name) {
? ? ? ? return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name);
? ? }
}
六:controller.java模板
##定義初始變量
#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}.service.$!{tableInfo.name}Service;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import javax.annotation.Resource;
?
/**
?* $!{tableInfo.comment}($!{tableInfo.name})表控制層
?* @author shenning
?* @description?
?* @since $!time.currTime()
?*/
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
? ? /**
? ? ?* $!{tableInfo.comment}服務對象
? ? ?*/
? ? @Autowired
? ? private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
?
? ? /**
? ? ?* 通過主鍵查詢單條數據
? ? ?*/
? ? @GetMapping("get$!tool.firstUpperCase($tableInfo.name)One")
? ? public $!{tableInfo.name} get$!tool.firstUpperCase($tableInfo.name)One($!pk.shortType id) {
? ? ? ? return $!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id);
? ? }
? ??
? ? /**
? ? ?* 通過查詢條件篩選數據
? ? ?*/
? ? @GetMapping("get$!tool.firstUpperCase($tableInfo.name)List")
? ? public List<$!{tableInfo.name}> get$!tool.firstUpperCase($tableInfo.name)List($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
? ? ? ? ?List<$!{tableInfo.name}> getList=$!{tool.firstLowerCase($tableInfo.name)}Service.queryAll($!tool.firstLowerCase($!{tableInfo.name}));
? ? ? ? ?return getList;
? ? }
? ??
? ? /**
? ? ?* 新增數據$!{tableInfo.comment}
? ? ?*/
? ? @PostMapping("add$!tool.firstUpperCase($tableInfo.name)")
? ? public int add$!tool.firstUpperCase($!{tableInfo.name})(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
? ? ? ? int addState=$!{tool.firstLowerCase($tableInfo.name)}Service.insert($!tool.firstLowerCase($!{tableInfo.name}));
? ? ? ? return addState;
? ? }
? ??
? ? /**
? ? ?* 修改數據$!{tableInfo.comment}
? ? ?*/
? ? @GetMapping("update$!tool.firstUpperCase($tableInfo.name)")
? ? public int update$!tool.firstUpperCase($!{tableInfo.name})(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
? ? ? ? ?int updateState=$!{tool.firstLowerCase($tableInfo.name)}Service.updateById($!tool.firstLowerCase($!{tableInfo.name}));
? ? ? ? ?return updateState;
? ? }
? ??
? ? /**
? ? ?* 刪除數據$!{tableInfo.comment}
? ? ?*/
? ? @GetMapping("delete$!tool.firstUpperCase($tableInfo.name)")
? ? public int delete$!tool.firstUpperCase($!{tableInfo.name})($!pk.shortType id) {
? ? ? ? ?int deleteState=$!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id);
? ? ? ? ?return deleteState;
? ? }
}
?
————————————————
版權聲明:本文為CSDN博主「程序員s」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/u012448904/article/details/108792178
總結
以上是生活随笔為你收集整理的easycode自定义模板转载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑桌面美化
- 下一篇: java lombok