小白mybatis学习(五)MyBatis工具包和分页插件

MyBatis工具包

public class MyBatisUtils {
	private static SqlSessionFactory factory = null;

static {
	String config="mybatis.xml";
	try {
		InputStream in = Resources.getResourceAsStream(config);
		factory = new SqlSessionFactoryBuilder().build(in);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

//获取SqlSession的方法
public static SqlSession getSqlSession() {
	SqlSession sqlSession = null;
	if (factory != null) {
		sqlSession = factory.openSession();
	}
	return sqlSession;
}
}

配置PageHelper

支持多种数据库
在这里插入图片描述
分页
在这里插入图片描述
mybatis通用分页插件

1)maven坐标

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>

2)加入plugin配置(过滤器)

 <plugins>
   	<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
   </plugins>

3)PageHelper对象

查询语句之前调用PageHelper.startPage 静态方法。

除了PageHelper.startPage方法外,还提供了类似用法的PageHelper.offsetPage方法

在你需要进行分页的MyBatis查询方法前调用,PageHelper.startPage静态方法即可,紧跟在这个方法后的第一个MyBatis查询方法会被进行分页。

@Test

public void testSelect() throws IOException{//获取第一页,三条内容PageHelper.startPage(1,3);List<Student> studentList = new Student.selectStudents();

​	studentList.forEach(stu -> System.out.println(stu));

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值