如下配置,可自動生成entity、controller、service、serviceImpl、dao、mapper檔案
實作簡單的CRUD接口
1、entity.java
##引入宏定義
$!define
##使用宏定義設定回調(儲存位置與檔案字尾)
#save("/model", ".java")
##使用宏定義設定包字尾
#setPackageSuffix("model")
##使用全局變量實作預設包導入
$!autoImport
import java.io.Serializable;
import lombok.Data;
##使用宏定義實作類注釋資訊
#tableComment("實體類")
@Data
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
}
2、controller.java
##定義初始變量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##設定回調
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller/$!tool.firstLowerCase($!{tableInfo.name})"))
##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller.$!{tool.firstLowerCase($!{tableInfo.name})};
import com.zjimee.boot.core.vo.requestParam.CommonRequest;
import com.zjimee.boot.rest.common.ResultCode;
import com.zjimee.boot.rest.constant.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zjimee.boot.core.model.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tool.firstLowerCase($!{tableInfo.name})}.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;
import com.zjimee.boot.rest.common.ResultCode;
import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制層
*
* @author wangj
* @since $!time.currTime()
*/
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 服務對象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 通過主鍵查詢單條資料
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 單條資料
*/
@GetMapping("/selectOne")
public ResultCode selectOne($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
try {
return this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById($!tool.firstLowerCase($!{tableInfo.name}));
} catch (Exception e) {
return new ResultCode(StatusCode.Exception, "查詢資訊異常");
}
}
/**
* 條件查詢
*
* @param request commonRequest
* @return 單條資料
*/
@PostMapping("/queryList")
public ResultCode queryList(HttpServletRequest request,@RequestBody CommonRequest commonRequest) {
try {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Service.queryList(request,commonRequest);
} catch (Exception e) {
return new ResultCode(StatusCode.Exception, "查詢資訊異常");
}
}
/**
* 新增資訊
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return
*/
@PostMapping("/insert")
public ResultCode insert(HttpServletRequest request,@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
try {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Service.insert(request,$!tool.firstLowerCase($!{tableInfo.name}));
} catch (Exception e) {
return new ResultCode(StatusCode.Exception, "新增異常");
}
}
/**
* 修改資訊
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return
*/
@PostMapping("/update")
public ResultCode update(HttpServletRequest request,@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
try {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Service.update(request,$!tool.firstLowerCase($!{tableInfo.name}));
} catch (Exception e) {
return new ResultCode(StatusCode.Exception, "修改異常");
}
}
/**
* 批量删除資訊
*
* @param commonRequest 删除類的id集合
* @return
*/
@PostMapping("/deleteByIds")
public ResultCode deleteByIds(HttpServletRequest request,@RequestBody CommonRequest commonRequest) {
try {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Service.deleteByIds(request,commonRequest.getIds());
} catch (Exception e) {
return new ResultCode(StatusCode.Exception, "删除異常");
}
}
}
3、service.java
##定義初始變量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##設定回調
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/$!tool.firstLowerCase($!{tableInfo.name})"))
##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.$!{tool.firstLowerCase($!{tableInfo.name})};
import com.zjimee.boot.core.model.$!{tableInfo.name};
import com.zjimee.boot.rest.common.ResultCode;
import java.util.List;
import com.zjimee.boot.core.vo.requestParam.CommonRequest;
import javax.servlet.http.HttpServletRequest;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服務接口
*
* @author wangj
* @since $!time.currTime()
*/
public interface $!{tableName} {
/**
* 通過ID查詢單條資料
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 執行個體對象
*/
ResultCode queryById($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 條件查詢資料
*
* @param request ,commonRequest執行個體對象
* @return 對象清單
*/
ResultCode queryList(HttpServletRequest request,CommonRequest commonRequest);
/**
* 新增資料
*
* @param request $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 執行個體對象
*/
ResultCode insert(HttpServletRequest request,$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 修改資料
*
* @param request $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 執行個體對象
*/
ResultCode update(HttpServletRequest request,$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 批量删除資料
*
* @param request ids
* @return 是否成功
*/
ResultCode deleteByIds(HttpServletRequest request,List<String> ids);
}
4、serviceImpl.java
##定義初始變量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##設定回調
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/$!tool.firstLowerCase($!{tableInfo.name})/impl"))
##拿到主鍵
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.$!{tool.firstLowerCase($!{tableInfo.name})}.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjimee.boot.rest.constant.StatusCode;
import com.zjimee.boot.core.model.$!{tableInfo.name};
import com.zjimee.boot.core.dao.$!{tableInfo.name}DAO;
import $!{tableInfo.savePackageName}.service.$!{tool.firstLowerCase($!{tableInfo.name})}.$!{tableInfo.name}Service;
import com.zjimee.boot.rest.common.ResultCode;
import org.springframework.stereotype.Service;
import com.zjimee.boot.core.util.EmptyUtil;
import com.zjimee.boot.core.util.StringUtil;
import com.zjimee.boot.core.util.Util;
import javax.servlet.http.HttpServletRequest;
import com.zjimee.boot.core.vo.requestParam.CommonRequest;
import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.UUID;
import com.zjimee.boot.rest.modular.auth.util.JwtTokenUtil;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服務實作類
*
* @author wangj
* @since $!time.currTime()
*/
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} implements $!{tableInfo.name}Service {
@Resource
private $!{tableInfo.name}DAO $!tool.firstLowerCase($!{tableInfo.name})Dao;
@Autowired
private JwtTokenUtil tokenUtil;
/**
* 通過ID查詢單條資料
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 執行個體對象
*/
@Override
public ResultCode queryById($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
if (EmptyUtil.isEmpty($!tool.firstLowerCase($!{tableInfo.name}))) {
return new ResultCode(StatusCode.REQ_PARAM_EMPTY.getCode(), "請求參數為空");
}
return new ResultCode(StatusCode.SUCCESS.getCode(), "查詢一條記錄成功", this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectById($!{tool.firstLowerCase($!{tableInfo.name})}.getId()));
}
/**
* 條件查詢資料
*@param request,commonRequest 執行個體對象
* @return 對象清單
*/
@Override
public ResultCode queryList(HttpServletRequest request,@RequestBody CommonRequest commonRequest) {
if (EmptyUtil.isEmpty(commonRequest)) {
return new ResultCode(StatusCode.REQ_PARAM_EMPTY.getCode(), "請求參數為空");
}
PageHelper.startPage(commonRequest.getPageNumber(),commonRequest.getPageSize(), commonRequest.getOrderBy());
List<$!{tableInfo.name}> list =this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.queryList(commonRequest);
Map<String, Object> resultMap = new HashMap<>();
PageInfo<$!{tableInfo.name}> PI = new PageInfo<>(list);
resultMap.put("totalNum", PI.getTotal());
resultMap.put("list", list);
return new ResultCode(StatusCode.SUCCESS, resultMap);
}
/**
* 新增資料
*
* @param request, $!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 執行個體對象
*/
@Override
public ResultCode insert(HttpServletRequest request,$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
if (EmptyUtil.isEmpty($!tool.firstLowerCase($!{tableInfo.name}))) {
return new ResultCode(StatusCode.REQ_PARAM_EMPTY.getCode(), "請求參數為空");
}
$!{tool.firstLowerCase($!{tableInfo.name})}.setId(Util.getUUID());
this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name}));
return new ResultCode(StatusCode.SUCCESS.getCode(), "新增成功");
}
/**
* 修改資料
*
* @param request,$!tool.firstLowerCase($!{tableInfo.name}) 執行個體對象
* @return 執行個體對象
*/
@Override
public ResultCode update(HttpServletRequest request,$!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
if (EmptyUtil.isEmpty($!tool.firstLowerCase($!{tableInfo.name}))) {
return new ResultCode(StatusCode.REQ_PARAM_EMPTY.getCode(), "請求參數為空");
}
this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.updateById($!tool.firstLowerCase($!{tableInfo.name}));
return new ResultCode(StatusCode.SUCCESS.getCode(), "修改成功", this.queryById($!tool.firstLowerCase($!{tableInfo.name})));
}
/**
* 批量删除
* @param request ids
* @return
*/
@Override
public ResultCode deleteByIds(HttpServletRequest request,List<String> ids) {
if (StringUtil.isEmpty(ids)) {
return new ResultCode(StatusCode.REQ_PARAM_EMPTY.getCode(), "請求參數為空");
}
try {
for (int i = 0; i <ids.size(); i++) {
if (this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById(ids.get(i)) <= 0) {
return new ResultCode(StatusCode.DELETE_FAIL, "删除失敗");
}
}
return new ResultCode(StatusCode.SUCCESS.getCode(), "删除成功");
} catch (Exception e) {
e.printStackTrace();
return new ResultCode(StatusCode.DELETE_FAIL, "删除失敗");
}
}
}
5、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}.model.$!{tableInfo.name};
import org.apache.ibatis.annotations.Param;
import java.util.List;
import com.zjimee.boot.core.vo.requestParam.CommonRequest;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表資料庫通路層
*
* @author wangj
* @since $!time.currTime()
*/
public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}> {
/**
* 通過實體作為篩選條件查詢
*
* @param commonRequest 執行個體對象
* @return 對象清單
*/
List<$!{tableInfo.name}> queryList(CommonRequest commonRequest);
/**
* 通過實體作為篩選條件查詢
*
* @param list 執行個體對象
* @return 對象清單
*/
int insertBatch(List<$!{tableInfo.name}> list);
}
6、mapper.xml
##引入mybatis支援
$!mybatisSupport
##設定儲存名稱與儲存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapping"))
##拿到主鍵
#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="com.zjimee.boot.core.dao.$!{tableInfo.name}DAO">
<!-- 結果集 -->
<resultMap type="com.zjimee.boot.core.model.$!{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>
<!-- 批量插入 -->
<insert id="insertBatch">
insert into $!tableInfo.obj.name
(<include refid="Base_Column_List" />)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#foreach($column in $tableInfo.fullColumn)
#{item.$!column.name}#if($foreach.hasNext), #end
#end
)
</foreach>
</insert>
<!--通過實體作為篩選條件查詢-->
<select id="queryList" resultMap="$!{tableInfo.name}Map">
select
#foreach($column in $tableInfo.fullColumn)
a.$!{column.obj.name}#if($foreach.hasNext), #end
#end
from $!tableInfo.obj.name a
<where>
#foreach($column in $tableInfo.fullColumn)
<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
and a.$!column.obj.name = #{$!column.name}
</if>
#end
<if test="keyWord != null and keyWord != ''">
and (a.name like concat('%',#{keyWord},'%'))
</if>
<if test="startTime != null">
<![CDATA[and a.sign_time >= #{startTime}]]>
</if>
<if test="endTime != null">
<![CDATA[and a.sign_time <= #{endTime}]]>
</if>
<if test="ids != null and ids.size()>0">
and a.id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</if>
</where>
</select>
</mapper>
附:響應類和請求類
ResultCode.java
package com.zjimee.boot.rest.common;
import com.zjimee.boot.rest.constant.StatusCode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ResultCode {
/**
* 0成功,其他失敗
*/
private int code;
/**
* 提示資訊
*/
private String msg;
/**
* 傳回資料
*/
private Object data;
public ResultCode() {
this.code = StatusCode.DO_SUCCESS.getCode();
this.msg = StatusCode.DO_SUCCESS.getMsg();
}
public boolean isSuccess() {
return this.code == 0;
}
public static boolean isSuccess(Map<String, Object> result) {
if (result.get("success_type") == null) {
return false;
}
return Integer.parseInt((String) result.get("success_type")) == 0;
}
public ResultCode(int code, String msg) {
this.code = code;
this.msg = msg;
this.data = new Object();
}
public ResultCode(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public ResultCode(List list, Integer totalNum) {
this.code = StatusCode.SUCCESS.getCode();
this.msg = StatusCode.SUCCESS.getMsg();
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("list", list);
resultMap.put("totalNum", totalNum);
this.data = resultMap;
}
public ResultCode(List list, Long totalNum) {
this.code = StatusCode.SUCCESS.getCode();
this.msg = StatusCode.SUCCESS.getMsg();
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("list", list);
resultMap.put("totalNum", totalNum);
this.data = resultMap;
}
public ResultCode(Object data) {
this.code = StatusCode.DO_SUCCESS.getCode();
this.msg = StatusCode.DO_SUCCESS.getMsg();
this.data = data;
}
public ResultCode(StatusCode statusCode) {
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
this.data = new Object();
}
public ResultCode(StatusCode statusCode, Object o) {
if (o != null) {
if (o instanceof String) {
this.msg = o.toString();
} else {
this.msg = statusCode.getMsg();
}
}
this.data = o;
this.code = statusCode.getCode();
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
請求類
CommonRequest.java
package com.zjimee.boot.core.vo.requestParam;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
import java.util.List;
/**
* @ClassName 請求參數
* @Author wangj
* @Date 2020/2/17 14:59
* @Version 1.0
*/
@Data
public class CommonRequest {
/**
* 每頁數量
*/
private int pageSize;
/**
* 目前頁
*/
private int pageNumber;
/**
* 排序
*/
private String orderBy;
/**
* ids
*/
private List<String> ids;
/**
* 名稱
*/
private String name;
/**
* 關鍵字模糊查詢
*/
private String keyWord;
/**
* ID
*/
private String id;
/**
* 編号
*/
private String code;
/**
* flag
*/
private String flag;
/**
* 開始時間
*/
private Date startTime;
/**
* 結束時間
*/
private Date endTime;
/**
* 狀态
*/
private String state;
/**
* 分類
*/
private String type;
}