Mybatis如何按順序查詢出對應的數(shù)據(jù)字段
Mybatis按順序查詢出對應的數(shù)據(jù)字段
今天遇到一個問題,就是寫xml文件時,返回的順序始終不一致,無論我sql語句寫的如何好,前端接收到的數(shù)據(jù)都是亂的。終于,我發(fā)現(xiàn)到了原因。
原來我的查詢返回resultType = "map" , 也就是這個map, 打亂了順序。
因為map 并不保證存入取出順序一致, 因此,打亂順序可想而知了。
解決方法
resultType = "map" 改為 resultType="java.util.LinkedHashMap" 。
介紹:返回為LinkedHashMap時,表中存儲的null值并不會存入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>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
tio-boot框架整合ehcache實現(xiàn)過程示例
這篇文章主要為大家介紹了tio-boot框架整合ehcache實現(xiàn)過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12SpringBoot項目中使用@Scheduled讀取動態(tài)參數(shù)
這篇文章主要介紹了SpringBoot項目中使用@Scheduled讀取動態(tài)參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
本篇文章主要介紹了Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02springboot項目中application.properties無法變成小樹葉問題解決方案
這篇文章主要介紹了springboot項目中application.properties無法變成小樹葉問題解決,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09