通用型自定义拼接 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>
相关推荐
viperrrrrrrrrr74 小时前
大数据学习(112)-HIVE中的窗口函数
hive·sql·学习
艺杯羹12 小时前
JDBC之ORM思想及SQL注入
数据库·sql·jdbc·orm·sql注入
我的golang之路果然有问题17 小时前
速成GO访问sql,个人笔记
经验分享·笔记·后端·sql·golang·go·database
秃头佛爷19 小时前
常用SQL整理
数据库·sql
浩浩测试一下20 小时前
SQL注入高级绕过手法汇总 重点
数据库·sql·安全·web安全·网络安全·oracle·安全架构
獨枭21 小时前
SQL Server 2019 安装与配置详细教程
sql·sqlserver
bing_1581 天前
在 Spring Boot 项目中如何使用索引来优化 SQL 查询?
spring boot·sql·索引优化
husterlichf1 天前
MYSQL 常用字符串函数 和 时间函数详解
数据库·sql·mysql
什么芮.1 天前
spark-streaming
pytorch·sql·spark·kafka·scala