没有合适的资源?快使用搜索试试~ 我知道了~
一、引言Mybatis提供了强大的分页拦截实现,可以完美的实现分功能二、普通的分页实现普通分页实现即使直接在mapper文件中写分页查询语句Messsage.x
资源详情
资源评论
资源推荐
一、引言
Mybatis 提供了强大的分页拦截实现,可以完美的实现分功能
二、普通的分页实现
普通分页实现即使直接在 mapper 文件中写分页查询语句
Messsage.xml
[html] view plain copy
1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE mapper
3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5.
6. <mapper namespace="com.imooc.bean.Message">
7.
8. <resultMap type="com.imooc.bean.Message" id="MessageResult">
9. <id column="id" jdbcType="INTEGER" property="id"/>
10. <result column="command" jdbcType="VARCHAR" property="command"/>
11. <result column="description" jdbcType="VARCHAR" property="description"/>
12. <result column="content" jdbcType="VARCHAR" property="content"/>
13. </resultMap>
14.
15. <!--
16. 普通分页实现
17. 使用 sql 实现分页查询,不是使用拦截器
18. oracle 分页使用 rownum,子查询呢需要使用别名不能使用 rownum,row 等关键字
19. -->
20. <select id="queryMessageListByPage" parameterType="java.util.Map" resultMa
p="MessageResult">
21. select * from ( select rownum r, id,command,description,content from mes
sage
22. <where>
23. <if test="command != null and command.trim() != ''">
24. and command=#{command}
25. </if>
26. <if test="descriptioin != null and description.trim() != '' ">
27. and descriptioin=#{descriptioin}
28. </if>
29. <if test="page != null">
30. and rownum <= #{page.dbNumber}
31. </if>
32. </where>
33. )
34. <where>
35. and r > #{page.dbIndex}
36. </where>
37. order by id
38. </select>
39. <select id="count" parameterType="com.imooc.bean.Message" resultType="int
">
40. select count(*) from message
41. <where>
42. <if test="command != null and !"".equals(command.trim())">
43. and command=#{command}
44. </if>
45. <if test="description != null and ''!=description.trim()">
46. and description like concat(concat('%',#{description}),'%')
47. </if>
48. </where>
49. </select>
50. </mapper>
Page.Java 分页相关的参数都通过该类对象设置
[java] view plain copy
1. package com.imooc.common;
2.
3. /**
4. * 分页对应的实体类
5. */
6. public class Page {
7. /**
8. * 总条数
9. * */
10. private int totalNumber;
11.
12. /**
13. * 总页数
14. * */
15. private int totalPage;
16.
17. /**
18. * 当前页
19. * */
20. private int currentPage;
21.
22. /**
23. * 每页显示的数目
24. * */
25. private int pageNumber = 5;
26.
27. /**
28. * 数据库中 limit 的参数,从第几条开始取
29. * */
30. private int dbIndex;
31.
32. /**
33. * 数据库中 limit 的参数,一共取多少条,适用于 mysql, 如果是 oracle 则表示最大取到
的条数
34. * */
35. private int dbNumber;
36.
37. /**
38. * 根据当前对象中的属性值计算并设置相关属性的值
39. * */
40.
41. public void count() {
42. // 计算总页数
43. int totalPageTemp = this.totalNumber / this.pageNumber;
44. int plus = (this.totalNumber % this.pageNumber) == 0 ? 0 : 1;
45. totalPageTemp += plus;
46. // 如果总页数小于 0 显示第一页
47. if (totalPageTemp <= 0) {
48. totalPageTemp += 1;
49. }
50. this.totalPage = totalPageTemp;
51. //设置 limit 参数
52. this.dbIndex = (this.currentPage -1 ) * this.pageNumber;
53. this.dbNumber = this.pageNumber;
54. }
55.
56. public int getTotalNumber() {
57. return totalNumber;
58. }
59.
60. public void setTotalNumber(int totalNumber) {
61. this.totalNumber = totalNumber;
62. count();
63. }
64.
65. public int gettotalPage() {
66. return totalPage;
67. }
68.
69. public void settotalPage(int totalPage) {
70. this.totalPage = totalPage;
71. }
72.
73. public int getCurrentPage() {
74. return currentPage;
75. }
76.
77. public void setCurrentPage(int currentPage) {
78. this.currentPage = currentPage;
79. count();
80. }
81.
82. public int getPageNumber() {
83. return pageNumber;
84. }
85.
86. public void setPageNumber(int pageNumber) {
87. this.pageNumber = pageNumber;
88. }
89.
90. public int getDbIndex() {
91. return dbIndex;
92. }
93.
94. public void setDbIndex(int dbIndex) {
95. this.dbIndex = dbIndex;
96. }
97.
98. public int getDbNumber() {
99. return dbNumber;
100. }
101.
102. public void setDbNumber(int dbNumber) {
103. this.dbNumber = dbNumber;
104. }
105.
106. }
Dao 层实现
[java] view plain copy
1. package com.imooc.dao;
2.
3. import java.util.HashMap;
4. import java.util.List;
5. import java.util.Map;
6.
7. import org.apache.ibatis.session.SqlSession;
8. import com.imooc.bean.Message;
9. import com.imooc.common.Page;
10. import com.imooc.db.DBAccess;
11.
12. public class MessageDao {
13.
14. DBAccess dbAccess = new DBAccess();
15.
16. //计算查询的数据的总数
17.
18. public int getMessageCount(String command,String description) throw
s Exception{
19. int count = 0;
20. SqlSession session = null;
21. try {
22. session = dbAccess.getSqlSession();
23. Message message = new Message();
24. message.setCommand(command);
25. message.setDescription(description);
26. count = session.selectOne(Message.class.getName()+".count", mess
age);
27. } catch (Exception e) {
28. throw new Exception(e.getMessage());
29. } finally {
剩余20页未读,继续阅读
开眼旅行精选
- 粉丝: 12
- 资源: 327
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0