加载中...
加载中...
mybatis一个select标签执行多个select语句

mybatis一个select标签执行多个select语句 原创

使用了limit查询同时又想取得总的记录数。不是获取的实际条数。

从Mysql4.0.0开始,我们可以选择使用另外一个方式:

SELECT SQL_CALC_FOUND_ROWS name, email FROM users WHERE name LIKE ‘a%’ LIMIT 10;
SELECT FOUND_ROWS();  

可参考:https://blog.csdn.net/sytigeryhl/article/details/4830337  

mybatis一个select标签执行多个select语句,  

XML中这样

复制收展XML<resultMap id="BaseResultMap" type="com.leixing.blog.admin.pojo.LxAdminDictDetail" >
<id column="id" property="id" jdbcType="VARCHAR" />
</resultMap>

<resultMap id="longMap" type="java.lang.Long"/>
<select id="selectHotTags" resultMap="BaseResultMap,longMap" resultSets="items,count">
/*#标签分组,标签多的排到前面*/
SELECT
SQL_CALC_FOUND_ROWS
name,
COUNT(1) AS 'sort'
FROM
lx_admin_dict_detail a
WHERE d_id = #{dId}
GROUP BY name
ORDER BY sort DESC
LIMIT #{startIndex}, #{size};
SELECT FOUND_ROWS() AS COUNT;
</select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Mapper层接口

复制收展JavaCollection<Collection<?>> selectHotTags (@Param("dId") String dId, @Param("startIndex") Integer startIndex, @Param("size") Integer size);
  • 1

获取返回值,

其实就是每一个select查询的结果放到一个Collection然后又放到一个Collection中。

获取如下。

复制收展JavaArrayList list = (ArrayList) dictDetailMapper.selectHotTags(dId, startIndex, size);
List<LxAdminDictDetail> lxAds = (ArrayList<LxAdminDictDetail>) list.get(0);
List<Long> conuts = (List<Long>) list.get(1);
/*获取总条数*/
Long conut = conuts.get(0);
  • 1
  • 2
  • 3
  • 4
  • 5


没有更多推荐了 [去首页]
image
文章
357
原创
284
转载
73
翻译
0
访问量
199063
喜欢
47
粉丝
6
码龄
5年
资源
0

文章目录

加载中...
0
1