步骤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下
步骤 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();
}
|
步骤 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();
}
|
步骤 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
|
更多内容,点击了解: https://how2j.cn/k/ssm/ssm-pagehelper/1373.html