mybatis 一对一关联 association 返回空值

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

posted @ 2018-06-06 18:07  八方鱼  阅读(3654)  评论(0编辑  收藏  举报