编程知识 cdmana.com

Do you know how the mybatis framework implements cascading relationships?

How cascading relationships are implemented

  • one-on-one
  • One to many

Case practice

One to one relationship

Realization way resultType/resultMap

Interface method

/**

  • One to one association query resultType
  • @param userId
  • @return

*/
UserDto queryUserCardInfoForResultType(@Param("userId")int userId);

sql Inquire about

<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 Realization

<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 Realization

<!-- User defined mapping relation realizes Association  -->
<resultMap id="baseUser" type="com.xxx.mybatis.pojo.User">
   <id column="id"  property="id" /> <!-- Primary key -->
   <!-- Common example -->
   <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 Realization

resultMap Mapping definition

<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>

Inquire about

<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>

One-to-many relation

Realization way :resultMap Realization

resultType There are limitations , You can't get rid of it , It needs to be handled manually .

ResultMap Definition

<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>

Inquire about

<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>

Expand

sql

sql The element is used to define a Reusable SQL Statement segment , For other statements to call . such as :

<sql id="User_columns">userId, userName, password</sql>
<!-- use include quote -->
<select id="findUserById" resultMap="RM_User" >    
   select <include refid="User_columns"/>
   from user where userId = #{userId}
</select>

版权声明
本文为[HUIYL1]所创,转载请带上原文链接,感谢

Scroll to Top