1.根目录配置 config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>
<!-- 控制台打印sql语句 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="supportMethodsArguments" value="true" />
</plugin>
</plugins>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbcDriver}" />
<property name="url" value="${jdbcUrl}" />
<property name="username" value="${jdbcUsername}" />
<property name="password" value="${jdbcPassword}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="empMapper.xml"></mapper>
<!-- 使用注解时需要配置映射类 -->
<mapper class="cn.ps.lesson05.page.EmpMapper"></mapper>
</mappers>
</configuration>
- 自动分页需要在根配置中 将supportMethodsArguments的value值设置为true
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="supportMethodsArguments" value="true" />
</plugin>
</plugins>
2.映射配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 相当于package包下面的类 id=queryEmp相当于类下的方法 resultType 声明返回的类型 -->
<mapper namespace="映射的包以及类(cn.ps.lesson05.page.EmpMapper)">
<select id="query" resultType="返回的结果集类型(cn.ps.lesson05.page.Emp_t)">
select * from Emp_t where ename like #{0}
</select>
<select id="queryParatem" resultType="cn.ps.lesson05.page.Emp_t">
select * from Emp_t
</select>
</mapper>
3.定义映射接口
package cn.ps.lesson05.page;
import java.util.List;
import org.apache.ibatis.annotations.Param;
/**
*@author 邹渣渣
*@date 2018年11月14日下午3:35:04
*
*/
public interface EmpMapper {
public List<Emp_t> query(String ename);
**使用参数必须加@Param() 注解的名字必须是pageNum/pageSize **
public List<Emp_t> queryParatem(@Param("pageNum")int pageNum,@Param("pageSize")int pageSize);
}
4.测试方法
package cn.ps.lesson05.page;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
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.PageHelper;
import com.github.pagehelper.PageInfo;
/**
* @author 渣渣
* @date
*
*/
public class TestMyBatis {
public static SqlSession getSession() {
String resource = "conf.xml";
InputStream inputStream = TestMyBatis.class.getResourceAsStream(resource);
// session 负责产生会话
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 会话就是打开和数据库的连接
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
// RowBounds方法调用 分页
@Test
public void testPage() {
SqlSession openSession = getSession();
List<Emp_t> empList = openSession.selectList("cn.ps.lesson05.page.EmpMapper.query", null, new RowBounds(0, 10));
openSession.close();
}
//mapper接口分页方法
//offsetPage(1,2); 使用下标
//startPage(1, 10); 直接使用当前页,每页展示多少条
@Test
public void testPage1() {
SqlSession openSession = getSession();
EmpMapper mapper = openSession.getMapper(EmpMapper.class);
PageHelper.startPage(1, 2);
List<Emp_t> list = mapper.query("%唐%");
PageInfo pi = new PageInfo(list);
//System.out.println("---"+list);
System.out.println(pi.getTotal());
}
//使用参数获取分页时需在根配置中定义supportMethodsArguments
@Test
public void testPagePar() {
SqlSession openSession = getSession();
EmpMapper mapper = openSession.getMapper(EmpMapper.class);
List<Emp_t> list = mapper.queryParatem(2, 10);
System.out.println(list.size());
}
}
依赖管理
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.4</version>
</dependency>