mybatis 的連表查詢one2onejava
1.能夠經過主從表之間的關聯查詢經過主鍵兩次sql
在resultMap映射中的association標籤<association property="info" column="user_id"
javaType="UserInfo"mybatis
再經過select屬性獲得從表對應映射的mapper的getUserInfoByUserId方法
select="com.gezhi.mapper.UserInfoMapper.getUserInfoByUserId">
</association>app
從表的mapperci
<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>get
2.經過left join 連表查詢一次string
去掉select屬性,添加從表的result Mapio
<association property="info" column="user_id"
javaType="UserInfo"
resultMap="com.gezhi.mapper.UserInfoMapper.userInfoMap">
</association>email
從表中的mapperselect
<!-- 聯合查詢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>