赞
踩
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
select * from user where id limit a, b<dependencies> <!--数据库连接--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.7</version> </dependency> <!--Lombok插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency> </dependencies>
学生实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name;
private Integer age;
}
分页实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageInfo<T> {
private Long currentPage;//当前页数
private Integer pageSize;//每页记录数
private Long totalSize;//总记录数
private Long totalPage;//总页数
private List<T> data;//当前页数据
}
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mydb1
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
public class JDBCUtils { private static DataSource dataSource; static { Properties properties = new Properties(); try { properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties")); } catch (IOException e) { e.printStackTrace(); } try { dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } public static void release(Connection connection, Statement statement, ResultSet resultSet) { if (null != connection) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } connection = null; } if (null != statement) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } statement = null; } if (null != resultSet) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } resultSet = null; } } public static void release(Connection connection, Statement statement) { release(connection, statement, null); } }
编写DemoDao接口
该接口有两个功能,一个是分页查询数据,另一个是查询总记录数
public interface DemoDao {
/**
* 分页查询
* @param begin 起始查找的位置
* @param pageSize 要查找的数量
* @return
*/
public List<Student> findPage(Long begin,Integer pageSize);
/**
* 查询总记录数
* @return
*/
public Long getTotalSize();
}
编写DemoDao的实现类,DemoDaoImpl.java
public class DemoDaoImpl implements DemoDao { /** * 分页查询 * @param begin 起始查找的位置 * @param pageSize 要查找的数量 * @return */ public List<Student> findPage(Long begin, Integer pageSize) { Connection connection = null; try { // 1.连接数据库 connection = JDBCUtils.getConnection(); // 2.创建查询语句 QueryRunner queryRunner = new QueryRunner(); List<Student> student = queryRunner.query( connection, "select * from student limit ?,?", new BeanListHandler<Student>(Student.class), begin, pageSize ); return student; } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.release(connection, null); } return null; } /** * 查询总记录数 * @return */ public Long getTotalSize() { Connection connection = null; try { // 创建连接 connection = JDBCUtils.getConnection(); // 查询总记录 return new QueryRunner() .query( connection, "select count(*) from student", new ScalarHandler<Long>() ); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.release(connection, null); } return -1L; } }
编写DemoService接口
public interface DemoService {
/**
* 分页查询
* @param currentPage
* @param pageSize
* @return
*/
public PageInfo<Student> findPage(Long currentPage, Integer pageSize);
}
编写DemoService接口实现类DemoServiceImpl.java
public class DemoServiceImpl implements DemoService { public PageInfo<Student> findPage(Long currentPage, Integer pageSize) { DemoDao dao = new DemoDaoImpl(); // 封装数据 PageInfo<Student> pageInfo = new PageInfo<Student>(); // 当前页数 pageInfo.setCurrentPage(currentPage); // 每页记录数 pageInfo.setPageSize(pageSize); // 总记录数 Long totalSize = dao.getTotalSize(); pageInfo.setTotalSize(totalSize); // 总页数 Long totalPage = (totalSize % pageSize == 0 ) ? totalSize / pageSize : totalSize / pageSize + 1; pageInfo.setTotalPage(totalPage); // 当前页的数据 Long begin = (currentPage - 1) * pageSize; List<Student> data = dao.findPage(begin, pageSize); pageInfo.setData(data); return pageInfo; } }
public class DemoServiceTest {
@Test
public void findPage() {
DemoService demoService = new DemoServiceImpl();
PageInfo<Student> page = demoService.findPage(1L, 2);
System.out.println("page = " + page);
}
}
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.3.0</version> </dependency> </dependencies>
Student.java
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Student {
private Integer id;
private String name;
private Integer age;
}
url=jdbc:mysql://localhost:3306/mydb1?useSSL=false
username=root
password=root
driver=com.mysql.cj.jdbc.Driver
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <!--configuration 核心根标签--> <configuration> <properties resource="jdbc.properties"></properties> <!-- 配置核心配置文件 --> <plugins> <!-- PageInterceptor : 拦截查询所有的sql语句, 在后面拼接分页查询的sql --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--已经在第一页, 点击上一页, 返回第一页数据--> <property name="reasonable" value="true"/> <!--不同类型的数据库的分页查询的sql语句不一样--> <property name="helperDialect" value="mysql"/> </plugin> </plugins> <!--environments配置数据库环境,环境可以有多个。default属性指定使用的是哪个--> <environments default="development"> <!--environment配置数据库环境 id属性唯一标识--> <environment id="development"> <!-- transactionManager事务管理。 type属性,采用JDBC默认的事务--> <transactionManager type="JDBC"/> <!-- dataSource数据源信息 type属性 连接池--> <dataSource type="POOLED"> <!-- property获取数据库连接的配置信息 --> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!--Mapper映射文件--> <mappers> <!-- mapper 引入指定的映射配置文件 resource属性指定映射配置文件的名称 --> <mapper resource="com/batis/dao/StudentDao.xml"/> </mappers> </configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.batis.dao.StudentDao">
<!--
id:Mapperp中唯一标识
parameterType:输入映射的类型
resultType:输出映射的类型
-->
<select id="findAll" resultType="com.batis.pojo.Student">
select * from student
</select>
</mapper>
编写StudentDao.java
public interface StudentDao {
/**
* 查询所有
* @return
*/
List<Student> findAll();
}
编写StudentService接口
public interface StudentService {
PageInfo<Student> findPage(Integer currentPage,Integer pageSize) throws IOException;
}
编写StudentService接口的实现类
public class StudentServiceImpl implements StudentService { public PageInfo<Student> findPage(Integer currentPage, Integer pageSize) throws IOException { //开始分页查询 , 触发PageInterceptor PageHelper.startPage(currentPage,pageSize); // 查询所有 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(Resources.getResourceAsStream("mybatis-config.xml")); SqlSession sqlSession = sqlSessionFactory.openSession(); StudentDao studentDao = sqlSession.getMapper(StudentDao.class); List<Student> all = studentDao.findAll(); // 分页查询 return new PageInfo<Student>(all); } }
public class StudentServiceTest {
@Test
public void findPage() throws IOException {
StudentServiceImpl studentService = new StudentServiceImpl();
PageInfo<Student> page = studentService.findPage(1, 2);
System.out.println("page = " + page);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。