本文主要是介绍mybaits 使用pagehelper实现分页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一 pageHelper功能介绍
1.1 概述
1.mybaits可以使用第3方的插件来对功能进行扩展,分页助手pagehelper是将分页的负责操作进行封装,使用简单的方式即可获得分页的相关数据。
1.2 开发步骤
1.导入通用pageHelper的坐标
2.在mybaits的核心配置文件中配置pagehelper的插件
3.测试分页数据
二 操作案例
2.1 工程结构
2.2 配置pom依赖
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope></dependency><!--mywql dueong --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.32</version></dependency><!--mybaits --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><!-- log4j--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- 分页插件--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>3.7.5</version></dependency><dependency><groupId>com.github.jsqlparser</groupId><artifactId>jsqlparser</artifactId><version>0.9.1</version></dependency>
2.3 配置javabean
package com.ljf.mybaits.plugs.pagehelper.bean;/*** @ClassName: User* @Description: TODO* @Author: liujianfu* @Date: 2021/01/23 20:45:36 * @Version: V1.0**/
public class User
{private int id;private String userName;private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User{" +"id=" + id +", userName='" + userName + '\'' +", password='" + password + '\'' +'}';}
}
2.4 配置dao层
package com.ljf.mybaits.plugs.pagehelper.dao;import com.ljf.mybaits.plugs.pagehelper.bean.User;import java.io.IOException;
import java.util.List;public interface UserMapper {List<User> findAll() throws IOException;List<User> findByIds(List<Integer> dataList) throws IOException;
}
2.5 配置service层
1.service的接口层
package com.ljf.mybaits.plugs.pagehelper.service;import com.ljf.mybaits.plugs.pagehelper.bean.User;import java.io.IOException;
import java.util.List;public interface UserService {List<User> findAll() throws IOException;
}
2.service的实现层
package com.ljf.mybaits.plugs.pagehelper.service.impl;import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljf.mybaits.plugs.pagehelper.bean.User;
import com.ljf.mybaits.plugs.pagehelper.dao.UserMapper;
import com.ljf.mybaits.plugs.pagehelper.service.UserService;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** @ClassName: UserServiceImpl* @Description: TODO* @Author: liujianfu* @Date: 2021/01/30 22:00:02 * @Version: V1.0**/
public class UserServiceImpl implements UserService {@Overridepublic List<User> findAll() throws IOException {//加载核心配置文件InputStream resourcesAsStream= Resources.getResourceAsStream("mybaitsConfig.xml");//获得sqlsession 工厂对象SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourcesAsStream);//获得sqlsession对象SqlSession sqlSession=sqlSessionFactory.openSession();//执行sqlUserMapper orgMapper=sqlSession.getMapper(UserMapper.class);//设置分页参数PageHelper.startPage(1,1);List<User> userList=orgMapper.findAll();//获得与分页相关参数PageInfo<User> pageInfo=new PageInfo<User>(userList);System.out.println("当前页:"+pageInfo.getPageNum());System.out.println("煤业显示条数:"+pageInfo.getPageSize());System.out.println("总条数:"+pageInfo.getTotal());System.out.println("上一页:"+pageInfo.getPrePage());System.out.println("下一页:"+pageInfo.getNextPage());System.out.println("是否第一个::"+pageInfo.isIsFirstPage());System.out.println("下一页:"+pageInfo.isIsLastPage());//mybaits 执行更新操作,提交事务sqlSession.commit();//关闭释放资源sqlSession.close();return userList;}
}
2.6 resource下的配置文件
1.userMapper.xml
<?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" >
<!-- <mapper namespace="yonghuMapper"> -->
<mapper namespace="com.ljf.mybaits.plugs.pagehelper.dao.UserMapper"><!-- 查询, 在mybaitsconfig.xml文件中给起了别名 --><select id="findAll" resultType="yonghu">select id,user_name userName,password from tb_user</select></mapper>
2.mybaits-jdbc.properties 配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/nongda
jdbc.username=root
jdbc.password=
3.mybaitsconfig.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="mybaits-jdbc.properties"></properties><!-- 起别名--><typeAliases><typeAlias type="com.ljf.mybaits.plugs.pagehelper.bean.User" alias="yonghu"></typeAlias></typeAliases><!-- 注册类型注册器<typeHandlers><typeHandler handler="com.mybaits.demo.handler.DataTypeTransfer"></typeHandler></typeHandlers>--><!--配置分页插件 --><plugins><plugin interceptor="com.github.pagehelper.PageHelper"><property name="dialect" value="mysql"/></plugin></plugins>
<!-- 数据源环境 --><environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></dataSource></environment></environments><!-- xxxmapper.xml都要在mybaits.config.xml文件中注册--><mappers><mapper resource="com/mybaits/mapper/UserMapper.xml"></mapper></mappers></configuration>
2.7 调用
package com.ljf.mybaits.plugs.pagehelper;import com.ljf.mybaits.plugs.pagehelper.bean.User;
import com.ljf.mybaits.plugs.pagehelper.service.UserService;
import com.ljf.mybaits.plugs.pagehelper.service.impl.UserServiceImpl;import java.io.IOException;
import java.util.List;/*** Hello world!**/
public class App
{public static void main( String[] args ) throws IOException {UserService os=new UserServiceImpl();List<User> orgList=os.findAll();System.out.println("orglist:"+orgList);for(User u:orgList){System.out.println("u:"+u.getUserName());}System.out.println("....");System.out.println( "Hello World!" );}
}
结果:
这篇关于mybaits 使用pagehelper实现分页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!