mybatis 一對一關聯 association 返回空值
最近學習spring mvc + mybatis開發,看的書是《Spring MVC+Mybatis開發 從入門到精通》,在學習一對一關聯,並且延遲加載一節的時候,使用書上講解的例子無法調通,主要代碼問題是在mapper.xml文件中,部分如下:
<resultMap id="BaseResultMap" type="com.pp.entity.SysUser"> <id column="F_Id" jdbcType="VARCHAR" property="fId" /> <result column="F_Account" jdbcType="VARCHAR" property="fAccount" /> <result column="F_RealName" jdbcType="VARCHAR" property="fRealname" /> <result column="F_NickName" jdbcType="VARCHAR" property="fNickname" /> <result column="F_HeadIcon" jdbcType="VARCHAR" property="fHeadicon" /> <result column="F_Gender" jdbcType="TINYINT" property="fGender" /> <result column="F_Birthday" jdbcType="TIMESTAMP" property="fBirthday" /> <result column="F_MobilePhone" jdbcType="VARCHAR" property="fMobilephone" /> <result column="F_Email" jdbcType="VARCHAR" property="fEmail" /> <result column="F_WeChat" jdbcType="VARCHAR" property="fWechat" /> <result column="F_ManagerId" jdbcType="VARCHAR" property="fManagerid" /> <result column="F_SecurityLevel" jdbcType="INTEGER" property="fSecuritylevel" /> <result column="F_Signature" jdbcType="VARCHAR" property="fSignature" /> <result column="F_OrganizeId" jdbcType="VARCHAR" property="fOrganizeid" /> <result column="F_DepartmentId" jdbcType="VARCHAR" property="fDepartmentid" /> <result column="F_RoleId" jdbcType="VARCHAR" property="fRoleid" /> <result column="F_DutyId" jdbcType="VARCHAR" property="fDutyid" /> <result column="F_IsAdministrator" jdbcType="TINYINT" property="fIsadministrator" /> <result column="F_SortCode" jdbcType="INTEGER" property="fSortcode" /> <result column="F_DeleteMark" jdbcType="TINYINT" property="fDeletemark" /> <result column="F_EnabledMark" jdbcType="TINYINT" property="fEnabledmark" /> <result column="F_Description" jdbcType="VARCHAR" property="fDescription" /> <result column="F_CreatorTime" jdbcType="TIMESTAMP" property="fCreatortime" /> <result column="F_CreatorUserId" jdbcType="VARCHAR" property="fCreatoruserid" /> <result column="F_LastModifyTime" jdbcType="TIMESTAMP" property="fLastmodifytime" /> <result column="F_LastModifyUserId" jdbcType="VARCHAR" property="fLastmodifyuserid" /> <result column="F_DeleteTime" jdbcType="TIMESTAMP" property="fDeletetime" /> <result column="F_DeleteUserId" jdbcType="VARCHAR" property="fDeleteuserid" /> <result column="F_CardNo" jdbcType="VARCHAR" property="fCardno" /> <association property="userLogon" javaType="com.pp.entity.SysUserLogon" select="findSysUserLogonById" column="F_Id"> </association> </resultMap> <!-- 加載sysuserlogon --> <select id="findSysUserLogonById" resultType="com.pp.entity.SysUserLogon" parameterType="java.lang.String"> select * from sys_userlogon where F_UserId = #{value} </select>
sys_user 表和 sys_userlogon 表的關系是
sys_user.f_id = sys_userlogon.f_userid
一對一關聯使用的是association配置,property指向sys_user實體類的userLogon屬性,如下:
<association property="userLogon" javaType="com.pp.entity.SysUserLogon" select="findSysUserLogonById" column="F_Id"> </association>
關聯的sql語句id是 findSysUserLogonById ,具體結構如下:
<select id="findSysUserLogonById" resultType="com.pp.entity.SysUserLogon" parameterType="java.lang.String"> select * from sys_userlogon where F_UserId = #{value} </select>
調試過程中在控制台中確實看到了延遲加載執行的sql是正常的,但是SysUser實體中的 userLogon卻始終為null
懷疑是兩個類某些字段的屬性存在相同命名的問題,但是覺得不應該如此low,但還是試着在association節中單獨加上result節聲明,如下:
<association property="userLogon" javaType="com.pp.entity.SysUserLogon" select="findSysUserLogonById" column="F_Id"> <id column="F_Id" property="fId" jdbcType="VARCHAR" /> <result column="F_UserId" property="fUserid" jdbcType="VARCHAR" /> <result column="F_UserPassword" property="fUserpassword" jdbcType="VARCHAR" /> <result column="F_UserSecretkey" property="fUsersecretkey" jdbcType="VARCHAR" /> </association>
結果依然不行,后來參考網上相同問題的答案,單獨增加一個resultMap,修改 findSysUserLogonById的select配置節,將resultType=“com.pp.entity.SysUserLogon”去掉,換成resultMap=“(新定義的SysUserLogon的resultMap名)”,代碼如下:
<resultMap id="userLogonMap" type="com.pp.entity.SysUserLogon"> <id column="F_Id" property="fId" jdbcType="VARCHAR" /> <result column="F_UserId" property="fUserid" jdbcType="VARCHAR" /> <result column="F_UserPassword" property="fUserpassword" jdbcType="VARCHAR" /> <result column="F_UserSecretkey" property="fUsersecretkey" jdbcType="VARCHAR" /> <result column="F_AllowStartTime" property="fAllowstarttime" jdbcType="TIMESTAMP" /> <result column="F_AllowEndTime" property="fAllowendtime" jdbcType="TIMESTAMP" /> <result column="F_LockStartDate" property="fLockstartdate" jdbcType="TIMESTAMP" /> <result column="F_LockEndDate" property="fLockenddate" jdbcType="TIMESTAMP" /> <result column="F_FirstVisitTime" property="fFirstvisittime" jdbcType="TIMESTAMP" /> <result column="F_PreviousVisitTime" property="fPreviousvisittime" jdbcType="TIMESTAMP" /> <result column="F_LastVisitTime" property="fLastvisittime" jdbcType="TIMESTAMP" /> <result column="F_ChangePasswordDate" property="fChangepassworddate" jdbcType="TIMESTAMP" /> <result column="F_MultiUserLogin" property="fMultiuserlogin" jdbcType="TINYINT" /> <result column="F_LogOnCount" property="fLogoncount" jdbcType="INTEGER" /> <result column="F_UserOnLine" property="fUseronline" jdbcType="TINYINT" /> <result column="F_Question" property="fQuestion" jdbcType="VARCHAR" /> <result column="F_AnswerQuestion" property="fAnswerquestion" jdbcType="VARCHAR" /> <result column="F_CheckIPAddress" property="fCheckipaddress" jdbcType="TINYINT" /> <result column="F_Language" property="fLanguage" jdbcType="VARCHAR" /> <result column="F_Theme" property="fTheme" jdbcType="VARCHAR" /> </resultMap> <!-- 加載sysuserlogon --> <select id="findSysUserLogonById" resultMap="userLogonMap" parameterType="java.lang.String"> select * from sys_userlogon where F_UserId = #{value} </select>
這樣調整之后,association關聯的對象不再為null