idea easyCode 插件 tk.mybatis+pagehelper 模板

13 篇文章 1 订阅
6 篇文章 0 订阅

简单粗暴 无需多说 上截图 上代码

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

<?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}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>

    <!--通过Id列表作为筛选条件查询列表,列表长度不为0-->
    <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>


    <!--批量新增所有列,列表长度不能为0,且列表id统一为null或者统一不为null-->
    <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>

    <!-- 动态 修改 根据对象属性不是null -->
    <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>


    <!--通过主键修改实体列表,列表长度不能为0,注意:当实体属性为null时,对应的列不会更新为null-->
    <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>
    
    <!--通过主键列表删除,列表长度不能为0-->
    <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>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值