您好,登錄后才能下訂單哦!
這篇文章主要介紹“mybatis resultmap怎么為對象賦值的調用順序”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“mybatis resultmap怎么為對象賦值的調用順序”文章能幫助大家解決問題。
寫了一個mybatis的mapper映射文件,
public class GroupCourseResult extends GroupResult { private String cid; private String cname; public GroupCourseResult(int stuschool, int nian, String stuclass, String cid, String cname) { super(stuschool, nian, stuclass); this.cid = cid; this.cname = cname; } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } }
<select id="selectFailedCourseRationByGroupIdAndCourseName" resultType="GroupCourseResult"> ... </select>
實體類中的屬性名和查詢的列名完全匹配,但是沒有查詢stuclass,則封裝后的實體類中的stuclass屬性應該為空。
然而程序運行后,stuclass屬性不僅不為空,還與cname完全相同,百思不得其解,故翻了翻mybatis的源碼。
在mybatis中的DefaultResultSetHandler類中,
private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix) throws SQLException { final Class<?> resultType = resultMap.getType(); final MetaClass metaType = MetaClass.forClass(resultType, reflectorFactory); final List<ResultMapping> constructorMappings = resultMap.getConstructorResultMappings(); if (hasTypeHandlerForResultObject(rsw, resultType)) { return createPrimitiveResultObject(rsw, resultMap, columnPrefix); } else if (!constructorMappings.isEmpty()) { return createParameterizedResultObject(rsw, resultType, constructorMappings, constructorArgTypes, constructorArgs, columnPrefix); } else if (resultType.isInterface() || metaType.hasDefaultConstructor()) { return objectFactory.create(resultType); } else if (shouldApplyAutomaticMappings(resultMap, false)) { return createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs); } throw new ExecutorException("Do not know how to create an instance of " + resultType); }
經過調試發現,mybatis會先查找該javabean有無默認構造方法,如果有則采用設值注入,若沒有,則根據javabean的有參構造方法進行設值,而在8以前的jdk版本中,我們利用反射只能獲取到參數類型,不能獲取到參數名稱,這其中設值可能出現了匹配失誤,將cname的值同時賦給了cname和stuclass。
想要解決這個問題,只須在javabean中添加默認構造方法即可。
如果是實體中是直接引用別的對象的具體參數字段,
<resultMap id="baseMap" type="com.ei.medical.modules.model.EduEducationKnowledge"> <id column="id" property="id"/> <result column="visitNumber" property="visitNumber"/> <result column="patientName" property="patientName"/> <result column="sendTime" property="sendTime"/> <result column="wardCode" property="wardCode"/> <result column="wardName" property="wardName"/> <result column="categoryCode" property="categoryCode"/> <result column="title" property="title"/> <result column="content" property="content"/> <result column="cover" property="cover"/> </resultMap> <select id="getAllBy" resultMap="baseMap"> SELECT eer.visit_number as visitNumber, eer.patient_name as patientName, eer.send_time as sendTime, eek.id as id, eek.ward_code as wardCode, eek.ward_name as wardName, eek.category_code as categoryCode, eek.title as title, eek.content as content, eek.cover as cover FROM edu_education_record AS eer, edu_education_knowledge AS eek WHERE eer.education_knowledge_id=eek.id </select>
<resultMap id="baseMap" type="com.ei.medical.modules.model.EduEducationKnowledge"> <id column="id" property="id"/> <result column="visitNumber" property="visitNumber"/> <result column="patientName" property="patientName"/> <result column="sendTime" property="sendTime"/> <result column="wardCode" property="wardCode"/> <result column="wardName" property="wardName"/> <result column="categoryCode" property="categoryCode"/> <result column="title" property="title"/> <result column="content" property="content"/> <result column="cover" property="cover"/> <collection property="pic" ofType="string"> <result column="pic"/> </collection> </resultMap> <select id="getAllBy" resultMap="baseMap"> SELECT eer.visit_number as visitNumber, eer.patient_name as patientName, eer.send_time as sendTime, eek.id as id, eek.ward_code as wardCode, eek.ward_name as wardName, eek.category_code as categoryCode, eek.title as title, eek.content as content, eek.cover as cover FROM edu_education_record AS eer, edu_education_knowledge AS eek WHERE eer.education_knowledge_id=eek.id </select>
如果實體中引用的是別的對象,
<resultMap id="baseMap" type="com.ei.medical.modules.model.EduEducationKnowledge"> <id column="id" property="id"/> <result column="wardCode" property="wardCode"/> <result column="wardName" property="wardName"/> <result column="categoryCode" property="categoryCode"/> <result column="title" property="title"/> <result column="content" property="content"/> <result column="cover" property="cover"/> <association property="eduEducationRecord" javaType="com.ei.medical.modules.model.EduEducationRecord"> <result column="visitNumber" property="visitNumber"/> <result column="patientName" property="patientName"/> <result column="sendTime" property="sendTime"/> </association> </resultMap> <select id="getAllBy" resultMap="baseMap"> SELECT eer.visit_number as visitNumber, eer.patient_name as patientName, eer.send_time as sendTime, eek.id as id, eek.ward_code as wardCode, eek.ward_name as wardName, eek.category_code as categoryCode, eek.title as title, eek.content as content, eek.cover as cover FROM edu_education_record AS eer, edu_education_knowledge AS eek WHERE eer.education_knowledge_id=eek.id </select>
如果實體中是引用的別的對象的list集合,
<resultMap id="baseMap" type="com.ei.medical.modules.model.EduEducationKnowledge"> <id column="id" property="id"/> <result column="wardCode" property="wardCode"/> <result column="wardName" property="wardName"/> <result column="categoryCode" property="categoryCode"/> <result column="title" property="title"/> <result column="content" property="content"/> <result column="cover" property="cover"/> <collection property="eduEducationRecordList" ofType="com.ei.medical.modules.model.EduEducationRecord"> <result column="visitNumber" property="visitNumber"/> <result column="patientName" property="patientName"/> <result column="sendTime" property="sendTime"/> </collection> </resultMap> <select id="getAllBy" resultMap="baseMap"> SELECT eer.visit_number as visitNumber, eer.patient_name as patientName, eer.send_time as sendTime, eek.id as id, eek.ward_code as wardCode, eek.ward_name as wardName, eek.category_code as categoryCode, eek.title as title, eek.content as content, eek.cover as cover FROM edu_education_record AS eer, edu_education_knowledge AS eek WHERE eer.education_knowledge_id=eek.id </select>
tips:
使用resultMap的時候,應該直接用as后面的字段名,即自己命的名字
如果沒有使用as的話,直接使用數據庫中原本的名字
resultMap中各個標簽的含義
tips:
在一個 resultMap 元素中,這些子元素出現的先后順序是有嚴格規定的,它們從前到后依次是:constructor–>id --> result–> association–>collection -->discriminator, 不然就會報錯。
關于“mybatis resultmap怎么為對象賦值的調用順序”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。