通用型自定义拼接 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>
相关推荐
喂完待续3 小时前
【Tech Arch】Hive技术解析:大数据仓库的SQL桥梁
大数据·数据仓库·hive·hadoop·sql·apache
路多辛4 小时前
Golang database/sql 包深度解析(二):连接池实现原理
数据库·sql·golang
javachen__8 小时前
SpringBoot整合P6Spy实现全链路SQL监控
spring boot·后端·sql
武昌库里写JAVA18 小时前
JAVA面试汇总(四)JVM(一)
java·vue.js·spring boot·sql·学习
老虎062719 小时前
数据库基础—SQL语句总结及在开发时
数据库·sql·oracle
Mr. zhihao1 天前
SQL LEFT JOIN 与 WHERE 条件的隐藏坑
数据库·sql
silver98861 天前
sql链接的url中serverTimezone的作用
数据库·sql
sleetdream1 天前
Flink Sql 按分钟或日期统计数据量
sql·flink
一宿君1 天前
Github 9 个惊艳的开源 NL2SQL 项目
sql·nlp·github
吃掉你也没关系吧2 天前
【postgresql】一文详解postgresql中的统计模块
sql·postgresql