
{"id":2868,"date":"2020-12-30T14:02:53","date_gmt":"2020-12-30T06:02:53","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2868"},"modified":"2020-12-30T14:02:53","modified_gmt":"2020-12-30T06:02:53","slug":"2020java%e6%a1%86%e6%9e%b6%e6%95%99%e7%a8%8b%e4%b9%8bmybatis%e5%88%86%e9%a1%b5","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2868","title":{"rendered":"2020java\u6846\u67b6\u6559\u7a0b\u4e4bmybatis\u5206\u9875"},"content":{"rendered":"<h2>2020java\u6846\u67b6\u6559\u7a0b\u4e4bmybatis\u5206\u9875<\/h2>\n<p><strong>1.\u5bfc\u5165\u4f9d\u8d56<\/strong><\/p>\n<pre>&lt;dependency&gt;\r\n  &lt;groupId&gt;com.github.pagehelper&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;pagehelper&lt;\/artifactId&gt;\r\n  &lt;version&gt;5.2.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/30\/1.png\" alt=\"\" width=\"680\" \/><br \/>\n<!--more--><br \/>\n<strong>2.\u914d\u7f6emybatis-config.xml\u6587\u4ef6<\/strong><\/p>\n<p><span style=\"color: #ff0000;\">\u5206\u9875\u63d2\u4ef6\u5199\u5728typeAliases\u540e\u9762<\/span><\/p>\n<pre>&lt;!--\u5206\u9875--&gt;\r\n&lt;plugins&gt;\r\n    &lt;plugin interceptor=\"com.github.pagehelper.PageInterceptor\"\/&gt;\r\n&lt;\/plugins&gt;<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/30\/2.png\" alt=\"\" width=\"680\" \/><br \/>\n<strong>3.\u8c03\u7528\u5206\u9875\u65b9\u6cd5<\/strong><\/p>\n<pre>@Test\r\npublic void test1(){\r\n    SqlSession ss = MybatisUtils.getSqlSession();\r\n    StudentMapper sm = ss.getMapper(StudentMapper.class);\r\n    PageHelper.startPage(2,3);\r\n    List&lt;Student&gt; students = sm.queryAllStudent();\r\n    for (Student student : students) {\r\n        System.out.println(student);\r\n    }\r\n}<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/30\/3.png\" alt=\"\" width=\"680\" \/><\/p>\n<p><strong>\u6570\u636e\u5e93\u8868\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/30\/4.png\" alt=\"\" width=\"680\" \/><\/p>\n<p><strong>\u5206\u9875\u67e5\u8be2\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<p>1.\u53ea\u6709\u5728PageHelper.startPage\u65b9\u6cd5\u4e4b\u540e\u7684 \u7b2c\u4e00\u4e2a\u67e5\u8be2\u4f1a\u6709\u6267\u884c\u5206\u9875<br \/>\n2.\u5206\u9875\u63d2\u4ef6\u4e0d\u652f\u6301\u5e26\u6709 for update \u7684\u67e5\u8be2\u8bed\u53e5<br \/>\n3.\u5206\u9875\u63d2\u4ef6\u4e0d\u652f\u6301 \u5d4c\u5957\u67e5\u8be2 \uff0c\u7531\u4e8e\u5d4c\u5957\u67e5\u8be2\u7ed3\u679c\u65b9\u5f0f\u4f1a\u5bfc\u81f4\u7ed3\u679c\u96c6\u88ab\u6298\u53e0\uff0c\u6240\u4ee5\u65e0\u6cd5\u4fdd\u8bc1\u5206\u9875\u7ed3\u679c\u6570\u91cf\u6b63\u786e\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>web\u5206\u9875\u65b9\u6cd5<\/h2>\n<p><strong>1.\u521b\u5efapage\u5b9e\u4f53\u7c7b\uff0c\u5c5e\u6027\u6709pageNum\u548cpageSize\u5e76\u6dfb\u52a0\u6784\u9020\u65b9\u6cd5\u4ee5\u53cagetset\u65b9\u6cd5<\/strong><\/p>\n<pre>public class Page {\r\n    private Integer pageNum=1;\r\n    private Integer pageSize=6;<\/pre>\n<p><strong>2.\u7ed9\u5c5e\u6027\u9644\u4e0a\u9ed8\u8ba4\u503c<\/strong><\/p>\n<p><strong>3.\u521b\u5efa\u63a5\u53e3\u4ee5\u53ca\u5b9e\u73b0\u7c7b<\/strong><\/p>\n<pre>public interface PageMapper {\r\n    PageInfo&lt;Student&gt; queryAllStudent(Page page);\r\n}<\/pre>\n<pre>public class PageMapperImpl implements PageMapper{\r\n    @Override\r\n    public PageInfo&lt;Student&gt; queryAllStudent(Page page) {\r\n        SqlSession sqlSession = MybatisUtils.getSqlSession();\r\n        StudentMapper sm = sqlSession.getMapper(StudentMapper.class);\r\n        PageHelper.startPage(page.getPageNum(),page.getPageSize());\r\n        List&lt;Student&gt; students = sm.queryAllStudent();\r\n        return new PageInfo&lt;&gt;(students);\r\n    }\r\n}<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/30\/5.png\" alt=\"\" width=\"680\" \/><br \/>\n<strong>4.\u521b\u5efaservlet<\/strong><\/p>\n<p><strong>5.\u83b7\u53d6pageNum\u548cpageSize\u53c2\u6570\u5e76\u5224\u65ad<\/strong><\/p>\n<pre>String pageNum = request.getParameter(\"pageNum\");\r\nString pageSize = request.getParameter(\"pageSize\");\r\nPage page=new Page();\r\nif (pageNum != null) {\r\n    page.setPageNum(Integer.parseInt(pageNum));\r\n}\r\nif (pageSize != null) {\r\n    page.setPageSize(Integer.parseInt(pageSize));\r\n}<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/30\/6.png\" alt=\"\" width=\"680\" \/><br \/>\n<strong>6.\u8c03\u7528\u63a5\u53e3\u5b9e\u73b0\u7c7b\uff0c\u521b\u5efaPage\u5bf9\u8c61\u5c06\u53c2\u6570\u4f20\u5165\u5b9e\u73b0\u7c7b\u65b9\u6cd5<\/strong><\/p>\n<p><strong>7.\u5b58\u4f5c\u7528\u57df\u5e76\u8f6c\u53d1<\/strong><\/p>\n<pre>PageMapper pm=new PageMapperImpl();\r\nPageInfo&lt;Student&gt; info = pm.queryAllStudent(page);\r\nsession.setAttribute(\"info\",info);\r\nrequest.getRequestDispatcher(\"\/select.jsp\").forward(request,response);<\/pre>\n<p><strong>8.\u5728jsp\u9875\u9762\u4e2d\u901a\u8fc7\u96c6\u5408\u8fdb\u884c\u904d\u5386<\/strong><\/p>\n<pre>&lt;c:forEach items=\"${sessionScope.info.list}\" var=\"stu\"&gt;<\/pre>\n<p><strong>9.\u5206\u9875\u663e\u793a<\/strong><\/p>\n<pre>&lt;a href=\"${pageContext.request.contextPath}\/qs?pageNum=${info.navigateFirstPage}\"&gt;\u9996\u9875&lt;\/a&gt;\r\n&lt;c:if test=\"${info.isFirstPage==false}\"&gt;\r\n    &lt;a href=\"${pageContext.request.contextPath}\/qs?pageNum=${info.prePage}\"&gt;\u4e0a\u4e00\u9875&lt;\/a&gt;\r\n&lt;\/c:if&gt;\r\n&lt;c:forEach begin=\"1\" end=\"${info.pages}\" var=\"i\"&gt;\r\n    &lt;a href=\"${pageContext.request.contextPath}\/qs?pageNum=${i}\"&gt;${i}&lt;\/a&gt;\r\n&lt;\/c:forEach&gt;\r\n&lt;c:if test=\"${info.isLastPage==false}\"&gt;\r\n    &lt;a href=\"${pageContext.request.contextPath}\/qs?pageNum=${info.nextPage}\"&gt;\u4e0b\u4e00\u9875&lt;\/a&gt;\r\n    &lt;a href=\"${pageContext.request.contextPath}\/qs?pageNum=${info.navigateLastPage}\"&gt;\u5c3e\u9875&lt;\/a&gt;\r\n&lt;\/c:if&gt;<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/30\/7.png\" alt=\"\" width=\"680\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2020java\u6846\u67b6\u6559\u7a0b\u4e4bmybatis\u5206\u9875 1.\u5bfc\u5165\u4f9d\u8d56 &lt;dependency&gt; &lt;gr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2868","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2868"}],"version-history":[{"count":4,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2868\/revisions"}],"predecessor-version":[{"id":2872,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2868\/revisions\/2872"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}