MyBatis動態(tài)SQL如何實現(xiàn)前端指定返回字段
動態(tài)SQL實現(xiàn)前端指定返回字段
問題描述
在使用ClickHouse時,遇到需要根據(jù)業(yè)務需求,動態(tài)返回指定字段,從而充分利用ClickHouse列式存儲的優(yōu)勢。
解決方案
可用MyBatis的動態(tài)SQL進行數(shù)據(jù)返回;故可在業(yè)務層將指定返回的列傳入數(shù)據(jù)層,利用foreach標簽進行遍歷返回數(shù)據(jù)。
代碼實現(xiàn)
1.請求參數(shù)
@Data public class QueryDTO { ? ? /** ? ? ?* 返回值 ? ? ?*/ ? ? private Set<String> targetFields; }
2.mapper接口
?? ?/** ? ? ?* 查詢數(shù)據(jù) ? ? ?* @param record ? ? ?* @return ? ? ?*/ ? ? List<ResourceTotal> query(QueryDTO record);
3.mapper.xml實現(xiàn)
? <sql id="Base_Result_Column"> ? ? resourceId, platformCount, createUserId ? </sql>
? <select id="query" parameterType="cn.example.module.data.test.QueryDTO" resultMap="BaseResultMap"> ? ? select ? ? <foreach collection="targetFields" item="item" separator=","> ? ? ? ${item} ? ? </foreach> ? ? , ? ? <include refid="Base_Result_Column"/> ? ? from resource_total ? ? where 1 = 1 ? </select>
4.注意事項
a.使用動態(tài)返回字段時,不可使用預先編譯,故foreach中使用‘$’符號,而不使用‘#’;
b.foreach標簽后若還有其他固定字段返回,記得用逗號‘,’分隔。
MyBatis如何返回部分字段
mybatis 返回部分字段,這里介紹兩種方式(主推第一種)
.xml文件中resultMap的type改為
java.util.HashMap 即可
其余代碼都不用寫, 親測可用,xml文件中代碼如下:
? ? <!-- 返回類型 ?--> ?? ?<resultMap id="testRestMap" type="java.util.HashMap"> ?? ??? ? <result column="province_name" jdbcType="VARCHAR" property="provinceName" /> ?? ??? ? <result column="area_name" jdbcType="VARCHAR" property="areaName" /> ?? ??? ? <result column="lane_num" jdbcType="VARCHAR" property="laneNum" /> ?? ??? ? <result column="road_name" jdbcType="VARCHAR" property="roadName" /> ?? ?</resultMap> ?? ? ?? ?<!--sql查詢 ?--> ?? ?<select id="selTest" ?resultMap="testRestMap"> ?? ??? ?select? ? ? ? ? ? ? province_name, ? ? ? ? ? ? area_name,lane_num, ? ? ? ? ? ? road_name? ? ? ? ? from ?site_info ?? ??? ?where 1=1 ?? ??? ?and del_flag='0' ?? ?</select>
第二種很笨的方法
重新新建一個對象實體,該實體文件的字段與要返回的 “部分字段” 一一對應即可,代碼如下:
resultMap 的type=com.sinosoft.pojo.DeviceNumber 指向一個文件,該文件為新建實體類,里面是要返回的 “部分字段”
? ? <!-- 全省設備數(shù)量統(tǒng)計 數(shù)據(jù)返回 --> ?? ?<resultMap id="DeviceNumber" type="com.sinosoft.pojo.DeviceNumber"> ?? ??? ? <result column="distCode" jdbcType="VARCHAR" property="distCode" /> ?? ??? ? <result column="distName" jdbcType="VARCHAR" property="distName" /> ?? ??? ? <result column="number" jdbcType="VARCHAR" property="number" /> ?? ?</resultMap> ?? ? ?? ?<!-- 全省設備數(shù)量統(tǒng)計 --> ?? ?<select id="statisticsDevice" ?resultMap="DeviceNumber"> ?? ??? ?SELECT ?? ??? ? ? ?t1.distCode, ?? ??? ??? ?t1.distName, ?? ??? ??? ?t2.number ?? ??? ?FROM ?? ??? ? ? ?( ?? ??? ? ? ?select? ?? ??? ??? ??? ?fd_objectid distCode, ?? ??? ??? ??? ?area_name ?distName ?? ??? ??? ?FROM t_sys_area? ?? ??? ??? ?WHERE LEVEL='2' ?? ??? ?) t1 LEFT JOIN ( ?? ??? ??? ?SELECT? ?? ??? ??? ??? ?city_no, ?? ??? ??? ??? ?count(*) as number? ?? ??? ??? ?FROM site_info? ?? ??? ??? ?WHERE 1=1 ?? ??? ??? ?and del_flag='0' ?? ??? ??? ?and is_bridge!='1' ?? ??? ??? ?GROUP BY city_no ?? ??? ?)t2 on t2.city_no=t1.distCode ?? ?</select>
DeviceNumber 文件內(nèi)容如下:
package com.sinosoft.pojo; /** ?* 數(shù)據(jù)返回 ?* @author zhangyajuan ?* ?*/ public class DeviceNumber {? ?? ?? ?private String distCode;//行政區(qū)劃 ?? ?private String distName;//行政區(qū)劃名稱 ?? ?private String number;//設備數(shù)量? ?? ?public String getDistCode() { ?? ??? ?return distCode; ?? ?} ?? ?public void setDistCode(String distCode) { ?? ??? ?this.distCode = distCode; ?? ?} ?? ?public String getDistName() { ?? ??? ?return distName; ?? ?} ?? ?public void setDistName(String distName) { ?? ??? ?this.distName = distName; ?? ?} ?? ?public String getNumber() { ?? ??? ?return number; ?? ?} ?? ?public void setNumber(String number) { ?? ??? ?this.number = number; ?? ?} }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis如何按順序查詢出對應的數(shù)據(jù)字段
這篇文章主要介紹了Mybatis如何按順序查詢出對應的數(shù)據(jù)字段,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Java實現(xiàn)InputStream的任意拷貝方式
這篇文章主要介紹了Java實現(xiàn)InputStream的任意拷貝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10基于springboot和redis實現(xiàn)單點登錄
這篇文章主要為大家詳細介紹了基于springboot和redis實現(xiàn)單點登錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06防止未登錄用戶操作—基于struts2攔截器的簡單實現(xiàn)
下面小編就為大家?guī)硪黄乐刮吹卿浻脩舨僮鳌趕truts2攔截器的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10