如何在MyBatis中返回多个select查询?例如,我想返回20个用户的列表,即用户总数,例如1000.在下面的示例中,我使用SQL_CALC_FOUND_ROWS在LIMIT应用之前获取总数,并使用FOUND_ROWS()检索缓存的值第二个选择查询。如何将多个select查询包装到Mybatis中的一个查询中?
<resultMap type="User" id="userResultMap">
<id property="id" column="u_id" />
<result property="username" column="u_username" />
<result property="password" column="u_password" />
<result property="email" column="u_email" />
</resultMap>
<select id="get" parameterType="Integer" resultType="list" resultMap="userResultMap">
SELECT
SQL_CALC_FOUND_ROWS
u.id AS u_id,
u.username AS u_username,
u.password AS u_password,
u.email AS u_email
FROM user u WHERE u.id=#{id}
<if test="startIndex != null and perPage != null">
LIMIT #{startIndex}, #{perPage}
</if>
SELECT FOUND_ROWS(), #{startIndex}, #{perPage};
</select>
我正在考虑添加另一个resultMap,并没有找到一种方法来在Java Spring端进行多次返回。
在Java方面,它像
List<User> get(@Param("id") Integer id, @Param("startIndex") Integer startIndex,
@Param("perPage") Integer perPage);
这不会发生在MyBatis中。你要申请什么逻辑你必须运行2选择查询? – Akhil 2014-09-29 20:29:59
如果只有一次选择就可以完成,那就好多了。在我的情况下,可以在1选择完成吗? – 2014-09-29 20:34:23
为什么你不能有2个不同的