Even table query one2one mybatis framework

mybatis even table query one2one

1. The association between master and slave can query the table primary key twice

ResultMap association tag map <Property association = "info" column = "user_id"
   the javaType = "the UserInfo"

Then obtained from the mapping table corresponding to the select attribute mapper method getUserInfoByUserId
   select = "com.gezhi.mapper.UserInfoMapper.getUserInfoByUserId">
  </ Association>

From mapper table

<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 query table even once

Remove the select attribute, from the added result Map Table

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

From the table 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>

 

Guess you like

Origin www.cnblogs.com/IT-xiaoliang/p/11126828.html