级联关系实现的形式
- 一对一
- 一对多
案例实操
一对一关系
实现形式 resultType/resultMap
接口办法
/**
* 一对一关联查问 resultType
* @param userId
* @return
*/
UserDto queryUserCardInfoForResultType(@Param("userId")int userId);
sql 查问
<select id="queryUserCardInfoForResultType" resultType="userDto">
SELECT
u.id,
u.user_name AS userName,
u.user_pwd AS userPwd,
u.flag,
u.cid,
c.number
FROM
USER u
LEFT JOIN card c ON u.cid = c.id
WHERE
u.id = #{userId}
</select>
resultType 实现
<select id="queryUserByIdResultType" parameterType="int" resultType="com.xxx.dto.UserDto">
SELECT
u.id,
u.user_name AS userName,
u.create_time,
u.flag,
u.cid AS cid,
c.number
FROM
User u
LEFT JOIN card c u ON u.cid =c.id
WHERE
u.id = #{id}
</select>
resultMap 实现
<!-- 自定义映射关系实现关联 -->
<resultMap id="baseUser" type="com.xxx.mybatis.pojo.User">
<id column="id" property="id" /> <!-- 主键 -->
<!-- 一般例 -->
<result column="user_name" property="userName" />
<result column="true_name" property="trueName" />
<result column="role_name" property="roleName"/>
</resultMap>
<select id="queryUserInfoByIdV2" parameterType="int" resultMap="baseUser" >
SELECT u.id,u.user_name,u.true_name,l.role_name
FROM t_user u JOIN t_user_role r
ON (u.id = r.user_id) JOIN t_role l
on (r.role_id = l.id)
WHERE u.id = #{id}
</select>
resultMap + association 实现
resultMap 映射定义
<resultMap id="baseUserRole" type="userRole">
<id column="id" property="id"></id>
<result column="role_id" property="roleId"></result>
<association property="role" column="role_id" select="queryRoleById"/>
</resultMap>
<resultMap id="baseRole" type="role">
<id column="id" property="id"></id>
<result column="role_name" property="roleName"/>
<result column="role_remark" property="roleRemark"/>
</resultMap>
查问
<select id="queryUserRoleInfoById" parameterType="int" resultMap="baseUserRole">
SELECT id, role_id FROM t_user_role WHERE id = #{id}
</select>
<select id="queryRoleById" parameterType="int" resultMap="baseRole">
SELECT id, role_name , role_remark FROM t_role WHERE id = #{role_id}
</select>
一对多关系
实现形式:resultMap 实现
resultType 有局限,无奈去重,需手动解决。
ResultMap 定义
<resultMap id="baseGoods" type="goods">
<id column="id" property="id"/>
<result column="name" property="goodsName"/>
<result column="promotePrice" property="price"/>
<collection property="ixxxs" column="id" select="queryGoodsIxxxsById"/>
</resultMap>
<resultMap id="baseGoodsImnage" type="GoodsIxxxs">
<id column="id" property="id"></id>
<result column="goods_skuid" property="goodsSkuid" ></result>
<result column="goods_img" property="ixxxUrl" ></result>
</resultMap>
查问
<select id="queryGoodsById" parameterType="int" resultMap="baseGoods" useCache="true">
select id, name , promotePrice FROM yg_goods WHERE id = #{id}
</select>
<select id="queryGoodsIxxxsById" parameterType="int" resultMap="baseGoodsImnage" useCache="true">
SELECT id , goods_skuid , goods_img FROM yg_goods_imgage where goods_skuid = #{id}
</select>
扩大
sql
sql 元素用来定义一个 能够复用的 SQL 语句段,供其它语句调用。比方:
<sql id="User_columns">userId, userName, password</sql>
<!-- 用 include 援用 -->
<select id="findUserById" resultMap="RM_User" >
select <include refid="User_columns"/>
from user where userId = #{userId}
</select>