Mybatis如何按順序查詢出對應(yīng)的數(shù)據(jù)字段
Mybatis按順序查詢出對應(yīng)的數(shù)據(jù)字段
今天遇到一個(gè)問題,就是寫xml文件時(shí),返回的順序始終不一致,無論我sql語句寫的如何好,前端接收到的數(shù)據(jù)都是亂的。終于,我發(fā)現(xiàn)到了原因。
原來我的查詢返回resultType = "map" , 也就是這個(gè)map, 打亂了順序。
因?yàn)閙ap 并不保證存入取出順序一致, 因此,打亂順序可想而知了。
解決方法
resultType = "map" 改為 resultType="java.util.LinkedHashMap" 。
介紹:返回為LinkedHashMap時(shí),表中存儲(chǔ)的null值并不會(huì)存入Map中。
Mybatis基本查詢、條件查詢、查詢排序
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.inspur.analysis.tool.ontology.linkType.dao.LinkTypeMapper"> <resultMap type="com.inspur.analysis.tool.ontology.linkType.data.LinkType" id="linkType"> <id property="linkUri" column="LINK_URI"/> <result property="uriHash" column="URI_HASH"/> <result property="baseTypeUri" column="BASE_TYPE_URI"/> <result property="linkLabel" column="LINK_LABEL"/> <result property="isAsymmetrical" column="IS_ASYMMETRICAL"/> <result property="aliase" column="ALIASE"/> <result property="pcName" column="P_C_NAME"/> <result property="pcAliase" column="P_C_ALIASE"/> <result property="cpName" column="C_P_NAME"/> <result property="cpAliase" column="C_P_ALIASE"/> <result property="detailIconUri" column="DETAIL_ICON_URI"/> <result property="detailIcon" column="DETAIL_ICON"/> <result property="edgeIconUri" column="EDGE_ICON_URI"/> <result property="edgeIcon" column="EDGE_ICON"/> <result property="isSys" column="IS_SYS"/> <result property="note" column="NOTE"/> <result property="creatorId" column="CREATOR_ID"/> <result property="createTime" column="CREATE_TIME"/> <result property="editorId" column="EDITOR_ID"/> <result property="editTime" column="EDIT_TIME"/> <result property="scn" column="SCN"/> </resultMap> <select id="existLinkTypeUri" parameterType="String" resultMap="linkType"> SELECT * FROM OD_LINK_TYPE WHERE LINK_URI = #{linkUri} </select> <select id="isRootLinkType" parameterType="String" resultType="int"> SELECT EXISTS(SELECT LINK_URI FROM OD_LINK_TYPE WHERE LINK_URI=BASE_TYPE_URI AND LINK_URI=#{linkUri}) </select> <select id="deleteRootLinkType" parameterType="String"> DELETE FROM OD_LINK_TYPE WHERE BASE_TYPE_URI=#{baseTypeUri} </select> <select id="getRootLinkTypeList" resultMap="linkType"> SELECT * FROM OD_LINK_TYPE WHERE LINK_URI = BASE_TYPE_URI </select> <select id="getAllLinkTypeListByParent" parameterType="java.util.Map" resultMap="linkType"> SELECT * FROM OD_LINK_TYPE <where> LINK_URI != BASE_TYPE_URI <if test="baseTypeUri != null"> AND BASE_TYPE_URI=#{baseTypeUri} </if> </where> <if test="orderfield != null" > ORDER BY <choose> <when test="orderfield == 'linkUri'"> LINK_URI ${orderdir} </when> <when test="orderfield == 'linkLabel'"> LINK_LABEL ${orderdir} </when> <otherwise> BASE_TYPE_URI ${orderdir} </otherwise> </choose> </if> </select> <select id="getLinkTypeListByCondition" parameterType="java.util.Map" resultMap="linkType"> SELECT * FROM OD_LINK_TYPE <where> LINK_URI != BASE_TYPE_URI <if test="linkUri != null"> AND LINK_URI LIKE '%${linkUri}%' </if> <if test="linkLabel != null"> AND LINK_LABEL LIKE '%${linkLabel}%' </if> <if test="baseTypeUri != null"> AND BASE_TYPE_URI=#{baseTypeUri} </if> </where> <if test="orderfield != null" > ORDER BY <choose> <when test="orderfield == 'linkUri'"> LINK_URI ${orderdir} </when> <when test="orderfield == 'linkLabel'"> LINK_LABEL ${orderdir} </when> <otherwise> BASE_TYPE_URI ${orderdir} </otherwise> </choose> </if> </select> </mapper>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA實(shí)現(xiàn)導(dǎo)入module并成功運(yùn)行
這篇文章主要介紹了IDEA實(shí)現(xiàn)導(dǎo)入module并成功運(yùn)行方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04tio-boot框架整合ehcache實(shí)現(xiàn)過程示例
這篇文章主要為大家介紹了tio-boot框架整合ehcache實(shí)現(xiàn)過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12SpringBoot實(shí)現(xiàn)項(xiàng)目文件上傳的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot中實(shí)現(xiàn)項(xiàng)目文件上傳的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2022-11-11SpringBoot項(xiàng)目中使用@Scheduled讀取動(dòng)態(tài)參數(shù)
這篇文章主要介紹了SpringBoot項(xiàng)目中使用@Scheduled讀取動(dòng)態(tài)參數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11簡單學(xué)習(xí)Java抽象類要點(diǎn)及實(shí)例
這篇文章主要介紹了Java抽象類要點(diǎn)及實(shí)例,有需要的朋友可以參考一下2014-01-01解決常見的Eclipse SVN插件報(bào)錯(cuò)方法詳解
本篇文章是對常見的Eclipse SVN插件報(bào)錯(cuò)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
本篇文章主要介紹了Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02springboot項(xiàng)目中application.properties無法變成小樹葉問題解決方案
這篇文章主要介紹了springboot項(xiàng)目中application.properties無法變成小樹葉問題解決,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09