myatbis的一个好的封装
package com.pj.project4sp; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.pj.project4sp.public4mapper.PublicMapper;
import com.pj.project4sp.public4mapper.PublicService; /**
* 公共Mapper 与 公共Service
* @author kong
*
*/
@Component
public class SP { /**
* 公共Mapper
*/
public static PublicMapper publicMapper;
/**
* 公共Service
*/
public static PublicService publicService; // 注入
@Autowired
public void setBean(
PublicMapper publicMapper,
PublicService publicService
) {
SP.publicMapper = publicMapper;
SP.publicService = publicService;
} }
package com.pj.project4sp.public4mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import com.pj.utils.so.SoMap; /**
* 公用Mapper,封装一些常见的Mapper操作,避免某些及其简单的逻辑也要写一堆xml方法
* @author kong
* 更新于2020-12-1 新增部分方法
*
*/
@Mapper
public interface PublicMapper { // ------------------------ 一些工具方法 ------------------------ /**
* 返回上一句SQL插入的自增主键值
* @return
*/
public long getPrimarykey(); // ------------------------ 新增SQL相关 ------------------------ // ------------------------ 删除SQL相关 ------------------------ /**
* 根据id删除一条记录
* @param tableName 表名字
* @param id id值
* @return
*/
public int deleteById(
@Param("tableName")String tableName,
@Param("id")Object id
); /**
* 根据指定列指定值删除一条记录
* @param tableName 表名
* @param whereName 条件列
* @param whereValue 条件列值
* @return
*/
public int deleteBy(
@Param("tableName")String tableName,
@Param("whereName") String whereName,
@Param("whereValue") Object whereValue
); /**
* 根据id列表批量删除
* @param tableName 表名字
* @param ids id列表
* @return
*/
public int deleteByIds(
@Param("tableName")String tableName,
@Param("ids")List<?> ids
); /**
* 根据指定列指定值删除多条记录
* @param tableName 表名
* @param whereName 条件列名
* @param whereList 条件列值
* @return
*/
public int deleteByWhereList(
@Param("tableName")String tableName,
@Param("whereName") String whereName,
@Param("whereList") List<?> whereList
); // ------------------------ 修改SQL相关 ------------------------ /**
* 指定表的指定字段增加指定值,可以为负值
* @param tableName 表名字
* @param columnName 列值
* @param num 增加的值
* @param id id值
* @return
*/
public int columnAdd(
@Param("tableName") String tableName,
@Param("columnName") String columnName,
@Param("num") long num,
@Param("id") Object id
); /**
* 指定表的指定字段增加指定值,可以为负值
* @param tableName 表名字
* @param columnName 列名字
* @param num 增加的值
* @param ids id列表
* @return
*/
public int columnAddByIds(
@Param("tableName") String tableName,
@Param("columnName") String columnName,
@Param("num") long num,
@Param("ids") List<?> ids
); /**
* 指定表的指定字段更新为指定值,根据指定id
* @param tableName 表名子
* @param columnName 列名
* @param value 值
* @param id id值
* @return
*/
public int updateColumnById(
@Param("tableName") String tableName,
@Param("columnName") String columnName,
@Param("value") Object value,
@Param("id") Object id
); /**
* 指定表的指定字段更新为指定值,根据指定id列表
* @param tableName 表名子
* @param columnName 列名
* @param value 值
* @param ids id值
* @return
*/
public int updateColumnByIds(
@Param("tableName") String tableName,
@Param("columnName") String columnName,
@Param("value") Object value,
@Param("ids") List<?> ids
); /**
* 指定表的指定字段更新为指定值, 根据指定列的指定值
* @param tableName 表名
* @param columnName 列名
* @param columnValue 列值
* @param whereName 条件列名
* @param whereValue 条件列值
* @return
*/
public int updateColumnBy(
@Param("tableName") String tableName,
@Param("columnName") String columnName,
@Param("columnValue") Object columnValue,
@Param("whereName") String whereName,
@Param("whereValue") Object whereValue
); /**
* 指定表的指定字段SoMap集合更新为指定值,根据指定id
* @param tableName 表名
* @param soMap 要修改的列
* @param id id值
* @return
*/
public int updateBySoMapById(
@Param("tableName") String tableName,
@Param("soMap") SoMap soMap,
@Param("id") Object id
); /**
* 指定表的指定字段SoMap集合更新为指定值,指定列的指定值
* @param tableName 表名子
* @param soMap 要修改的列
* @param whereName 条件列值
* @param whereValue 条件列值
* @return
*/
public int updateBySoMapBy(
@Param("tableName") String tableName,
@Param("soMap") SoMap soMap,
@Param("whereName") String whereName,
@Param("whereValue") Object whereValue
); // ------------------------ 查询SQL相关 ------------------------ /**
* 获取指定表的指定字段值,根据id值
* @param tableName 表名
* @param columnName 列名
* @param id id值
* @return
*/
public String getColumnById(
@Param("tableName") String tableName,
@Param("columnName") String columnName,
@Param("id") Object id
); /**
* 获取指定表的指定字段值,并转化为long,根据id值
* @param tableName 表名
* @param columnName 列名
* @param id id值
* @return
*/
public long getColumnByIdToLong(
@Param("tableName") String tableName,
@Param("columnName") String columnName,
@Param("id") Object id
); /**
* 获取指定表的指定字段值,根据指定条件(whereName=whereValue)
* @param tableName 表名
* @param columnName 列名
* @param whereName 条件列名
* @param whereValue 条件列值
* @return
*/
public String getColumnByWhere(
@Param("tableName") String tableName,
@Param("columnName") String columnName,
@Param("whereName") String whereName,
@Param("whereValue") Object whereValue
); /**
* 获取指定表的指定字段值列表,并转化为long, 根据指定条件(whereName=whereValue)
* @param tableName 表名
* @param columnName 列名
* @param whereName 条件列名
* @param whereValue 条件列值
* @return
*/
public List<Long> getColumnListToLongByWhere(
@Param("tableName") String tableName,
@Param("columnName") String columnName,
@Param("whereName") String whereName,
@Param("whereValue") Object whereValue
); /**
* 获取指定表的count数据,根据指定条件(whereName=whereValue)
* @param tableName 表名
* @param whereName 条件列名
* @param whereValue 条件列值
* @return
*/
public long getCountBy(
@Param("tableName") String tableName,
@Param("whereName") String whereName,
@Param("whereValue") Object whereValue
); // ------------------------ 查询集合SQL相关 ------------------------ /**
* 获取指定表的全部字段全部数据转化为Map
* @param tableName 表名子
* @return
*/
public List<SoMap> getListMap(@Param("tableName") String tableName); /**
* 获取指定表的全部字段全部数据转化为Map, 根据指定条件(whereName=whereValue)
* @param tableName 表名字
* @param whereName 条件列名
* @param whereValue 条件列值
* @return
*/
public List<SoMap> getListMapByWhere(
@Param("tableName") String tableName,
@Param("whereName") String whereName,
@Param("whereValue") Object whereValue
); /**
* 获取指定表的全部字段全部数据转化为Map, 根据指定条件(id=id)
* @param tableName 表名子
* @param id id值
* @return
*/
public List<SoMap> getListMapById(
@Param("tableName") String tableName,
@Param("id") Object id
); }
<?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.pj.project4sp.public4mapper.PublicMapper"> <!-- ======================== 一些工具方法 ======================== --> <!-- 返回上一句SQL插入的自增主键值 -->
<select id="getPrimarykey" resultType="long">
SELECT @@identity
</select> <!-- ======================== 新增SQL相关 ======================== --> <!-- ======================== 删除SQL相关 ======================== --> <!-- 删除一条记录,指定表名,id -->
<delete id="deleteById">
delete from ${tableName} where id = #{id}
</delete> <!-- 根据指定列指定值删除一条记录,// 参数: 表名、条件列表、条件列值 -->
<delete id="deleteBy">
delete from ${tableName} where ${whereName} = #{whereValue}
</delete> <!-- 删除一条记录,指定表名,id列表 -->
<delete id="deleteByIds">
delete from ${tableName}
where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete> <!-- 根据指定列指定值删除多条记录 -->
<delete id="deleteByWhereList">
delete from ${tableName}
where ${whereName} in
<foreach collection="whereList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete> <!-- ======================== 修改SQL相关 ======================== --> <!-- 指定表的指定字段加num(可以小于0 ),根据指定id -->
<update id="columnAdd">
update ${tableName} set
${columnName} = IFNULL(${columnName}, 0) + #{num}
where id = #{id};
</update> <!-- 指定表的指定字段增加指定值,可以为负值 -->
<update id="columnAddByIds">
update ${tableName} set
${columnName} = ${columnName} + #{num}
where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</update> <!-- 指定表的指定字段更新为指定值,根据指定id -->
<update id="updateColumnById">
update ${tableName} set
${columnName} = #{value}
where id = #{id}
</update> <!-- 指定表的指定字段更新为指定值,根据指定id列表 -->
<update id="updateColumnByIds">
update ${tableName} set
${columnName} = #{value}
where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</update> <!-- 指定表的指定字段更新为指定值, 根据指定列的指定值 -->
<update id="updateColumnBy">
update ${tableName} set
${columnName} = #{columnValue}
where ${whereName} = #{whereValue}
</update> <!-- 指定表的指定字段SoMap集合更新为指定值,根据指定id -->
<update id="updateBySoMapById">
update ${tableName} set
<foreach collection="soMap.entrySet()" item="value" index="key" separator=",">
${key} = #{value}
</foreach>
where id = #{id}
</update> <!-- 指定表的指定字段SoMap集合更新为指定值,根据指定id -->
<update id="updateBySoMapBy">
update ${tableName} set
<foreach collection="soMap.entrySet()" item="value" index="key" separator=",">
${key} = #{value}
</foreach>
where ${whereName} = #{whereValue}
</update> <!-- ======================== 查询SQL相关 ======================== --> <!-- 获取指定表的指定字段值,根据id值 -->
<select id="getColumnById" resultType="String">
select ${columnName} from ${tableName}
where id = #{id}
</select> <!-- 获取指定表的指定字段值,并转化为long,根据id值 -->
<select id="getColumnByIdToLong" resultType="long">
select ${columnName} from ${tableName}
where id = #{id}
</select> <!-- 获取指定表的指定字段值,根据指定条件(whereName=whereValue) -->
<select id="getColumnByWhere" resultType="String">
select ${columnName} from ${tableName}
where ${whereName} = #{whereValue}
</select> <!-- 获取指定表的指定字段值列表,并转化为long, 根据指定条件(whereName=whereValue) -->
<select id="getColumnListToLongByWhere" resultType="long">
select ${columnName} from ${tableName}
where ${whereName} = #{whereValue}
</select> <!-- 获取指定表的count数据,根据指定条件(whereName=whereValue) -->
<select id="getCountBy" resultType="long">
select count(*) from ${tableName}
where ${whereName} = #{whereValue}
</select> <!-- ======================== 查询集合SQL相关 ======================== --> <!-- 获取指定表的全部字段全部数据 -->
<select id="getListMap" resultType="somap">
select * from ${tableName}
</select> <!-- 获取指定表的全部字段全部数据转化为Map, 根据指定条件(whereName=whereValue) -->
<select id="getListMapByWhere" resultType="somap">
select * from ${tableName}
where ${whereName} = #{whereValue}
</select> <!-- 获取指定表的全部字段全部数据转化为Map, 根据指定条件(id=id) -->
<select id="getListMapById" resultType="somap">
select * from ${tableName}
where id = #{id}
</select> </mapper>
myatbis的一个好的封装的更多相关文章
- 对Raphael画图标的一个jquery简单封装
公司要做一个项目的demo,要求地图上可以插红旗,所以就用到了Raphael. 因为是个demo,所以地图就用了一张图片,效果如下: 所以为了更好的封装一下这个功能,就写了一个简单的插件:jquery ...
- 对 JDBC 做一个轻量封装,待完善。。。
对 JDBC 做一个轻量地封装,顺便复习,熟悉sql,io,util,lang.Reflect等包的使用,泛型的使用,待完善... package com.webproj.utils; import ...
- 准备开始,选好第一个C#的封装库
如今C#做工业图像处理和开发,最多资料和例子的就是Emgu.不过现在人家开始商业收费了,对于我们这些小企业就不是很好了.这里,我要介绍和推荐的是另外一个也同样牛逼的C#封装Opnecv的库,叫做Ope ...
- 一个强大的封装好的pdo处理类
php5.5后就不支持mysql扩展了,也就是说这以后都不能使用msyql_conncet之类的函数了.不过没有关系,pdo比mysql有更多优势,写法也很简单,下面贴出一个来自互联网的pdo处理类. ...
- DirectX11 学习笔记4 - 一个完整的封装框架
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3EzNjExMDYzMDY=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 分享一个dapper简单封装
using System;using System.Data.Common;using System.Linq;using Dapper;using MySql.Data.MySqlClient; p ...
- AES和RSA的加密过程通过面向对象的方式写成一个类,封装起来
# 面向对象的方式 实现加密方法 from Crypto.Cipher import AES from Crypto import Random from binascii import b2a_he ...
- Javascript:来一个AJAX封装函数
前不久换工作了,最近一直在出差,忙得跟狗一样,所以博客都荒废许久了. 最近的工作中涉及到大量的ajax操作,本来该后台做的事也要我来做了.而现在使用的ajax函数是一个后台人员封装的—-但他又是基于 ...
- 一个简单的定时器(NSTimer)的封装
在项目开发中我们有的时候需要用到计时器,比如登录超时,scrollview的滚动等,那么就让我们自己手动的去创建一个类库吧. 1 首先你需要一个向外提供创建的便捷方法. 1.1 这里考虑两种情况,一种 ...
- 一个高性能异步socket封装库的实现思路 (c#)
前言 socket是软件之间通讯最常用的一种方式.c#实现socket通讯有很多中方法,其中效率最高就是异步通讯. 异步通讯实际是利用windows完成端口(IOCP)来处理的,关于完成端口实现原理, ...
随机推荐
- day01-Tomcat框架分析
引入课程和Maven 1.Maven maven中央仓库:Maven Repository: Search/Browse/Explore (mvnrepository.com) maven仓库是国外的 ...
- 浅谈HTTP缓存与CDN缓存的那点事
HTTP缓存与CDN缓存一直是提升web性能的两大利器,合理的缓存配置可以降低带宽成本.减轻服务器压力.提升用户的体验.而不合理的缓存配置会导致资源界面无法及时更新,从而引发一系列的衍生问题.本文将分 ...
- 基于k8s的发布系统的实现
综述 首先,本篇文章所介绍的内容,已经有完整的实现,可以参考这里. 在微服务.DevOps和云平台流行的当下,使用一个高效的持续集成工具也是一个非常重要的事情.虽然市面上目前已经存在了比较成熟的自动化 ...
- JS图片放大镜功能实现
JS图片放大镜功能实现 技术关键点 1.左侧和上侧距离,在一个水平位置和垂直位置中有我们可以挪动的区域,就是原图片区域,鼠标挪动位置是一个块状位置,他的左侧和上侧距离浏览器上侧和左侧分别有一个长度,我 ...
- 推荐一款采用 .NET 编写的 反编译到源码工具 Reko
今天给大家介绍的是一款名叫Reko的开源反编译工具,该工具采用C#开发,广大研究人员可利用Reko来对机器码进行反编译处理.我们知道.NET 7 有了NativeAOT 的支持,采用NativeAOT ...
- Python:灵活的开发环境
以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「englyf」https://mp.weixin.qq.com/s/WTl7BPAhX5VuK-gmHaErMg 本文大概 1667 个 ...
- C#不提升自己程序的权限实现操作注册表
1. 绪论 当我们编写了自己的C#程序,有程序自定义的文件类型时,通常希望它满足以下需求: 双击自定义文件打开自定义程序 自定义文件有着自己的图标 此时,在网上检索可以发现,大多数回答是使用Micro ...
- 如何在SpringBoot中优雅地重试调用第三方API?
前言 作为后端程序员,我们的日常工作就是调用一些第三方服务,将数据存入数据库,返回信息给前端.但你不能保证所有的事情一直都很顺利.像有些第三方API,偶尔会出现超时.此时,我们要重试几次,这取决于你的 ...
- Python从入门到精通(第2版)——pyuic5: error: no such option: -m的问题解决
前言 在学习<Python从入门到精通(第2版)>的第15章 GUI界面编程--15.2.4 将.ui文件转换为.py文件时,按照书中步骤出错时的问题解决,希望对同样学习本书的同学有所帮助 ...
- 【转载】VFP编写DLL,并调用
1. 编制DLL文件 ,保存为Temp.prg Define Class vfptools As Session OlePublic Procedure Add As Integer Lp ...