mybatis-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>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers\StudentMapper.xml"></mapper>
</mappers>
</configuration>
@Test
public void ListStudentByPage1() throws IOException{
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentInterface mapper = sqlSession.getMapper(StudentInterface.class);
PageHelper.startPage(3 ,3);
List<Student> list = mapper.listStudents();
PageInfo<Student> pageInfo=new PageInfo<Student>(list);
List<Student> student=pageInfo.getList();
for (Student stu:student) {
System.out.println(stu);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-demo1</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
</dependencies>
</project>
import org.apache.ibatis.annotations.Param;
import org.mybatis.Student.Student;
import java.util.List;
public interface StudentInterface {
public int insertStudent(Student student);
public int deleteStudent(String ename);
public int UpdateStudent(@Param("bouns") double bouns,@Param("ename") String ename);
public List<Student> listStudents();
public List<Student> queryStudents(String ename);
public int CountStudents();
public int CountStudents1(String ename);
public List<Student> listStudentByPage(@Param("start") int start,@Param("end") int end);
}
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class Student {
private String ename;
private int jod_id;
private int mgr;
private double salary;
private double bouns;
private int dept_id;
public Student(String ygl, int i, int i1, double v, double v1, int i2) {
ename=ygl;jod_id=i;mgr=i1;salary=v;bouns=v1;dept_id=i2;
}
}