MyBatis关联查询示例:
<resultMap type="UserView" id="userAndRoleViewResultMap">
<id column="user_id" property="userId"/>
<result column="loginname" property="loginname"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="user_rights" property="rights"/>
<result column="status" property="status"/>
<result column="last_login" property="lastLogin"/>
<association property="role" column="role_id" javaType="Role">
<id column="role_id" property="roleId"/>
<result column="role_name" property="roleName"/>
<result column="role_rights" property="rights"/>
</association>
</resultMap>
<select id="listPageUser" parameterType="User" resultMap="userAndRoleResultMap">
select u.user_id,u.username,u.loginname,u.password,r.role_id,r.role_name ,u.last_login
from tb_user u
left join tb_role r on u.role_id=r.role_id
where u.status=0
<if test="loginname!=null and loginname!=''">
and u.loginname like "%"#{loginname}"%"
</if>
<if test="roleId!=null and roleId!=0">
and u.role_id=#{roleId}
</if>
<if test="lastLoginStart!=null">
and u.last_login>=#{lastLoginStart}
</if>
<if test="lastLoginEnd!=null">
and u.last_login<=#{lastLoginEnd}
</if>
</select>
本文介绍了一个使用MyBatis实现的关联查询示例。通过一个具体的案例展示了如何配置resultMap来映射复杂的关联关系,并在SQL语句中实现左连接及条件过滤。此示例覆盖了参数传递、结果映射等多个方面。
3322

被折叠的 条评论
为什么被折叠?



