spring boot +mybatis 整合 association报空指针

<resultMap id="userInfoMap" type="com.pwq.www.myProject.user.model.User">
        <id property="id" column="id" />
        <result property="username" column="username"/>
        <result property="password" column="password" />
        <association property="gender">
            <id property="id" column="id" />
            <result property="userId" column="user_id" />
            <result property="gender" column="gender" />
        </association>
    </resultMap>

    <select id="selectUserInfo" resultMap="userInfoMap">
        SELECT * FROM t_user u LEFT JOIN t_gender g ON (u.id = g.user_id) WHERE u.id = 1
    </select>
    

clipboard.png

clipboard.png

然后我调用了selectUserInfo方法报了空指针

clipboard.png
实际是有值的

clipboard.png

不知道哪里错了?

阅读 3.9k
1 个回答

resultMap配置不正确,需要增加关联的gender的类型javaType="com.pwq.www.myProject.user.model.Gender"

<resultMap id="userInfoMap" type="com.pwq.www.myProject.user.model.User">
        <id property="id" column="id" />
        <result property="username" column="username"/>
        <result property="password" column="password" />
        <association property="gender" javaType="com.pwq.www.myProject.user.model.Gender">
            <id property="id" column="id" />
            <result property="userId" column="user_id" />
            <result property="gender" column="gender" />
        </association>
    </resultMap>

com.pwq.www.myProject.user.model.Gender 这个是猜的包名,需要再确认下是否是这个。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题