通用型自定义拼接 SQL 脚本,摆脱重复工作量

通用型自定义拼接sql脚本,摆脱重复工作量

在开发 Restful 接口时,我们经常需要根据前端传递的参数动态拼接 SQL 查询语句,以满足不同的查询需求。本文将介绍一个通用的自定义拼接 SQL 脚本的方法,帮助开发人员减少重复工作量。

首先,我们定义了一个名为 SqlParmDTO 的传参实体类,其中包含了需要传递给 SQL 查询的各个参数,如 where 条件、排序字段 sort、排序方式 order、结果集偏移量 offset 和结果集限制数量 limit

接下来,我们提供了一个方法 listPageBysql() 来根据传入的 SqlParmDTO 对象执行 SQL 查询,并返回分页结果。该方法首先判断是否存在唯一字段 unique,如果不存在,则执行普通的 SQL 查询,否则执行带有去重功能的 SQL 查询。查询结果封装在 PageInfo 对象中返回。

在 MyBatis 的 XML 配置文件中,我们定义了四个 SQL 查询语句,分别是 selectSqlselectSqlCountselectUniqueSqlselectUniqueSqlCount。这些查询语句根据传入的参数动态拼接 SQL 语句,并执行相应的查询操作。

以下就是通用型自定义拼接 SQL 脚本的示例代码。通过这种方式,开发人员可以根据不同的查询需求,灵活地拼接 SQL 语句,从而避免了重复的工作量。

Rest Full接口联调

{

"order": "desc",

"sort": "id",

"unique": "field1, field2, field3",

"where": "field1 IN ( 'A00000020231017001') AND field2 IN ('62284613461111198') AND time1>= '2017--09--08' AND time2<= '2024--11--16 23:59:59' "

}

自定义传参实体类:

复制代码
public class SqlParmDTO implements Serializable{
private static final long serialVersionUID = 1L;
 private String where;
 private String unique;
 private String sort;
 private String order;
 private int offset;
 private int limit;
}

方法类:

复制代码
@Override
public PageInfo<PhoneBill> listPageBysql(SqlParmDTO sqlParm) {
if (StringUtils.isEmpty(sqlParm.getUnique())) {
 long total = this.mapper.selectSqlCount(sqlParm.getWhere());
 List<PhoneBill> lists = this.mapper.selectSql(sqlParm.getWhere(), sqlParm.getSort(), sqlParm.getOrder(), sqlParm.getOffset(), sqlParm.getLimit());
 PageInfo<PhoneBill> pageInfo = new PageInfo<>(lists);
 pageInfo.setTotal(total);
 pageInfo.setPageNum(sqlParm.getOffset());
 pageInfo.setPageSize(sqlParm.getLimit());
 return pageInfo;
 } else {
 long total = this.mapper.selectUniqueSqlCount(sqlParm.getWhere(), sqlParm.getUnique());
 List<PhoneBill> lists = this.mapper.selectUniqueSql(sqlParm.getWhere(), sqlParm.getSort(), sqlParm.getOrder(), sqlParm.getOffset(), sqlParm.getLimit(), sqlParm.getUnique());
 PageInfo<PhoneBill> pageInfo = new PageInfo<>(lists);
 pageInfo.setTotal(total);
 pageInfo.setPageNum(sqlParm.getOffset());
 pageInfo.setPageSize(sqlParm.getLimit());
 return pageInfo;
 }
}

#mybastis xml文件:

复制代码
<select id="selectSql" resultType="map">
 select * from jz_phone_bill
 <where>
 <if test="where != null and where !=''">
 ${where}
 </if>
 </where>
 <if test="sort != null and sort !=''">
 order by
 ${sort}
 <if test="order != null and order !=''">
 ${order}
 </if>
 </if>
 </select>
<select id="selectSqlCount" resultType="java.lang.Long">
 select count(1) from jz_phone_bill
 <where>
 <if test="where != null and where !=''">
 ${where}
 </if>
 </where>
 </select>
复制代码
<select id="selectUniqueSql" resultType="map">
 select * from ( select t.*, ROW_NUMBER ( ) OVER (
 PARTITION BY
 <if test="unique != null and unique !=''">
 ${unique}
 </if>
 <if test="sort != null and sort !=''">
 order by
 ${sort}
 <if test="order != null and order !=''">
 ${order}
 </if>
 </if>
 ) AS `row_number1`
 from jz_phone_bill t
 <where>
 <if test="where != null and where !=''">
 ${where}
 </if>
 </where>
 ) t2 where `row_number1` = 1
 </select>
 <select id="selectUniqueSqlCount" resultType="java.lang.Long">
 select count(1) from ( select t.*, ROW_NUMBER ( ) OVER (
 PARTITION BY
 <if test="unique != null and unique !=''">
 ${unique}
 </if>
 ) AS `row_number1`
 from jz_phone_bill t
 <where>
 <if test="where != null and where !=''">
 ${where}
 </if>
 </where>
 ) t2 where `row_number1` = 1
</select>
相关推荐
阿里嘎多哈基米7 小时前
SQL 层面行转列
数据库·sql·状态模式·mapper·行转列
雨落Liy9 小时前
SQL 函数从入门到精通:原理、类型、窗口函数与实战指南
数据库·sql
三毛200411 小时前
玳瑁的嵌入式日记D33-0908(SQL数据库)
jvm·数据库·sql
TDengine (老段)12 小时前
TDengine 选择函数 Last() 用户手册
大数据·数据库·sql·物联网·时序数据库·tdengine·涛思数据
心 一14 小时前
Web安全基石:深入理解与防御SQL注入漏洞
sql·安全·web安全
林熙蕾LXL16 小时前
SQL Server——基本操作
数据库·sql
FLS16818 小时前
Kali搭建sqli-labs靶场
linux·sql·安全·网络安全
007php00718 小时前
某大厂MySQL面试之SQL注入触点发现与SQLMap测试
数据库·python·sql·mysql·面试·职场和发展·golang
Ultipa1 天前
查询语言的进化:SQL之后,为什么是GQL?数据世界正在改变
数据库·sql·图数据库·gql
LB21121 天前
SQL隐式链接显式连接
大数据·数据库·sql