Mybatis如何按顺序查询出对应的数据字段
Mybatis按顺序查询出对应的数据字段
今天遇到一个问题,就是写xml文件时,返回的顺序始终不一致,无论我sql语句写的如何好,前端接收到的数据都是乱的。终于,我发现到了原因。
原来我的查询返回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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot项目中使用@Scheduled读取动态参数
这篇文章主要介绍了SpringBoot项目中使用@Scheduled读取动态参数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-11-11Spring Boot使用FastJson解析JSON数据的方法
本篇文章主要介绍了Spring Boot使用FastJson解析JSON数据的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-02-02springboot项目中application.properties无法变成小树叶问题解决方案
这篇文章主要介绍了springboot项目中application.properties无法变成小树叶问题解决,本文通过图文实例代码相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-09-09
最新评论