文章标题

来源:互联网 发布:java如何使用log4j 编辑:程序博客网 时间:2024/06/03 14:17

1). 引入 Jar 包
你可以从下面的地址中下载最新版本的 jar 包

https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/

http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/

由于使用了sql 解析工具,你还需要下载 jsqlparser.jar:

http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/0.9.5/

2).config.xml的配置:
< configuration>
< plugins>
< plugin interceptor=”com.github.pagehelper.PageHelper”>
< property name=”helperDialect” value=”mysql”/>
< property name=”pageSizeZero” value=”true”/>
< property name=”reasonable” value=”true”/>

< /plugins>

 <mappers>              <mapper resource="sqlMapper.xml"/>         <!-- 我的这个sqlMapper在src根目录下,所以没有包名称--> </mappers>


3).sqlMapper映射文件中我写了一个查询所有的方法:

SELECT * FROM student

4).对应的接口dao写一个查询所有的方法:

//分页测试public List<Student> selectByPageAndSelections();

5).为了方便我把实现方法和测试方法写在一起了

package Test;

 import java.util.List;     import org.apache.ibatis.session.SqlSession;     import com.github.pagehelper.PageHelper;     import com.github.pagehelper.PageInfo;     import common.SqlSessions;     import dao.StudentDao;     import enterty.Student; public class StudentTest {SqlSession session=SqlSessions.getSession();StudentDao sDao=session.getMapper(StudentDao.class); public PageInfo<Student> selectDocByPage1(int currentPage, int pageSize) {        PageHelper.startPage(currentPage, pageSize);//设置当前页,和每页的查询数量        List<Student> list = sDao.selectByPageAndSelections();        PageInfo<Student> pageInfo = new PageInfo<>(list);        return pageInfo; }  //main方法测试 public static void main(String[] args) {    StudentTest test=new StudentTest();    PageInfo<Student> page=test.selectDocByPage1(2,0);    //System.out.println(list.getFirstPage());    List<Student> list = page.getList();    for (Student s : list) {        System.out.println(s.getName());    } }       

}

原创粉丝点击