通用型自定义拼接 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>
相关推荐
看天走路吃雪糕40 分钟前
墨者:通过手工解决SQL手工注入漏洞测试(PostgreSQL数据库)
数据库·sql·postgresql·sql注入·墨者靶场
看天走路吃雪糕1 小时前
墨者:SQL注入漏洞测试(布尔盲注)
数据库·sql·sqlmap·墨者学院·布尔盲注
技术卷4 小时前
详解力扣高频SQL50题之610. 判断三角形【简单】
sql·leetcode·oracle
技术卷7 小时前
详解力扣高频SQL50题之619. 只出现一次的最大数字【简单】
sql·leetcode·oracle
Goona_20 小时前
拒绝SQL恐惧:用Python+pyqt打造任意Excel数据库查询系统
数据库·python·sql·excel·pyqt
rufeii1 天前
[极客大挑战 2019]FinalSQL--布尔盲注
sql
技术卷1 天前
详解力扣高频SQL50题之1084. 销售分析 III【简单】
sql·leetcode·oracle
NPE~1 天前
基于MySQL实现基础图数据库
数据库·sql·mysql·教程·图数据库·图结构
技术卷1 天前
详解力扣高频SQL50题之550. 游戏玩法分析 IV【中等】
sql·mysql·leetcode·oracle
样子20181 天前
Sql注入 之sqlmap使用教程
数据库·sql