标签:debug gate 文件中 记录 技术 int dynamic input pac
首先引入相关的jar包:
然后是在Mybatis全局配置文件中配置:
<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins>
就可以使用了。
EmployeeMapper.java中
public List<Employee> getAllEmp();
EmployeeMapper.xml中
<select id="getAllEmp" resultType="com.gong.mybatis.bean.Employee"> select * from tbl_employee </select>
进行测试:首先是不带分页的测试
package com.gong.mybatis.test; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.gong.mybatis.bean.Department; import com.gong.mybatis.bean.Employee; import com.gong.mybatis.dao.EmployeeMapper; import com.gong.mybatis.mapper.EmployeeMapperDynamicSql; public class TestMybatis5 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(is); } @Test public void test() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); List<Employee> emps = mapper.getAllEmp(); for(Employee emp:emps) { System.out.println(emp); } } finally { // TODO: handle finally clause openSession.close(); } } }
输出:
DEBUG 01-23 14:29:17,295 ==> Preparing: select * from tbl_employee (BaseJdbcLogger.java:145) DEBUG 01-23 14:29:17,334 ==> Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:29:17,412 <== Total: 7 (BaseJdbcLogger.java:145) Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null] Employee [id=2, lastName=jack, gender=1, email=675544321@qq.com, dept=null] Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null] Employee [id=4, lastName=小明, gender=0, email=xiaoming@qq.com, dept=null] Employee [id=6, lastName=timo, gender=0, email=timo@qq.com, dept=null] Employee [id=7, lastName=manwang, gender=1, email=manwang@qq.com, dept=null] Employee [id=8, lastName=timo, gender=0, email=timo@qq.com, dept=null]
我们共有7条数据。
第一种分页方式:
package com.gong.mybatis.test; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.gong.mybatis.bean.Department; import com.gong.mybatis.bean.Employee; import com.gong.mybatis.dao.EmployeeMapper; import com.gong.mybatis.mapper.EmployeeMapperDynamicSql; public class TestMybatis5 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(is); } @Test public void test() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); //第一个参数为当前页码,第二个参数是每页的记录数 Page<Object> page = PageHelper.startPage(1,5); List<Employee> emps = mapper.getAllEmp(); for(Employee emp:emps) { System.out.println(emp); } System.out.println("当前页码:"+page.getPageNum()); System.out.println("总记录数:"+page.getTotal()); System.out.println("每页几条记录:"+page.getPageSize()); System.out.println("总页码数:"+page.getPages()); } finally { // TODO: handle finally clause openSession.close(); } } }
输出:
DEBUG 01-23 14:32:50,024 Cache Hit Ratio [SQL_CACHE]: 0.0 (LoggingCache.java:62) DEBUG 01-23 14:32:50,071 ==> Preparing: SELECT count(0) FROM tbl_employee (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,128 ==> Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,197 <== Total: 1 (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,199 ==> Preparing: select * from tbl_employee LIMIT 5 (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,199 ==> Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:32:50,212 <== Total: 5 (BaseJdbcLogger.java:145) Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null] Employee [id=2, lastName=jack, gender=1, email=675544321@qq.com, dept=null] Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=null] Employee [id=4, lastName=小明, gender=0, email=xiaoming@qq.com, dept=null] Employee [id=6, lastName=timo, gender=0, email=timo@qq.com, dept=null] 当前页码:1 总记录数:7 每页几条记录:5 总页码数:2
第二种分页方式:
在上一种设置起始页和每页记录的情况下继续增加:
package com.gong.mybatis.test; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.gong.mybatis.bean.Department; import com.gong.mybatis.bean.Employee; import com.gong.mybatis.dao.EmployeeMapper; import com.gong.mybatis.mapper.EmployeeMapperDynamicSql; public class TestMybatis5 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(is); } @Test public void test() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); //第一个参数为当前页码,第二个参数是每页的记录数 Page<Object> page = PageHelper.startPage(1,2); List<Employee> emps = mapper.getAllEmp(); //第一个参数为传入的数据,第二个参数为连续显示多少页 PageInfo<Employee> pageInfo = new PageInfo<>(emps,3); for(Employee emp:emps) { System.out.println(emp); } System.out.println("当前页码:"+pageInfo.getPageNum()); System.out.println("总记录数:"+pageInfo.getTotal()); System.out.println("每页几条记录:"+pageInfo.getPageSize()); System.out.println("总页码数:"+pageInfo.getPages()); System.out.println("是否第一页:"+pageInfo.isIsFirstPage()); System.out.println("是否最后一页:"+pageInfo.isIsLastPage()); System.out.println("连续显示的页码:"); int[] navigatepageNums = pageInfo.getNavigatepageNums(); for (int i = 0; i < navigatepageNums.length; i++) { System.out.println(navigatepageNums[i]); } } finally { // TODO: handle finally clause openSession.close(); } } }
输出:
DEBUG 01-23 14:39:20,125 Cache Hit Ratio [SQL_CACHE]: 0.0 (LoggingCache.java:62) DEBUG 01-23 14:39:20,172 ==> Preparing: SELECT count(0) FROM tbl_employee (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,247 ==> Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,278 <== Total: 1 (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,281 ==> Preparing: select * from tbl_employee LIMIT 2 (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,283 ==> Parameters: (BaseJdbcLogger.java:145) DEBUG 01-23 14:39:20,292 <== Total: 2 (BaseJdbcLogger.java:145) Employee [id=1, lastName=dema, gender=1, email=dema@qq.com, dept=null] Employee [id=2, lastName=jack, gender=1, email=675544321@qq.com, dept=null] 当前页码:1 总记录数:7 每页几条记录:2 总页码数:4 是否第一页:true 是否最后一页:false 连续显示的页码: 1 2 3
mybatis扩展之使用PageHelper插件进行分页(是真好用)
标签:debug gate 文件中 记录 技术 int dynamic input pac
原文地址:https://www.cnblogs.com/xiximayou/p/12230625.html