通用型自定义拼接 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>
相关推荐
程序员张34 小时前
SQL分析与打印-p6spy组件
spring boot·sql·mybatis·mybatisplus·p6spy
RainbowSea15 小时前
15. MySQL 多版本并发控制
java·sql·mysql
唐可盐16 小时前
第六章 SQL编程系列-Gbase8a从入门到进阶
数据库·sql·gbase8a
DataGear1 天前
如何在DataGear 5.4.1 中快速制作SQL服务端分页的数据表格看板
javascript·数据库·sql·信息可视化·数据分析·echarts·数据可视化
数据狐(DataFox)1 天前
SQL参数化查询:防注入与计划缓存的双重优势
数据库·sql·缓存
先做个垃圾出来………2 天前
SQL的底层逻辑解析
数据库·sql
Lx3522 天前
MySQL物化视图:预计算查询结果的定期刷新
sql·mysql·性能优化
Lx3522 天前
Mysql死锁日志分析:事务逻辑冲突的排查技巧
sql·mysql·性能优化
RainbowSea2 天前
14. MySQL 锁的详细说明
java·sql·mysql
RainbowSea2 天前
12 MySQL 数据库其它调优策略
java·sql·mysql