mybatis 的连表查询one2one

1.可以通过主从表之间的关联查询通过主键两次

在resultMap映射中的association标签<association property="info" column="user_id"
   javaType="UserInfo"

再通过select属性得到从表对应映射的mapper的getUserInfoByUserId方法
   select="com.gezhi.mapper.UserInfoMapper.getUserInfoByUserId">
  </association>

从表的mapper

<sql id="userInfoSql">
  info_id,info_nickname,info_phone,info_email,info_gender,info_address
 </sql>
 
 <select id="getUserInfoByUserId" resultMap="userInfoMap"
  parameterType="int">
  select
  <include refid="userInfoSql"></include>
  from user_info where user_id=#{id}
 </select>

2.通过left join 连表查询一次

去掉select属性,添加从表的result Map

<association property="info" column="user_id"
   javaType="UserInfo"
   resultMap="com.gezhi.mapper.UserInfoMapper.userInfoMap">
  </association>

从表中的mapper

 <!-- 联合查询users表中的信息,双向关联 -->
 <resultMap type="UserInfo" id="userInfoMap">
  <id property="infoId" column="info_id" javaType="integer" />
  <result property="nickName" column="info_nickname"
   javaType="java.lang.String" />
  <result property="phone" column="info_phone"
   javaType="java.lang.String" />
  <result property="Email" column="info_email" javaType="string" />
  <result property="gender" column="info_gender"
   javaType="string" />
  <result property="address" column="info_address"
   javaType="java.lang.String" />
  <association property="user" column="user_id"
   javaType="User" resultMap="com.gezhi.mapper.UserMapper.userResultMap">
  </association>
 </resultMap>

 

posted on 2019-07-03 15:40  square凉  阅读(234)  评论(0编辑  收藏  举报