(Spring + SpringMVC + Mybatis)
使用到的技术栈:
spring,springMVC,mybatis,jQuery,ajax,bootstrap(前端框架),mysql数据库,基于maven创建工程
项目选型:
商品管理系统:
商品表:
品牌表:
类别表:
需求:
SSM项目整合的时候会出现的问题:
1、我们都知道,mybatis框架中,持久层实现类是通过代理创建出来的,那如何将持久层的实例装进spring中?
我们在ssm项目整合的时候会去导入一个spring整合mybatis的包,这个包有一个扫描器设置,该扫描器会去扫描持久层所有的接口,并为该接口创建代理对象,然后将该实例装进spring中
xxxxxxxxxx
1411 <dependencies>
2
3 <dependency>
4 <groupId>org.springframework</groupId>
5 <artifactId>spring-core</artifactId>
6 <version>5.2.1.RELEASE</version>
7 </dependency>
8
9 <dependency>
10 <groupId>org.springframework</groupId>
11 <artifactId>spring-web</artifactId>
12 <version>5.2.1.RELEASE</version>
13 </dependency>
14
15 <dependency>
16 <groupId>org.springframework</groupId>
17 <artifactId>spring-oxm</artifactId>
18 <version>5.2.1.RELEASE</version>
19 </dependency>
20
21 <dependency>
22 <groupId>org.springframework</groupId>
23 <artifactId>spring-tx</artifactId>
24 <version>5.2.1.RELEASE</version>
25 </dependency>
26
27 <dependency>
28 <groupId>org.springframework</groupId>
29 <artifactId>spring-jdbc</artifactId>
30 <version>5.2.1.RELEASE</version>
31 </dependency>
32
33 <dependency>
34 <groupId>org.springframework</groupId>
35 <artifactId>spring-webmvc</artifactId>
36 <version>5.2.1.RELEASE</version>
37 </dependency>
38
39 <dependency>
40 <groupId>javax.servlet</groupId>
41 <artifactId>servlet-api</artifactId>
42 <version>2.5</version>
43 </dependency>
44
45 <dependency>
46 <groupId>org.springframework</groupId>
47 <artifactId>spring-aop</artifactId>
48 <version>5.2.1.RELEASE</version>
49 </dependency>
50
51 <dependency>
52 <groupId>org.springframework</groupId>
53 <artifactId>spring-context</artifactId>
54 <version>5.2.1.RELEASE</version>
55 </dependency>
56
57 <dependency>
58 <groupId>org.springframework</groupId>
59 <artifactId>spring-test</artifactId>
60 <version>5.2.1.RELEASE</version>
61 </dependency>
62
63 <!--mybatis核心包-->
64 <dependency>
65 <groupId>org.mybatis</groupId>
66 <artifactId>mybatis</artifactId>
67 <version>3.4.5</version>
68 </dependency>
69
70 <!--mybatis/spring包-->
71 <dependency>
72 <groupId>org.mybatis</groupId>
73 <artifactId>mybatis-spring</artifactId>
74 <version>1.3.2</version>
75 </dependency>
76
77 <!--java ee jar包-->
78 <dependency>
79 <groupId>javax</groupId>
80 <artifactId>javaee-api</artifactId>
81 <version>7.0</version>
82 </dependency>
83
84 <!--mysql数据库-->
85 <dependency>
86 <groupId>mysql</groupId>
87 <artifactId>mysql-connector-java</artifactId>
88 <version>8.0.22</version>
89 </dependency>
90
91 <!--日志-->
92 <dependency>
93 <groupId>log4j</groupId>
94 <artifactId>log4j</artifactId>
95 <version>1.2.17</version>
96 </dependency>
97
98 <!--连接池-->
99 <dependency>
100 <groupId>commons-dbcp</groupId>
101 <artifactId>commons-dbcp</artifactId>
102 <version>1.2.2</version>
103 </dependency>
104
105 <dependency>
106 <groupId>com.mchange</groupId>
107 <artifactId>c3p0</artifactId>
108 <version>0.9.5.5</version>
109 </dependency>
110
111 <dependency>
112 <groupId>jstl</groupId>
113 <artifactId>jstl</artifactId>
114 <version>1.2</version>
115 </dependency>
116
117 <dependency>
118 <groupId>com.alibaba</groupId>
119 <artifactId>fastjson</artifactId>
120 <version>1.2.47</version>
121 </dependency>
122
123 <dependency>
124 <groupId>com.fasterxml.jackson.core</groupId>
125 <artifactId>jackson-databind</artifactId>
126 <version>2.9.5</version>
127 </dependency>
128
129 <dependency>
130 <groupId>com.fasterxml.jackson.core</groupId>
131 <artifactId>jackson-annotations</artifactId>
132 <version>2.9.5</version>
133 </dependency>
134
135 <dependency>
136 <groupId>com.fasterxml.jackson.core</groupId>
137 <artifactId>jackson-core</artifactId>
138 <version>2.9.5</version>
139 </dependency>
140
141 </dependencies>
由于导入了mybatis整合spring的包,以及数据库连接池的包,mybatis的配置文件可以不配置
我们将spring和springMVC的配置文件给拆分开来,因此我们要在web.xml中为spring核心容器与springMVC另作配置
注意:在spring核心配置文件和MVC配置文件中都要开启扫描包
xxxxxxxxxx
631
2<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
5 version="4.0">
6
7 <!--配置前端总控制器-->
8 <servlet>
9 <servlet-name>dispatcherServlet</servlet-name>
10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11
12 <!--绑定springMVC核心配置文件-->
13 <init-param>
14 <param-name>contextConfigLocation</param-name>
15 <param-value>classpath:springmvc.xml</param-value>
16 </init-param>
17
18 <!--设置启动级别-->
19 <load-on-startup>1</load-on-startup>
20
21 </servlet>
22
23 <!--拦截前端请求-->
24 <servlet-mapping>
25 <servlet-name>dispatcherServlet</servlet-name>
26 <url-pattern>/</url-pattern>
27 </servlet-mapping>
28
29
30 <!--设置全局属性,绑定spring核心配置文件-->
31 <context-param>
32
33 <param-name>contextConfigLocation</param-name>
34 <param-value>classpath:beans.xml</param-value>
35
36 </context-param>
37
38 <!--配置一个监听器,让项目在启动的时候就去加载spring核心容器-->
39 <listener>
40 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
41 </listener>
42
43 <!--配置一个编码过滤器,拦截所有的资源,将所有资源的字符集设置成utf-8-->
44 <filter>
45
46 <filter-name>characterEncodingFilter</filter-name>
47 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
48
49 <!--将字符集设置成utf-8-->
50 <init-param>
51 <param-name>encoding</param-name>
52 <param-value>utf-8</param-value>
53 </init-param>
54
55 </filter>
56
57 <!--加*代表也拦截静态资源-->
58 <filter-mapping>
59 <filter-name>characterEncodingFilter</filter-name>
60 <url-pattern>/*</url-pattern>
61 </filter-mapping>
62
63</web-app>
xxxxxxxxxx
461
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context.xsd">
10
11 <!--开启扫描包-->
12 <context:component-scan base-package="com.os467.shop"></context:component-scan>
13
14 <!--读取配置文件中的数据,获取数据源信息-->
15 <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
16
17 <!--配置数据源-->
18 <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
19
20 <!--注入源数据-->
21 <property name="driverClassName" value="${db.driver}"></property>
22 <property name="url" value="${db.url}"></property>
23 <property name="username" value="${db.username}"></property>
24 <property name="password" value="${db.password}"></property>
25
26 </bean>
27
28 <!--配置mybatis需要的会话工厂,使用mybatis整合spring的会话工厂-->
29 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
30
31 <!--注入数据源-->
32 <property name="dataSource" ref="basicDataSource"></property>
33 <!--匹配mapper配置文件-->
34 <property name="mapperLocations" value="classpath:com/os467/shop/controller/*.xml"></property>
35
36 </bean>
37
38 <!--mybatis扫描器的配置-->
39 <bean id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
40
41 <!--扫描此包下所有持久层接口,并且通过代理模式创建出每个接口对应的代理对象,并交由spring管理-->
42 <property name="basePackage" value="com.os467.shop.mapper"></property>
43
44 </bean>
45
46</beans>
xxxxxxxxxx
351
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:mvc="http://www.springframework.org/schema/mvc"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xsi:schemaLocation="
7 http://www.springframework.org/schema/beans
8 http://www.springframework.org/schema/beans/spring-beans.xsd
9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context.xsd
11 http://www.springframework.org/schema/mvc
12 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
13
14 <!--开启mvc注解的支持-->
15 <mvc:annotation-driven></mvc:annotation-driven>
16
17 <!--过滤静态资源-->
18 <mvc:default-servlet-handler></mvc:default-servlet-handler>
19
20 <!--扫描包-->
21 <context:component-scan base-package="com.os467.shop"></context:component-scan>
22
23 <!--配置视图解析器-->
24 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
25
26 <!--设置前缀-->
27 <property name="prefix" value="/WEB-INF/jsp/"></property>
28
29 <!--设置后缀-->
30 <property name="suffix" value=".jsp"></property>
31
32 </bean>
33
34
35</beans>
xxxxxxxxxx
41db.url=jdbc:mysql://localhost:3306/web_test?serverTimezone=GMT&characterEncoding=utf-8
2db.driver=com.mysql.cj.jdbc.Driver
3db.username=root
4db.password=root
1、首先在pom.xml文件中引入maven坐标,见上文
刷新pom.xml文件,让maven导入配置
为工程添加web项目支持
2、创建包结构
2.1、在java文件下创建包结构
com.os467.ssm
controller
mapper
pojo
service
impl
UserService
2.2在resource文件下创建文件夹,创建映射配置文件
com/os467/ssm/mapper/UserMapper.xml
2.3在resource文件夹下创建数据源文件db.properties,加入数据源信息,见上文
3、在resource文件夹下创建spring核心配置文件 beans.xml,配置核心配置文件
3.1、引入核心配置文件所对应的xml约束
xxxxxxxxxx
91
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context.xsd">
3.2、加入开启扫描包标签,指定需要被扫描的包路径
xxxxxxxxxx
21<!--开启扫描包-->
2<context:component-scan base-package="com.os467"></context:component-scan>
3.3、读取数据源配置文件中的数据,获取数据源信息,能够在本配置文件中用el表达式取到值
xxxxxxxxxx
21<!--读取配置文件中的数据,获取数据源信息-->
2<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
配置数据源信息,我们使用dbcp连接池提供的BasicDataSource类来创建数据源实例,通过池的思想来创建持久层实例
xxxxxxxxxx
101<!--配置数据源-->
2<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
3
4 <!--注入源数据-->
5 <property name="driverClassName" value="${db.driver}"></property>
6 <property name="url" value="${db.url}"></property>
7 <property name="username" value="${db.username}"></property>
8 <property name="password" value="${db.password}"></property>
9
10</bean>
3.4、配置mybatis所需要的会话工厂,工厂实例装载进spring容器中,选用mybatis.spring包下的SqlSessionFactoryBean类创建会话工厂
需要提供之前配置的数据源实例
然后需要提供映射文件的配置路径
xxxxxxxxxx
91<!--配置mybatis需要的会话工厂,使用mybatis整合spring的会话工厂-->
2<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
3
4 <!--注入数据源-->
5 <property name="dataSource" ref="basicDataSource"></property>
6 <!--匹配mapper配置文件-->
7 <property name="mapperLocations" value="classpath:com/os467/shop/controller/*.xml"></property>
8
9</bean>
3.5、mybatis扫描器的配置,引入MapperScannerConfigurer类
添加属性,配置需要被代理的持久层接口的包路径
xxxxxxxxxx
71<!--mybatis扫描器的配置-->
2<bean id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
3
4 <!--扫描此包下所有持久层接口,并且通过代理模式创建出每个接口对应的代理对象,并交由spring管理-->
5 <property name="basePackage" value="com.os467.shop.mapper"></property>
6
7</bean>
4、在resource文件夹下创建mvc框架配置文件 springmvc.xml
4.1、引入xml约束
xxxxxxxxxx
121
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:mvc="http://www.springframework.org/schema/mvc"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xsi:schemaLocation="
7 http://www.springframework.org/schema/beans
8 http://www.springframework.org/schema/beans/spring-beans.xsd
9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context.xsd
11 http://www.springframework.org/schema/mvc
12 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
4.2、开启扫描包
xxxxxxxxxx
21<!--扫描包-->
2<context:component-scan base-package="com.os467.shop"></context:component-scan>
4.3、添加标签开启springmvc对注解的支持,过滤前端传来的静态资源
xxxxxxxxxx
51<!--开启mvc对注解的支持-->
2<mvc:annotation-driven></mvc:annotation-driven>
3
4<!--过滤静态资源-->
5<mvc:default-servlet-handler></mvc:default-servlet-handler>
4.4、配置视图解析器,引入类InternalResourceViewResolver
配置视图前缀,jsp文件路径,配置视图后缀,文件格式
xxxxxxxxxx
101<!--配置视图解析器-->
2<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
3
4 <!--设置前缀-->
5 <property name="prefix" value="/WEB-INF/jsp/"></property>
6
7 <!--设置后缀-->
8 <property name="suffix" value=".jsp"></property>
9
10</bean>
5、配置web.xml文件
5.1、配置前端总控制器,引入类DispatcherServlet,绑定springMVC核心配置文件,配置初始化属性数据,设置启动级别
拦截前端请求资源
xxxxxxxxxx
211<!--配置前端总控制器-->
2<servlet>
3 <servlet-name>dispatcherServlet</servlet-name>
4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
5
6 <!--绑定springMVC核心配置文件-->
7 <init-param>
8 <param-name>contextConfigLocation</param-name>
9 <param-value>classpath:springmvc.xml</param-value>
10 </init-param>
11
12 <!--设置启动级别-->
13 <load-on-startup>1</load-on-startup>
14
15</servlet>
16
17<!--拦截前端请求-->
18<servlet-mapping>
19 <servlet-name>dispatcherServlet</servlet-name>
20 <url-pattern>/</url-pattern>
21</servlet-mapping>
5.2、设置全局属性,绑定spring核心配置文件,同时配置一个监听器ContextLoaderListener,让项目在启动的时候就去加载核心容器
xxxxxxxxxx
121<!--设置全局属性,绑定spring核心配置文件-->
2<context-param>
3
4 <param-name>contextConfigLocation</param-name>
5 <param-value>classpath:beans.xml</param-value>
6
7</context-param>
8
9 <!--配置一个监听器,让项目在启动的时候就去加载spring核心容器-->
10<listener>
11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
12</listener>
5.3、配置一个编码过滤器,将所有资源的字符集改成utf-8,同时拦所有资源,对静态资源也生效
xxxxxxxxxx
191<!--配置一个编码过滤器,拦截所有的资源,将所有资源的字符集设置成utf-8-->
2<filter>
3
4 <filter-name>characterEncodingFilter</filter-name>
5 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
6
7 <!--将字符集设置成utf-8-->
8 <init-param>
9 <param-name>encoding</param-name>
10 <param-value>utf-8</param-value>
11 </init-param>
12
13</filter>
14
15<!--加*代表也拦截静态资源-->
16<filter-mapping>
17 <filter-name>characterEncodingFilter</filter-name>
18 <url-pattern>/*</url-pattern>
19</filter-mapping>
6、配置UserMapper.xml文件
引入mybatis相关xml约束,注册命名空间,编写测试sql语句
xxxxxxxxxx
141
2
3
4
5
6<mapper namespace="com.os467.ssm.mapper.UserMapper">
7
8 <select id="findUserList" resultType="com.os467.ssm.pojo.User" >
9
10 select * from tb_user
11
12 </select>
13
14</mapper>
7、完善三层架构配置
持久层UserMapper
xxxxxxxxxx
131package com.os467.ssm.mapper;
2
3import com.os467.ssm.pojo.User;
4import org.springframework.stereotype.Repository;
5
6import java.util.List;
7
8
9public interface UserMapper {
10
11 List<User> findUserList();
12
13}
业务层实现类UserServiceImpl
xxxxxxxxxx
211package com.os467.ssm.service.impl;
2
3import com.os467.ssm.mapper.UserMapper;
4import com.os467.ssm.pojo.User;
5import com.os467.ssm.service.UserService;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.stereotype.Service;
8
9import javax.annotation.Resource;
10import java.util.List;
11
12
13public class UserServiceImpl implements UserService {
14
15
16 private UserMapper userMapper;
17
18 public List<User> findUserList() {
19 return userMapper.findUserList();
20 }
21}
表现层UserController
xxxxxxxxxx
291package com.os467.ssm.controller;
2
3import com.os467.ssm.pojo.User;
4import com.os467.ssm.service.UserService;
5import org.springframework.stereotype.Controller;
6import org.springframework.ui.Model;
7import org.springframework.web.bind.annotation.RequestMapping;
8
9import javax.annotation.Resource;
10import java.util.List;
11
12
13"user") (
14public class UserController {
15
16 name = "userServiceImpl") (
17 private UserService userService;
18
19 "list") (
20 public String findUserList(Model model){
21
22 List<User> userList = userService.findUserList();
23
24 model.addAttribute("userList",userList);
25
26 return "test";
27 }
28
29}
select * from ssm_goods limit 0,3
分页需要的参数:
如果需要给持久层传递多个不同数据,我们选择将数据先封装到集合中
xxxxxxxxxx
951/**
2 * 实现商品列表
3 * currentPage 当前页的页码值
4 */
5"/list") (
6public String getGoodsList(Model model,String currentPage){
7
8 //接收当前页的页码值
9 if (currentPage == null || currentPage.equals("")){
10
11 currentPage = "1";
12
13 }else {
14
15 System.out.println("当前页的页码值为 "+currentPage);
16
17 }
18
19 //计算上一页的页码值
20 Integer prevPage = 0;
21
22 if (currentPage.equals("1")){
23
24 prevPage = 1;
25
26 }else {
27
28 prevPage = Integer.parseInt(currentPage) - 1;
29
30 }
31
32 //计算尾页的值
33 Integer lastPage = 0;
34
35 //获取商品的总记录条数
36 Integer count = goodsService.getCount();
37
38 System.out.println("总记录条数为 "+count);
39
40 //指定分页单位
41 Integer pageSize = 2;
42
43 if (count % pageSize == 0){
44
45 lastPage = count / pageSize;
46
47 }else {
48
49 lastPage = (count / pageSize) + 1;
50
51 }
52
53 //计算下一页的值
54 Integer nexPage = 0;
55
56 if (currentPage.equals(lastPage+"")){
57
58 nexPage = lastPage;
59
60 }else {
61
62 nexPage = Integer.parseInt(currentPage) + 1;
63
64 }
65
66 //起始索引位置
67 Integer startIndex = (Integer.parseInt(currentPage) - 1) * pageSize;
68
69 //创建一个map集合,用于存放数据
70 HashMap<Object, Object> hashMap = new HashMap();
71
72 //将分页所需要的条件传到集合里
73 hashMap.put("startIndex",startIndex);
74 hashMap.put("pageSize",pageSize);
75
76 //查询商品数据
77 List<Goods> goodsList = goodsService.getGoodsList(hashMap);
78
79 //查询品牌数据
80 List<Brand> brandList = goodsService.getBrandList();
81
82 //查询类别的数据
83 List<Cate> cateList = goodsService.getCateList();
84
85 //将数据存到model中
86 model.addAttribute("goodsList",goodsList);
87 model.addAttribute("brandList",brandList);
88 model.addAttribute("cateList",cateList);
89 model.addAttribute("nexPage",nexPage);
90 model.addAttribute("lastPage",lastPage);
91 model.addAttribute("prevPage",prevPage);
92
93 return "goodsList";
94
95}
GoodsMapper
xxxxxxxxxx
61<!--查询所有的商品信息-->
2<select id="getGoodsList" parameterType="hashMap" resultMap="goodsVo">
3
4 select * from ssm_goods limit #{startIndex},#{pageSize}
5
6</select>
前端
xxxxxxxxxx
171<script>
2 //分页函数
3 function page(currentPage){
4
5 location.href = "<%=path%>/goods/list?currentPage="+currentPage;
6
7 }
8</script>
9
10<tr>
11 <td colspan="12" align="center">
12 <a onclick="page(1)">首页</a>
13 <a onclick="page(${prevPage})">上一页</a>
14 <a onclick="page(${nexPage})">下一页</a>
15 <a onclick="page(${lastPage})">尾页</a>
16 </td>
17</tr>
initPage()
初始化分页工具类的时候需要从前端获取字符串类型的当前页码,需要提供分页单位,需要提供数据总记录条数
我们通过将分页工具类放进model域中来从前端获取分页数据
xxxxxxxxxx
1391package com.os467.shop.utils;
2
3import org.springframework.stereotype.Component;
4
5
6public class PageUtils {
7
8 //当前页
9 private String currentPage;
10
11 //上一页
12 private Integer prevPage;
13
14 //下一页
15 private Integer nextPage;
16
17 //尾页(也可以表示总页数)
18 private Integer lastPage;
19
20 //总记录条数
21 private Integer count;
22
23 //起始索引位置
24 private Integer startIndex;
25
26 //分页单位
27 private Integer pageSize;
28
29 public String getCurrentPage() {
30 return currentPage;
31 }
32
33 public void setCurrentPage(String currentPage) {
34 this.currentPage = currentPage;
35 }
36
37 public Integer getPrevPage() {
38 return prevPage;
39 }
40
41 public void setPrevPage(Integer prevPage) {
42 this.prevPage = prevPage;
43 }
44
45 public Integer getNextPage() {
46 return nextPage;
47 }
48
49 public void setNextPage(Integer nextPage) {
50 this.nextPage = nextPage;
51 }
52
53 public Integer getLastPage() {
54 return lastPage;
55 }
56
57 public void setLastPage(Integer lastPage) {
58 this.lastPage = lastPage;
59 }
60
61 public Integer getCount() {
62 return count;
63 }
64
65 public void setCount(Integer count) {
66 this.count = count;
67 }
68
69 public Integer getStartIndex() {
70 return startIndex;
71 }
72
73 public void setStartIndex(Integer startIndex) {
74 this.startIndex = startIndex;
75 }
76
77 public Integer getPageSize() {
78 return pageSize;
79 }
80
81 public void setPageSize(Integer pageSize) {
82 this.pageSize = pageSize;
83 }
84
85 //计算上一页的值
86 private void initPrevPage(){
87
88 prevPage = currentPage.equals("1")?1:Integer.parseInt(currentPage)-1;
89
90 }
91
92 //计算尾页的值
93 private void initLastPage(){
94
95 lastPage = count % pageSize == 0 ? count / pageSize : (count / pageSize)+1;
96
97 }
98
99 //计算下一页的值
100 private void initNextPage(){
101
102 nextPage = currentPage.equals(lastPage+"") ? lastPage : Integer.parseInt(currentPage) + 1;
103
104 }
105
106 //计算起始索引位置
107 private void initStartIndex(){
108
109 startIndex = (Integer.parseInt(currentPage) - 1) * pageSize;
110
111 }
112
113 //初始化的方法
114 private void init(String currentPage,Integer pageSize,Integer count){
115
116 if (currentPage == null || currentPage.equals("")){
117
118 currentPage = "1";
119
120 }
121
122 this.currentPage = currentPage;
123 this.pageSize = pageSize;
124 this.count = count;
125
126 }
127
128 //调用的方法
129 public void initPage(String currentPage,Integer pageSize,Integer count){
130
131 init(currentPage,pageSize,count);
132 initStartIndex();
133 initPrevPage();
134 initLastPage();
135 initNextPage();
136
137 }
138
139}