EasyCode模闆
- 說明
- 一、Entity
- 二、Dao
- 三、Mapper(xml)
- 四、Service
- 五、ServiceImpl
- 六、Controller
- 六、Result 與 CodeEnum
說明
模闆提示:模闆包括controller,service,serviceImpl,dao(mapper),mapper.xml 六個部分。
接口詳細提示:其中接口有配套根據id查詢實體,根具實體作為篩選條件查詢,分頁查詢(帶時間段查詢),新增,修改,删除。
生成提示:建議生成順序為entrty–>Dao–>Mapper(xml)–>Service–>ServiceImpl–>Controller
其他提示:controller接口傳回統一Result實體傳回,在代碼最後。Result實體使用了Lombok,需要提前安裝。生成之後需要自行每個導包,為了導包能夠正确,插件開源文檔學習位址:https://gitee.com/makejava/EasyCode/wikis/pages;
提示:以下是本篇文章正文内容,下面案例可供參考,也給自己插個眼
一、Entity
##引入宏定義
$!define
##使用宏定義設定回調(儲存位置與檔案字尾)
#save("/entity", ".java")
##使用宏定義設定包字尾
#setPackageSuffix("entity")
##使用宏定義實作類注釋資訊
#tableComment("實體類")
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};
#end
#foreach($column in $tableInfo.fullColumn)
##使用宏定義實作get,set方法
#getSetMethod($column)
#end
}
二、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
/**
* $!{tableInfo.comment}($!{tableInfo.name})表資料庫通路層
*
* @author wut
* @since $!time.currTime()
*/
@Mapper
public interface $!{tableName} {
/**
* 通過ID查詢單條資料
*
* @param $!pk.name 主鍵
* @return 執行個體對象
*/
$!{tableInfo.name} selectById($!pk.shortType $!pk.name);
/**
* 分頁查詢
*
* @param pageNo 查詢起始位置
* @param pageSize 查詢條數
* @param $!tool.firstLowerCase($!{tableInfo.name}) 實體篩選條件
* @param beginDateScope 查詢時間段
* @return 對象清單
*/
List<$!{tableInfo.name}> selectByPage(@Param("pageNo") Integer pageNo, @Param("pageSize") Integer pageSize,
@Param("$!tool.firstLowerCase($!{tableInfo.name})") $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}),@Param("beginDateScope") Date[] beginDateScope);
/**
* 分頁查詢總資料數
*
* @return 資料總數
*/
Long getTotal(@Param("$!tool.firstLowerCase($!{tableInfo.name})") $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}),@Param("beginDateScope") Date[] beginDateScope);
/**
* 通過實體作為篩選條件查詢
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 對象清單
*/
List<$!{tableInfo.name}> selectList($!{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 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}, "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>
<!-- 基本字段 -->
<sql id="Base_Column_List">
#allSqlColumn()
</sql>
<!-- 基本查詢條件 -->
<sql id="Query_Items">
#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
</sql>
<!-- 查詢單個 -->
<select id="selectById" resultMap="$!{tableInfo.name}Map">
select
<include refid="Base_Column_List" />
from $!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</select>
<!-- 分頁查詢 -->
<select id="selectByPage" resultMap="$!{tableInfo.name}Map">
select
<include refid="Base_Column_List" />
from $!tableInfo.obj.name
<where>
<if test="$!tool.firstLowerCase($!{tableInfo.name}) != null" >
#foreach($column in $tableInfo.fullColumn)
<if test="$!tool.firstLowerCase($!{tableInfo.name}).$!column.name != null#if($column.type.equals("java.lang.String")) and $!tool.firstLowerCase($!{tableInfo.name}).$!column.name != ''#end">
and $!column.obj.name = #{$!tool.firstLowerCase($!{tableInfo.name}).$!column.name}
</if>
#end
<if test="beginDateScope !=null">
and create_time between #{beginDateScope[0]} and #{beginDateScope[1]}
</if>
</if>
</where>
<if test="pageNo !=null and pageSize!=null">
limit #{pageNo},#{pageSize}
</if>
</select>
<!-- 分頁查詢總資料數 -->
<select id="getTotal" resultType="java.lang.Long">
select count(*) from $!{tableInfo.obj.name}
<where>
<if test="$!tool.firstLowerCase($!{tableInfo.name}) != null" >
#foreach($column in $tableInfo.fullColumn)
<if test="$!tool.firstLowerCase($!{tableInfo.name}).$!column.name != null#if($column.type.equals("java.lang.String")) and $!tool.firstLowerCase($!{tableInfo.name}).$!column.name != ''#end">
and $!column.obj.name = #{$!tool.firstLowerCase($!{tableInfo.name}).$!column.name}
</if>
#end
<if test="beginDateScope !=null">
and create_time between #{beginDateScope[0]} and #{beginDateScope[1]}
</if>
</if>
</where>
</select>
<!--通過實體作為篩選條件查詢-->
<select id="selectList" resultMap="$!{tableInfo.name}Map">
select
<include refid="Base_Column_List" />
from $!tableInfo.obj.name
<where>
<include refid="Query_Items" />
</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>
<!-- 通過主鍵修改資料 -->
<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>
四、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
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服務接口
*
* @author wut
* @since $!time.currTime()
*/
public interface $!{tableName} {
/**
* 通過ID查詢單條資料
*
* @param $!pk.name 主鍵
* @return 執行個體對象
*/
$!{tableInfo.name} selectById($!pk.shortType $!pk.name);
/**
* 分頁查詢
*
* @param pageNo 查詢起始位置
* @param pageSize 查詢條數
* @param $!tool.firstLowerCase($!{tableInfo.name}) 實體篩選條件
* @param beginDateScope 查詢時間段
* @return 對象清單
*/
List<$!{tableInfo.name}> selectByPage(Integer pageNo, Integer pageSize,
$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}), Date[] beginDateScope);
/**
* 分頁查詢總資料數
*
* @return 資料總數
*/
Long getTotal($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}), Date[] beginDateScope);
/**
* 通過實體作為篩選條件查詢
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 對象清單
*/
List<$!{tableInfo.name}> selectList($!{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 updateById($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 通過主鍵删除資料
*
* @param $!pk.name 主鍵
* @return 影響行數
*/
int deleteById($!pk.shortType $!pk.name);
}
五、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
/**
* $!{tableInfo.comment}($!{tableInfo.name}表)服務實作類
*
* @author wut
* @since $!time.currTime()
*/
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} implements $!{tableInfo.name}Service {
@Autowired
private $!{tableInfo.name}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper;
/**
* 通過ID查詢單條資料
*
* @param $!pk.name 主鍵
* @return 執行個體對象
*/
@Override
public $!{tableInfo.name} selectById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.selectById($!pk.name);
}
/**
* 分頁查詢
*
* @param pageNo 查詢起始位置
* @param pageSize 查詢條數
* @return 對象清單
*/
@Override
public List<$!{tableInfo.name}> selectByPage(Integer pageNo, Integer pageSize,
$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}), Date[] beginDateScope) {
if (pageNo != null && pageSize != null) {
pageNo = (pageNo - 1) * pageSize;
}
return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.selectByPage(pageNo, pageSize, $!tool.firstLowerCase($!{tableInfo.name}), beginDateScope);
}
/**
* 查詢總資料數
*
* @return 資料總數
*/
@Override
public Long getTotal($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}), Date[] beginDateScope){
return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.getTotal($!tool.firstLowerCase($!{tableInfo.name}), beginDateScope);
}
/**
* 根據條件查詢
*
* @return 執行個體對象的集合
*/
@Override
public List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!{tool.firstLowerCase($!{tableInfo.name})}) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.selectList($!{tool.firstLowerCase($!{tableInfo.name})});
}
/**
* 新增資料
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 執行個體對象
*/
@Override
public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.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})}Mapper.updateById($!tool.firstLowerCase($!{tableInfo.name}));
}
/**
* 通過主鍵删除資料
*
* @param $!pk.name 主鍵
* @return 是否成功
*/
@Override
public int deleteById($!pk.shortType $!pk.name) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.deleteById($!pk.name);
}
}
六、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
/**
* $!{tableInfo.comment}($!{tableInfo.name})控制層
*
* @author wut
* @since $!time.currTime()
*/
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
@Autowired
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 通過主鍵查詢單條資料
*
* @param $!pk.name 主鍵
* @return 單條資料
*/
@RequestMapping(value = "get", method = RequestMethod.GET)
public Result<$tableInfo.name> selectOneById($!pk.shortType $!pk.name) {
$tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}Service.selectById($!pk.name);
return result != null ? Result.succeed("查詢成功", result) : Result.failed("查詢失敗");
}
/**
* 新增一條資料
*
* @param $!tool.firstLowerCase($tableInfo.name) 實體類
* @return Result對象
*/
@RequestMapping(value = "insert", method = RequestMethod.POST)
public Result insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
int i = $!{tool.firstLowerCase($tableInfo.name)}Service.insert($!tool.firstLowerCase($tableInfo.name));
return i > 0 ? Result.succeed("新增成功") : Result.failed("新增失敗");
}
/**
* 修改一條資料
*
* @param $!tool.firstLowerCase($tableInfo.name) 實體類
* @return Result對象
*/
@RequestMapping(value = "update", method = RequestMethod.POST)
public Result update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
int result = $!{tool.firstLowerCase($tableInfo.name)}Service.updateById($!tool.firstLowerCase($tableInfo.name));
return result > 0 ? Result.succeed("修改成功",result) : Result.failed("修改失敗");
}
/**
* 删除一條資料
*
* @param $!pk.name 主鍵
* @return Result對象
*/
@RequestMapping(value = "delete", method = RequestMethod.DELETE)
public Result<$tableInfo.name> delete($!pk.shortType $!pk.name) {
int result = $!{tool.firstLowerCase($tableInfo.name)}Service.deleteById($!pk.name);
return result > 0 ? Result.succeed("删除成功", null) : Result.failed("删除失敗");
}
/**
* 分頁查詢
*
* @param pageNo 查詢起始位置
* @param pageSize 查詢條數
* @param $!tool.firstLowerCase($!{tableInfo.name}) 實體篩選條件
* @param beginDateScope 查詢時間段
* @return 對象清單
*/
@RequestMapping(value = "selectPage", method = RequestMethod.GET)
public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNo,
@RequestParam(defaultValue = "10") Integer pageSize,
$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}), Date[] beginDateScope){
List<$tableInfo.name> $!tool.firstLowerCase($tableInfo.name)s = $!{tool.firstLowerCase($tableInfo.name)}Service.selectByPage(pageNo, pageSize, $!tool.firstLowerCase($!{tableInfo.name}), beginDateScope);
Long total = $!{tool.firstLowerCase($tableInfo.name)}Service.getTotal($!tool.firstLowerCase($!{tableInfo.name}), beginDateScope);
//自行調整分頁傳回資料整理,我這裡預設傳回分頁清單
return $!tool.firstLowerCase($tableInfo.name)s != null ? Result.succeed("查詢成功", $!tool.firstLowerCase($tableInfo.name)s) : Result.failed("查詢失敗");
}
}
六、Result 與 CodeEnum
package com.example.fast.common.web;
import com.example.fast.common.enums.CodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author wut
* @Description 前端傳回統一格式
* @create 2020/9/18
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
private T datas;
private Integer resp_code;
private String resp_msg;
public static <T> Result<T> succeed(String msg) {
return succeedWith(null, CodeEnum.SUCCESS.getCode(),msg);
}
public static <T> Result<T> succeed( String msg, T model) {
return succeedWith(model, CodeEnum.SUCCESS.getCode(),msg);
}
public static <T> Result<T> succeedWith(T datas, Integer code,String msg) {
return new Result<T>(datas, code, msg);
}
public static <T> Result<T> failed(String msg) {
return failedWith(null, CodeEnum.ERROR.getCode(), msg);
}
public static <T> Result<T> failed(T model,String msg) {
return failedWith(model, CodeEnum.ERROR.getCode(), msg);
}
public static <T> Result<T> failedWith(T datas, Integer code, String msg) {
return new Result<T>( datas, code, msg);
}
}
package com.example.fast.common.enums;
/**
* @author wut
* @Description 狀态碼
* @create 2020/9/18
*/
public enum CodeEnum {
SUCCESS(0),
ERROR(1);
private Integer code;
CodeEnum(Integer code){
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
提示:參考文章位址:
- https://blog.csdn.net/qq_35385687/article/details/107500982
- https://blog.csdn.net/weixin_43928962/article/details/100052274?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param