Result.java 返回工具类
@Data
public class Result<T> {
private int code;
private String msg;
private T data;
public Result(int code, String msg) {
this.code = code;
this.msg = msg;
}
public Result(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
/**
* 默认的成功
* @return
*/
public static <T> Result<T> success(){
return new Result(Status.SUCCESS.Code,Status.SUCCESS.msg);
}
public static <T> Result<T> success(String msg,T data){
return new Result(Status.SUCCESS.Code,msg,data);
}
public static <T> Result<T> success(T data){
return new Result(Status.SUCCESS.Code,Status.SUCCESS.msg,data);
}
public static <T> Result<T> success(int code,String msg,T data){
return new Result(code,msg,data);
}
/**
* 默认的失败
* @return
*/
public static <T> Result<T> fail(){
return new Result(Status.FAIL.Code,Status.FAIL.msg);
}
public static <T> Result<T> fail(String msg){
return new Result(Status.FAIL.Code,msg);
}
public static <T> Result<T> fail(int code,String msg){
return new Result(code,msg);
}
public enum Status {
SUCCESS(0,"成功"),
FAIL(1,"失败");
private int Code;
private String msg;
Status(int code, String msg) {
Code = code;
this.msg = msg;
}
}
controlller.java.vm
##定义初始变量
#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}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import $!{tableInfo.savePackageName}.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制层
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
@Autowired
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 分页查询
* @param $!tool.firstLowerCase($tableInfo.name) 对象
* @return
*/
@PostMapping("/page")
public Result<PageInfo<$!{tableInfo.name}>> page(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
PageInfo<$tableInfo.name> page = $!{tool.firstLowerCase($tableInfo.name)}Service.page($!{tool.firstLowerCase($!{tableInfo.name})});
return Result.success(page);
}
/**
* 查询列表
* @param $!tool.firstLowerCase($tableInfo.name) 对象
* @return
*/
@PostMapping("/list")
public Result<List<$!{tableInfo.name}>> list(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
List<$tableInfo.name> list = $!{tool.firstLowerCase($tableInfo.name)}Service.list($!{tool.firstLowerCase($!{tableInfo.name})});
return Result.success(list);
}
@PostMapping("/detail")
public Result<$!{tableInfo.name}> detail(@RequestParam $!pk.shortType $!pk.name) {
$tableInfo.name $!{tool.firstLowerCase($tableInfo.name)} = $!{tool.firstLowerCase($tableInfo.name)}Service.detail($!pk.name);
return Result.success($!{tool.firstLowerCase($tableInfo.name)});
}
/**
* 添加
* @param $!tool.firstLowerCase($tableInfo.name) 对象
* @return
*/
@PostMapping("/save")
public Result<$tableInfo.name> save(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)){
$!{tool.firstLowerCase($tableInfo.name)}Service.save($!tool.firstLowerCase($tableInfo.name));
return Result.success();
}
@PutMapping("/edit")
public Result edit(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)){
$!{tool.firstLowerCase($tableInfo.name)}Service.edit($!tool.firstLowerCase($tableInfo.name));
return Result.success();
}
@DeleteMapping("/delete")
public Result delete(@RequestParam $!pk.shortType $!pk.name){
$!{tool.firstLowerCase($tableInfo.name)}Service.delete($!pk.name);
return Result.success();
}
}
service.java.vm
##定义初始变量
#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 com.github.pagehelper.PageInfo;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务接口
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableInfo.name}Service {
/**
* 分页 查询
* @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
* @return
*/
PageInfo<$!{tableInfo.name}> page($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 查询 列表
* @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
* @return
*/
List<$!{tableInfo.name}> list($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 详情
* @param $!pk.name 主键
* @return
*/
$!{tableInfo.name} detail($!pk.shortType $!pk.name);
/**
* 添加
* @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
* @return
*/
int save($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 修改 根据主键
* @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
* @return
*/
int edit($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 删除
* @param $!pk.name 主键
* @return
*/
int delete($!pk.shortType $!pk.name);
/**
* 批量删除
* @param $!{pk.name}s 主键集合
* @return
*/
int deleteByIds(List<$!pk.shortType> ids);
}
serviceImpl.java.vm
##定义初始变量
#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}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})服务接口实现层
*
* @author $!author
* @since $!time.currTime()
*/
@Service
public class $!{tableName} implements $!{tableInfo.name}Service {
@Resource(type = $!{tableInfo.name}Dao.class)
private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
/**
* 查询列表
* @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
* @return
*/
@Override
public PageInfo<$!{tableInfo.name}> page($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
PageHelper.startPage(test.getPageNum(),test.getPageSize());
List<${tableInfo.name}> list = $!{tool.firstLowerCase($!{tableInfo.name})}Dao.findList($!tool.firstLowerCase($!{tableInfo.name}));
PageInfo<Test> page = new PageInfo<>(list);
return page;
}
/**
* 查询列表
* @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
* @return
*/
@Override
public List<$!{tableInfo.name}> list($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.findList($!tool.firstLowerCase($!{tableInfo.name}));
}
/**
* 详情
* @param $!pk.name 主键
* @return
*/
@Override
public $!{tableInfo.name} detail($!pk.shortType $!pk.name) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.selectByPrimaryKey($!{pk.name});
}
/**
* 添加
* @param $!tool.firstLowerCase($!{tableInfo.name}) 对象
* @return
*/
@Override
public int save($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.insertSelective($!{tool.firstLowerCase($!{tableInfo.name})});
}
/**
* 修改
* @param $!tool.firstLowerCase($!{tableInfo.name}) 对象 根据主键修改
* @return
*/
@Override
public int edit($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.updateByPrimaryKeySelective($!{tool.firstLowerCase($!{tableInfo.name})});
}
/**
* 删除
* @param $!{pk.name} 主键
* @return
*/
@Override
public int delete($!pk.shortType $!pk.name) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteByPrimaryKey($!pk.name);
}
/**
* 批量删除
* @param $!{pk.name}s 主键集合
* @return
*/
@Override
public int deleteByIds(List<$!pk.shortType> ids) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteByIds(ids);
}
}
dao.java.vm
##定义初始变量
#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 tk.mybatis.mapper.common.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* $!{tableInfo.comment}($!{entityName})表数据库访问层
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends Mapper<$!{tableInfo.name}> {
/**
* 动态 查询 多条数据 属性不是null
* @param $!tool.firstLowerCase(${tableInfo.name}) 对象
* @return 实例对象的集合
*/
List<$!{tableInfo.name}> findList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 查询多条数据
* @param list<$!pk.shortType> 主键集合
* @return 实例对象的集合
*/
List<$!{tableInfo.name}> findListByIds(List<$!pk.shortType> list);
/**
* 动态 批量 新增 属性不是null更新
* @param list<${tableInfo.name}> 实例对象
* @return 实例对象
*/
int insertSelectiveBatch(List<$!{tableInfo.name}> list);
/**
* 动态 批量 修改 属性不是null更新
* @param where 对象
* @param set 对象
* @return int
*/
int updateSelectiveByField(@Param("set") $!{tableInfo.name} set,@Param("where") $!{tableInfo.name} where);
/**
* 动态 批量 修改 属性不是null更新
* @param list<${tableInfo.name})> 实例对象集合
* @return int
*/
int updateSelectiveBatchByIds(List<$!{tableInfo.name}> list);
/**
* 删除
* @param $!pk.name 主键
* @return int
*/
int deleteById($!pk.shortType $!pk.name);
/**
* 动态 删除 属性不是null更新
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return int
*/
int deleteSelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 删除
* @param list<$!pk.shortType> 主键集合
* @return int
*/
int deleteByIds(List<$!pk.shortType> list);
/**
* 动态 查询 总数
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return int
*/
int findCountSelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
}
entity.java.vm
##引入宏定义
$!{define.vm}
#set($entityName = $tableInfo.name)
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
$!callback.setFileName($tool.append($entityName, ".java"))
##使用宏定义设置包后缀
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}entity;
##使用全局变量实现默认包导入
$!autoImport
import lombok.Data;
##import io.swagger.annotations.ApiModel;
##import io.swagger.annotations.ApiModelProperty;
import javax.persistence.Column;
import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Id;
#foreach($column in $tableInfo.fullColumn)
#if($column.type.equals("java.time.LocalDateTime"))
import java.time.LocalDateTime;
#elseif($column.type.equals("java.util.Date"))
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
#end
#end
import java.io.Serializable;
##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
@Table(name = "$tableInfo.obj.name")
##@ApiModel(value = "$!{entityName})", description = "$!{tableInfo.comment}")
public class $!{entityName} implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.pkColumn )
#if(${column.comment})/**
* ${column.comment}
*/
#end
@Id
@GeneratedValue(strategy =GenerationType.IDENTITY)
## @ApiModelProperty(value = "${column.comment}")
@Column(name = "$column.obj.name")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#foreach($column in $tableInfo.otherColumn)
#if(${column.comment})/**
* ${column.comment}
*/
#end
## @ApiModelProperty(value = "${column.comment}")
@Column(name = "$column.obj.name")
#if($column.type.equals("java.util.Date"))
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
#end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
/**
* 页数
*/
@Transient
private int pageNum =1;
/**
* 每页条数
*/
@Transient
private int pageSize =10;
}
mapper.xml.vm
##引入mybatis支持
$!{mybatisSupport.vm}
##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, ".xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
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}ResultMap">
#foreach($column in $tableInfo.fullColumn)
<result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
resultMap>
<sql id="table_field">
#allSqlColumn()
sql>
<select id="findList" resultMap="$!{tableInfo.name}ResultMap" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
select
<include refid="table_field" />
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,jdbcType=$!column.ext.jdbcType}
if>
#end
where>
select>
<select id="findOne" resultMap="$!{tableInfo.name}ResultMap" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
select
<include refid="table_field" />
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,jdbcType=$!column.ext.jdbcType}
if>
#end
where>
select>
<select id="findListByIds" resultMap="$!{tableInfo.name}ResultMap" parameterType="list">
select
<include refid="table_field" />
from $!tableInfo.obj.name
where $!pk.obj.name in
<foreach item="item" collection="list" separator="," open="(" close=")" index="index">
#{item}
foreach>
select>
<insert id="insertSelectiveBatch" keyProperty="$!pk.name" useGeneratedKeys="true" parameterType="list">
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 item="item" collection="list" separator="," open="" close="" index="index">
#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
foreach>
trim>
insert>
<update id="updateSelectiveByField">
update $!{tableInfo.obj.name}
<set>
#foreach($column in $tableInfo.otherColumn)
<if test="where.$!{column.name} == null and set.$!{column.name} != null#if($column.type.equals("java.lang.String")) and set.$!{column.name} != ''#end">
$!column.obj.name = #{set.$!{column.name},jdbcType=$!column.ext.jdbcType},
if>
#end
set>
<where>
#foreach($column in $tableInfo.fullColumn)
<if test="where.$!{column.name} != null">
and $!column.obj.name = #{where.$!{column.name},jdbcType=$!column.ext.jdbcType}
if>
#end
where>
update>
<update id="updateSelectiveBatchByIds" parameterType="list">
update $!{tableInfo.obj.name}
<trim prefix="set" suffixOverrides=",">
#foreach($column in $tableInfo.otherColumn)
<trim prefix="$!{column.obj.name} = case" suffix="end,">
<foreach collection="list" item="item" index="index">
when $!pk.obj.name = #{item.$!pk.name} then #{item.$!column.name}
foreach>
trim>
#end
trim>
where $!pk.obj.name in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.$!pk.name,jdbcType=$!pk.ext.jdbcType}
foreach>
update>
<delete id="deleteById" parameterType="$pk.type">
delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name,jdbcType=$!pk.ext.jdbcType}
delete>
<delete id="deleteSelective" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
delete from $!{tableInfo.obj.name}
<where>
#foreach($column in $tableInfo.otherColumn)
<if test="$!column.name != null">
and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType}
if>
#end
where>
delete>
<delete id="deleteByIds" parameterType="list">
delete from $!{tableInfo.obj.name} where $!pk.obj.name in
<foreach item="item" collection="list" separator="," open="(" close=")" index="index">
#{item}
foreach>
delete>
<select id="findCountSelective" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" resultType="int">
select count(*) from $!{tableInfo.obj.name}
<where>
#foreach($column in $tableInfo.fullColumn)
<if test="$!column.name != null">
and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType}
if>
#end
where>
select>
mapper>