当Mybatis中涉及到1对多查询时,PageHelp分页会失败,具体说明参见:
本文章说明参见 mybatis 使用 PageHelper 实现一对多正确分页
本文只贴示例代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bms.dao.UserDao">
<resultMap id="UserMap" type="User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="telephone" jdbcType="VARCHAR" property="telephone" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="enabled" jdbcType="BOOLEAN" property="enabled" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="userface" jdbcType="VARCHAR" property="userface" />
<result column="remark" jdbcType="VARCHAR" property="remark" />
</resultMap>
<resultMap id="lazyLoadRolesMap" type="User" extends="UserMap">
<collection property="roles" ofType="Role" select="com.bms.dao.UserDao.getRolesById" column="id"/>
</resultMap>
<resultMap id="eagerLoadRolesMap" type="User" extends="UserMap">
<collection property="roles" ofType="Role">
<id column="rid" property="id"/>
<result column="rname" property="name"/>
<result column="rnameZh" property="nameZh"/>
</collection>
</resultMap>
<!-- 因为PageHelper插件不支持1对多查询分页,所以这里需要用到懒加载,进行两次查询 -->
<select id="getUsersByKeyWords" parameterType="String" resultMap="lazyLoadRolesMap">
SELECT id,name,phone,telephone,address,enabled,remark
FROM user
WHERE id NOT IN (
SELECT ur.user_id FROM user_role ur,role r
WHERE ur.role_id=r.id
AND r.name = '${@com.bms.util.FinalName@ROLE_ADMIN}'
)
<if test="keyWords != 'all'">
AND name LIKE concat('%',#{keyWords},'%')
</if>
ORDER BY id ASC
</select>
</mapper>