欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis動(dòng)態(tài)SQL如何實(shí)現(xiàn)前端指定返回字段

 更新時(shí)間:2022年01月25日 17:20:48   作者:iSetYu  
這篇文章主要介紹了MyBatis動(dòng)態(tài)SQL如何實(shí)現(xiàn)前端指定返回字段,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

動(dòng)態(tài)SQL實(shí)現(xiàn)前端指定返回字段

問(wèn)題描述

在使用ClickHouse時(shí),遇到需要根據(jù)業(yè)務(wù)需求,動(dòng)態(tài)返回指定字段,從而充分利用ClickHouse列式存儲(chǔ)的優(yōu)勢(shì)。

解決方案

可用MyBatis的動(dòng)態(tài)SQL進(jìn)行數(shù)據(jù)返回;故可在業(yè)務(wù)層將指定返回的列傳入數(shù)據(jù)層,利用foreach標(biāo)簽進(jìn)行遍歷返回?cái)?shù)據(jù)。

代碼實(shí)現(xiàn)

1.請(qǐng)求參數(shù)

@Data
public class QueryDTO {
? ? /**
? ? ?* 返回值
? ? ?*/
? ? private Set<String> targetFields;
}

2.mapper接口

?? ?/**
? ? ?* 查詢數(shù)據(jù)
? ? ?* @param record
? ? ?* @return
? ? ?*/
? ? List<ResourceTotal> query(QueryDTO record);

3.mapper.xml實(shí)現(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.注意事項(xiàng)

a.使用動(dòng)態(tài)返回字段時(shí),不可使用預(yù)先編譯,故foreach中使用‘$’符號(hào),而不使用‘#’;

b.foreach標(biāo)簽后若還有其他固定字段返回,記得用逗號(hào)‘,’分隔。 

MyBatis如何返回部分字段

mybatis 返回部分字段,這里介紹兩種方式(主推第一種) 

.xml文件中resultMap的type改為

java.util.HashMap 即可

其余代碼都不用寫(xiě), 親測(cè)可用,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>

第二種很笨的方法

重新新建一個(gè)對(duì)象實(shí)體,該實(shí)體文件的字段與要返回的 “部分字段” 一一對(duì)應(yīng)即可,代碼如下:

resultMap 的type=com.sinosoft.pojo.DeviceNumber   指向一個(gè)文件,該文件為新建實(shí)體類,里面是要返回的 “部分字段”

? ? <!-- 全省設(shè)備數(shù)量統(tǒng)計(jì) 數(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è)備數(shù)量統(tǒng)計(jì) -->
?? ?<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è)備數(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;
?? ?}
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論