how2j.cn

相关下载
文件名 文件大小
pagehelper-5.1.0-beta2.jar 78k
jsqlparser-1.0.jar 355k
ssm.rar 10m
使用站长秘制下载工具

步骤 1 : 基于前面的知识点   
步骤 2 : 先运行,看到效果,再学习   
步骤 3 : 模仿和排错   
步骤 4 : 效果   
步骤 5 : jar包   
步骤 6 : 修改applicationContext.xml   
步骤 7 : CategoryService   
步骤 8 : CategoryServiceImpl   
步骤 9 : CategoryMapper   
步骤 10 : Category.xml   
步骤 11 : CategoryController   
步骤 12 : MybatisTest   
步骤 13 : 重启tomcat,测试   

步骤 1 :

基于前面的知识点

本知识点基于SSM 分页进行SSM 分页进行是采用手动SQL方式进行,本知识点将采用PageHelper插件进行。
参考: PageHelper和Mybatis整合教程
步骤 2 :

先运行,看到效果,再学习

老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
步骤 3 :

模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 4 :

效果

访问页面看到如图所示效果

http://127.0.0.1:8080/ssm/listCategory
效果
步骤 5 :

jar包

因为是第三方插件,所以需要额外的jar包,都在右上角提供了下载:pagehelper-5.1.0-beta2.jar,jsqlparser-1.0.jar
下载后放在WEB-INF/lib下
jar包
步骤 6 :

修改applicationContext.xml

增加38-48的PageHelper插件配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config /> <context:component-scan base-package="com.how2java.service" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>admin</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.how2java.pojo" /> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置参数,一行配置一个 --> <value> </value> </property> </bean> </array> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.how2java.mapper"/> </bean> </beans>
步骤 7 :

CategoryService

CategoryService去掉total方法和list(Page) 方法
package com.how2java.service; import java.util.List; import com.how2java.pojo.Category; import com.how2java.util.Page; public interface CategoryService { List<Category> list(); }
package com.how2java.service;

import java.util.List;

import com.how2java.pojo.Category;
import com.how2java.util.Page;

public interface CategoryService {

	List<Category> list();
}
步骤 8 :

CategoryServiceImpl

CategoryServiceImpl去掉total方法和list(Page) 方法
package com.how2java.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.how2java.mapper.CategoryMapper; import com.how2java.pojo.Category; import com.how2java.service.CategoryService; @Service public class CategoryServiceImpl implements CategoryService{ @Autowired CategoryMapper categoryMapper; public List<Category> list(){ return categoryMapper.list(); } }
步骤 9 :

CategoryMapper

CategoryMapper去掉total方法和list(Page) 方法
package com.how2java.mapper; import java.util.List; import com.how2java.pojo.Category; import com.how2java.util.Page; public interface CategoryMapper { public int add(Category category); public void delete(int id); public Category get(int id); public int update(Category category); public List<Category> list(); }
package com.how2java.mapper;
 
import java.util.List;

import com.how2java.pojo.Category;
import com.how2java.util.Page;
 
public interface CategoryMapper {
 
    public int add(Category category);  
       
    public void delete(int id);  
       
    public Category get(int id);  
     
    public int update(Category category);   
       
    public List<Category> list();
      
}
步骤 10 :

Category.xml

Category.xml去掉total对应的sql语句,list也去掉limit
<?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="com.how2java.mapper.CategoryMapper"> <insert id="add" parameterType="Category" > insert into category_ ( name ) values (#{name}) </insert> <delete id="delete" parameterType="Category" > delete from category_ where id= #{id} </delete> <select id="get" parameterType="_int" resultType="Category"> select * from category_ where id= #{id} </select> <update id="update" parameterType="Category" > update category_ set name=#{name} where id=#{id} </update> <select id="list" resultType="Category"> select * from category_ </select> </mapper>
步骤 11 :

CategoryController

CategoryController在调用categoryService.list(); 之前,执行:

PageHelper.offsetPage(page.getStart(),5);

并通过int total = (int) new PageInfo<>(cs).getTotal();获取总数。
其他都不变
package com.how2java.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.support.PagedListHolder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.how2java.pojo.Category; import com.how2java.service.CategoryService; import com.how2java.util.Page; // 告诉spring mvc这是一个控制器类 @Controller @RequestMapping("") public class CategoryController { @Autowired CategoryService categoryService; @RequestMapping("listCategory") public ModelAndView listCategory(Page page){ ModelAndView mav = new ModelAndView(); PageHelper.offsetPage(page.getStart(),5); List<Category> cs= categoryService.list(); int total = (int) new PageInfo<>(cs).getTotal(); page.caculateLast(total); // 放入转发参数 mav.addObject("cs", cs); // 放入jsp路径 mav.setViewName("listCategory"); return mav; } }
步骤 12 :

MybatisTest

MybatisTest 类注释掉用旧方式分页的代码,其实全部都注释掉了
package com.how2java.test; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.how2java.mapper.CategoryMapper; import com.how2java.pojo.Category; import com.how2java.util.Page; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class MybatisTest { // @Autowired // private CategoryMapper categoryMapper; // //// @Test // public void testAdd() { // for (int i = 0; i < 100; i++) { // Category category = new Category(); // category.setName("new Category"); // categoryMapper.add(category); // } // // } // // @Test // public void testTotal() { // int total = categoryMapper.total(); // System.out.println(total); // } // // @Test // public void testList() { // Page p = new Page(); // p.setStart(2); // p.setCount(3); // List<Category> cs=categoryMapper.list(p); // for (Category c : cs) { // System.out.println(c.getName()); // } // } }
步骤 13 :

重启tomcat,测试

重启tomcat,访问地址:

http://127.0.0.1:8080/ssm/listCategory?start=5
重启tomcat,测试


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


问答区域    
2018-01-31 这要什么参数啊
塑花kai不落



看不懂需要什么参数。然后我的项目是有报异常的

							
警告: Failed to scan JAR [file:/E:/practice/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ssm/WEB-INF/lib/jsqlparser-1.0.jar] from WEB-INF/lib
java.util.zip.ZipException: zip file is empty
	at java.util.zip.ZipFile.open(Native Method)
	at java.util.zip.ZipFile.<init>(ZipFile.java:219)
	at java.util.zip.ZipFile.<init>(ZipFile.java:149)
	at java.util.jar.JarFile.<init>(JarFile.java:166)
	at java.util.jar.JarFile.<init>(JarFile.java:103)
	at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:93)
	at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69)
	at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:109)
	at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
	at sun.net.www.protocol.jar.JarURLConnection.getJarFile(JarURLConnection.java:89)
	at org.apache.tomcat.util.scan.FileUrlJar.<init>(FileUrlJar.java:41)
	at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:34)
	at org.apache.catalina.startup.ContextConfig$FragmentJarScannerCallback.scan(ContextConfig.java:2677)
	at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:259)
	at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:178)
	at org.apache.catalina.startup.ContextConfig.processJarsForWebFragments(ContextConfig.java:1913)
	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1271)
	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:888)
	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:388)
	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5522)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:748)

一月 31, 2018 4:02:51 下午 org.apache.catalina.startup.ContextConfig processAnnotationsJar
严重: Unable to process Jar [jar:file:/E:/practice/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ssm/WEB-INF/lib/jsqlparser-1.0.jar!/] for annotations
java.util.zip.ZipException: zip file is empty
	at java.util.zip.ZipFile.open(Native Method)
	at java.util.zip.ZipFile.<init>(ZipFile.java:219)
	at java.util.zip.ZipFile.<init>(ZipFile.java:149)
	at java.util.jar.JarFile.<init>(JarFile.java:166)
	at java.util.jar.JarFile.<init>(JarFile.java:103)
	at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:93)
	at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69)
	at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:109)
	at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
	at sun.net.www.protocol.jar.JarURLConnection.getJarFile(JarURLConnection.java:89)
	at org.apache.tomcat.util.scan.FileUrlJar.<init>(FileUrlJar.java:41)
	at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:34)
	at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1967)
	at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1942)
	at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1927)
	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1332)
	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:888)
	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:388)
	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5522)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:748)

一月 31, 2018 4:02:52 下午 org.apache.catalina.startup.ContextConfig processResourceJARs
严重: Failed to process JAR found at URL [jar:file:/E:/practice/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ssm/WEB-INF/lib/jsqlparser-1.0.jar!/] for static resources to be included in context with name [/ssm]
一月 31, 2018 4:02:52 下午 org.apache.catalina.startup.TldConfig tldScanJar
警告: Failed to process JAR [jar:file:/E:/practice/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ssm/WEB-INF/lib/jsqlparser-1.0.jar!/] for TLD files
java.util.zip.ZipException: zip file is empty
	at java.util.zip.ZipFile.open(Native Method)
	at java.util.zip.ZipFile.<init>(ZipFile.java:219)
	at java.util.zip.ZipFile.<init>(ZipFile.java:149)
	at java.util.jar.JarFile.<init>(JarFile.java:166)
	at java.util.jar.JarFile.<init>(JarFile.java:103)
	at sun.net.www.protocol.jar.URLJarFile.<init>(URLJarFile.java:93)
	at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69)
	at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:109)
	at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
	at sun.net.www.protocol.jar.JarURLConnection.getJarFile(JarURLConnection.java:89)
	at org.apache.tomcat.util.scan.FileUrlJar.<init>(FileUrlJar.java:41)
	at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:34)
	at org.apache.catalina.startup.TldConfig.tldScanJar(TldConfig.java:502)
	at org.apache.catalina.startup.TldConfig.access$200(TldConfig.java:61)
	at org.apache.catalina.startup.TldConfig$TldJarScannerCallback.scan(TldConfig.java:300)
	at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:259)
	at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:178)
	at org.apache.catalina.startup.TldConfig.execute(TldConfig.java:271)
	at org.apache.catalina.startup.TldConfig.lifecycleEvent(TldConfig.java:590)
	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5522)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:748)


1 个答案

塑花kai不落 答案时间:2018-01-31
项目异常解决了,jar包可能下成空的了。这里要的参数还是不明白




答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2017-12-20 关于SSM-PageHelper和不用分页框架的对比提问
她心
这个调用了categoryService.list方法,已经全部把数据从数据库中拿出来了,也就是说PageHelper是操作list来进行分页的而不是数据库,出于性能考虑还是 操作数据库比较好吧?








答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2017-11-24 这个 插件可以选择跳转到多少页吗
2017-11-03 对比答案发现MybatisTest.java有修改没有写出来




提问之前请登陆
关于 JAVA 框架-SSM-PageHelper 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 389538688
站长会在每个工作日早上尽量回答提问(如果有漏掉没有回答的,请进群提醒一下)
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
站长是玻璃心,提问的时候请语气温柔些 kiss~
截止2017-5-19日累计提问 1638个,站长回答了 1546个
截止2017-8-15日累计提问 2788个,站长回答了 2544个

上传截图