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 表的关系是网址:yii666.com<

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 ,具体结构如下:文章地址https://www.yii666.com/article/758260.html

 <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文章来源地址:https://www.yii666.com/article/758260.html

怀疑是两个类某些字段的属性存在相同命名的问题,但是觉得不应该如此low,但还是试着在association节中单独加上result节声明,如下:网址:yii666.com

<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名)”,代码如下:文章来源地址https://www.yii666.com/article/758260.html

<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

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

mybatis 一对一关联 association 返回空值-相关文章

  1. Mybatis表关联多对一

  2. Mybatis表关联一对多、多对一、多对多

  3. 04—mybatis的关联映射

  4. mybatis 一对一关联 association 返回空值

  5. Java基础-SSM之mybatis一对一关联

  6. mybatis动态拼接条件的技巧 where 1=1 或者where标签

    /**     * 根据输入的学生信息进行条件检索     * 1. 当只输入用户名时, 使用用户名进行模糊检索;     * 2. 当只输入邮箱时, 使用性别进行完全匹配     * 3. 当用户名和性别都存在时, 用这两个条件进行查询匹配的用     * @param student     * @return 

  7. mybatis collection的使用

    Mybatis collection的使用今天学习了mybatis中的collection使用,作为记录以后使用。首先看一下javabean的结构!public class Article{    private User user;    private String name;    private int id;    private Date time;public class User{    private int id;    private String name;    private ListArticle article;用户

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png