最近利用业余时间看了两个ssh架构的项目教程,有所获,记录一下
----------------------------------------环境搭建----------------------------------------
【1】导入jar包
如果是web工程,导入ssh以及相关数据库的所需jar包,具体可以去官网下载;
如果是maven工程,则需要相关的maven配置;
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.winssage.fengshu</groupId>
<artifactId>winssage</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>winssage</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- 添加SSH依赖 -->
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.1</version>
</dependency>
<!-- 添加Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.5.Final</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- 添加Log4J依赖 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.6.4</version>
</dependency>
<!-- 添加javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.11.0.GA</version>
</dependency>
<!-- 添加Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<!-- 导入struts2整合spring插件 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-Java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- servlet、jsp -->
<!-- junit -->
</dependencies>
</project>
【2】数据库表
这里以用户、角色、部门为例(每个用户有不同的角色,每个角色都归属相关的部门)
【3】目录规范
方式一:com.公司名称.项目名称.子模块.分层
方式二:com.公司名称.项目名称.分层.子模块
com.itheima.crm.staff.dao.impl 员工dao层和实现类
com.itheima.crm.staff.service.impl 员工service层和实现类
com.itheima.crm.staff.web.action 员工web
com.itheima.crm.staff.domain 员工javabean
【4】编写po类和hbm文件(可以用hibernate插件生成,生成方法请查看http://www.jianshu.com/p/6c0afa1ed210)
CrmStaff.java
/**
* @author:lvfang
* @date:2017-7-8
* @desc:员工bean
*/
public class CrmStaff {
/*
* CREATE TABLE `crm_staff` (
`staffId` varchar(255) NOT NULL PRIMARY KEY,
`loginName` varchar(100) DEFAULT NULL,
`loginPwd` varchar(100) DEFAULT NULL,
`staffName` varchar(100) DEFAULT NULL,
`gender` varchar(20) DEFAULT NULL,
`onDutyDate` datetime DEFAULT NULL,
`postId` varchar(255) DEFAULT NULL,
CONSTRAINT FOREIGN KEY (`postId`) REFERENCES `crm_post` (`postId`)
); */
private String staffId;
private String loginName; //登录名
private String loginPwd; //登录密码
private String staffName; //员工姓名
private String gender; //性别
private Date onDutyDate; //入职时间
//多对一:多个员工 属于 【一个职务】
private CrmPost post; (注意,这里员工表中对应postId,但对应到java中代表post对象)
//getter setter方法
}
CrmStaff.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.itheima.crm.staff.domain.CrmStaff" table="crm_staff">
<id name="staffId">
<generator class="uuid"></generator>
</id>
<property name="loginName"></property>
<property name="loginPwd"></property>
<property name="staffName"></property>
<property name="gender"></property>
<property name="onDutyDate">
<column name="onDutyDate" sql-type="datetime"></column>
</property>
<!-- 多对一:多个员工 属于 【一个职务】 -->
<many-to-one name="post" class="com.itheima.crm.post.domain.CrmPost" column="postId"></many-to-one>
</class>
</hibernate-mapping>
CrmPost.java
/**
* @author:lvfang
* @date:2017-7-8
* @desc:职务bean
*/
public class CrmPost {
/*
* CREATE TABLE `crm_post` (
`postId` varchar(255) NOT NULL PRIMARY KEY,
`postName` varchar(100) DEFAULT NULL,
`depId` varchar(255) DEFAULT NULL,
CONSTRAINT FOREIGN KEY (`depId`) REFERENCES `crm_department` (`depId`)
); */
private String postId;
private String postName; //职务名称
//多对一:多个职务 属性 【一个部门】
private CrmDepartment department;
//一对多: 一个职务 可以有 【多个员工】
private Set<CrmStaff> staffSet = new HashSet<CrmStaff>();
//getter setter方法
}
CrmPost.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.itheima.crm.post.domain.CrmPost" table="crm_post">
<id name="postId">
<generator class="uuid"></generator>
</id>
<property name="postName"></property>
<!-- 多对一:多个职务 属性 【一个部门】 -->
<many-to-one name="department" class="com.itheima.crm.department.domain.CrmDepartment" column="depId"></many-to-one>
<!-- 一对多: 一个职务 可以有 【多个员工】 -->
<set name="staffSet">
<key column="postId"></key>
<one-to-many class="com.itheima.crm.staff.domain.CrmStaff"/>
</set>
</class>
</hibernate-mapping>
CrmDepartment.java
/**
* @author:lvfang
* @date:2017-7-8
* @desc:部门bean
*/
public class CrmDepartment {
/*
* CREATE TABLE `crm_department` ( `depId` varchar(255) NOT NULL PRIMARY
* KEY, `depName` varchar(50) DEFAULT NULL );
*/
private String depId;
private String depName; //部门名称
//一对多: 一个部门 对应 【多个职务】 ,选择集合 Set --> 不重复、没有顺序
private Set<CrmPost> postSet = new HashSet<CrmPost>();
//getter setter方法
}
CrmDepartment.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.itheima.crm.department.domain.CrmDepartment" table="crm_department">
<id name="depId">
<generator class="uuid"></generator>
</id>
<property name="depName"></property>
<!-- 一对多: 一个部门 对应 【多个职务】 -->
<set name="postSet">
<key column="depId"></key>
<one-to-many class="com.itheima.crm.post.domain.CrmPost"/>
</set>
</class>
</hibernate-mapping>
这里po和hbm文件要注意的是
(1)po类的类型要与数据库字段对应,主外键列为某表主键时,对应到javabean是该对象,而非对象ID
(2)hbm文件配置是注意包配置和关系映射
(3)hibernate版本不同,所对应的hbm头也不一样,一定要在对应的版本jar下寻找案例
(4)hbm文件一般与javabean放在一个包下
(5)如果还要加一点,那就是要细心
【5】spring配置
一般ssh项目配置文件比较多,所以大都单建一个包resource,在resource目录下在分包,加载路径对应好即可
resource/jdbcInfo.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3307/crmdb
jdbc.user=root
jdbc.password=root
resource/applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 公共配置项 -->
<!-- 1.1 加载properties文件 -->
<context:property-placeholder location="classpath:jdbcInfo.properties"/>
<!-- 1.2 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2. 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/itheima/crm/*/domain/*.hbm.xml"></property>
</bean>
<!-- 3.事务管理 -->
<!-- 3.1 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 3.2 事务详情 * 增删改:读写;查询:只读 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="login" read-only="true"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 3.3 aop编程
* 强制cglib : proxy-target-class="true"
-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.crm.*.service..*.*(..))"/>
</aop:config>
<!-- 4 导入其他配置文件 -->
<!-- 4.1 员工 (主要配置service和dao的注入以及dao中sessionFactory的注入)-->
<import resource="applicationContext-staff.xml"/>
</beans>
【6】struts配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 1 常量 -->
<!-- 1.1开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 1.2标签主题,简单风格 -->
<constant name="struts.ui.theme" value="simple"></constant>
<!-- #2 公共项 -->
<package name="common" namespace="/" extends="struts-default">
<action name="UIAction_*_*">
<result>/WEB-INF/pages/{1}/{2}.jsp</result>
</action>
</package>
<!-- 3 加载其他配置文件 -->
<include file="struts/struts-staff.xml" ></include>
<!-- 这里要注意的是公共的struts配置继承"struts-default",其他的子struts文件继承common-->
</struts>
【7】web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- spring 过滤器,延迟session关闭 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 1.1 spring配置文件位置 *
方式1: 【建议】 <param-value>classpath:spring/applicationContext.xml</param-value>
需要在applicationContext.xml 配置<import> 导入其他 *
方式2: <param-value>classpath:spring/applicationContext*.xml</param-value>
加载所有,不需要配置<import> -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- 1.2 加载spring配置文件使用监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2 struts 前端控制器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
【8】开发思路
【9】daoImpl,继承HibernateDaoSupport 实现Dao
public class StaffDaoImpl extends HibernateDaoSupport implements StaffDao {
@Override
public CrmStaff find(String loginName, String loginPwd) {
List<CrmStaff> allStaff = this.getHibernateTemplate().find("from CrmStaff where loginName=? and loginPwd = ?", loginName,loginPwd);
if(allStaff.size() == 1){
return allStaff.get(0);
}
return null;
}
}
【10】serviceImpl 实现对应的service接口,setter注入Dao接口
public class StaffServiceImpl implements StaffService {
private StaffDao staffDao;
public void setStaffDao(StaffDao staffDao) {
this.staffDao = staffDao;
}
@Override
public CrmStaff login(CrmStaff staff) {
return staffDao.find(staff.getLoginName(), staff.getLoginPwd());
}
}
【11】添加对应的子spring配置文件,给service注入dao,给dao注入sessionFactory,注意这里的sessionFactory要与全局spring中的sessionFactory名一致
applicationContext-staff.xml
<!-- 员工配置项:dao、service -->
<bean id="staffDao" class="com.itheima.crm.staff.dao.impl.StaffDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="staffService" class="com.itheima.crm.staff.service.impl.StaffServiceImpl">
<property name="staffDao" ref="staffDao"></property>
</bean>
【12】Action要继承ActionSupport 实现ModelDriven<CrmStaff>对应实体类,注意要set数据类
public class StaffAction extends ActionSupport implements ModelDriven<CrmStaff> {
//封装数据
private CrmStaff staff = new CrmStaff();
@Override
public CrmStaff getModel() {
return staff ;
}
//默认按照名称注入
private StaffService staffService;
public void setStaffService(StaffService staffService) {
this.staffService = staffService;
}
public String login(){
//1 查询员工
CrmStaff findStaff = staffService.login(staff);
//业务处理
return "login";
}
}
【13】struts子配置文件,注意要把子配置文件引入全局struts
struts-staff.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 员工配置 -->
<package name="sta" namespace="/" extends="common">
<action name="staffAction_*" class="com.itheima.crm.staff.web.action.StaffAction" method="{1}">
<!-- 1 登录成功 -->
<result name="success" type="redirectAction">staffAction_home</result>
<!-- 2 没有登录成,需要登录 【提出到全局struts配置】
<result name="login">/WEB-INF/pages/login.jsp</result>
-->
<!-- 3 首页 -->
<result name="home">/WEB-INF/pages/frame.jsp</result>
</action>
</package>
</struts>
【12】jsp文件
所有的jsp页面存在WEB-INF目录下。 (不能通过浏览器直接访问)
WEB-INF/page/模块/*.jsp
【13】base开发
由于每个模块之间都包括基础的怎删改查方法,以及action都要去注入对应的javaBean等等操作,这样过于繁琐
【14】baseDao
public interface BaseDao<T> {
public void save(T t);
public void update(T t);
public void delete(T t);
public void saveOrUpdate(T t);
/**
* 通过id查询
* @param id
* @return
*/
public T findById(Serializable id);
/**
* 查询所有
* @return
*/
public List<T> findAll();
/**
* 条件查询
* @param condition ,格式:" and ..? and ..."
* @param params
* @return
*/
public List<T> findAll(String condition, Object[] params);
/**
* 分页,查询总记录数
* @param condition
* @param params
* @return
*/
public int getTotalRecord(String condition, Object[] params);
/**
* 分页,查询结果
* @param condition 条件
* @param params 条件实际参数
* @param startIndex 开始索引
* @param pageSize 每页显示个数
* @return
*/
public List<T> findAll(String condition, Object[] params, int startIndex, int pageSize);
/**
* 离线条件查询,使用QBC
* @param criteria
* @return
*/
public List<T> findAll(DetachedCriteria criteria);
/**
* 分页
* @param criteria
* @param startIndex
* @param pageSize
* @return
*/
public List<T> findAll(DetachedCriteria criteria , int startIndex ,int pageSize);
}
【15】BaseDaoImpl
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T>{
//T 编译时,注释变量,运行时,才可以获取到具体的类型
private Class<?> beanClass;
public BaseDaoImpl(){
//获得运行时的类型 , BaseDaoImpl<CrmStaff>被参数化的类型
ParameterizedType paramType = (ParameterizedType) this.getClass().getGenericSuperclass();
//<a,b,c..> 泛型里可以规定多个,所以getActualTypeArguments获取的是数组,我们这里泛型只有一个T所以取[0]
beanClass = (Class<?>) paramType.getActualTypeArguments()[0];
}
public void save(T t) {
this.getHibernateTemplate().save(t);
}
public void update(T t) {
this.getHibernateTemplate().update(t);
}
public void delete(T t) {
this.getHibernateTemplate().delete(t);
}
public void saveOrUpdate(T t) {
this.getHibernateTemplate().saveOrUpdate(t);
}
public T findById(Serializable id) {
return (T) this.getHibernateTemplate().get(beanClass, id);
}
public List<T> findAll() {
return this.getHibernateTemplate().find("from " + beanClass.getName());
// hql
// session.createQuery("from CrmStaff"); //简写,存在自动导包 , <hibernate-mapping auto-import="true">
// session.createQuery("from com.itheima.crm.staff.domain.CrmStaff"); //完整写法
}
public List<T> findAll(String condition, Object[] params) {
String hql = "from "+beanClass.getName()+" where 1=1 " + condition;
return this.getHibernateTemplate().find(hql , params);
}
public int getTotalRecord(String condition, Object[] params) {
String hql = "select count(c) from "+beanClass.getName()+" c where 1=1 " + condition;
List<Long> list = this.getHibernateTemplate().find(hql,params);
return list.get(0).intValue();
}
public List<T> findAll(String condition, Object[] params, int startIndex, int pageSize) {
String hql = "from "+beanClass.getName()+" where 1=1 " + condition;
return this.getHibernateTemplate().execute(new PageHibernateCallback<T>(hql, params, startIndex, pageSize));
}
public List<T> findAll(DetachedCriteria criteria) {
return this.getHibernateTemplate().findByCriteria(criteria);
}
public List<T> findAll(DetachedCriteria criteria, int startIndex, int pageSize) {
return this.getHibernateTemplate().findByCriteria(criteria, startIndex, pageSize);
}
}
【16】baseAction
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
private static final long serialVersionUID = 5028961270688544619L;
// 封装数据Bean
private T t;
public T getModel() {
return t;
}
// 1.1 实例化t
public BaseAction() {
try {
// 1 获得T运行时Class
ParameterizedType paramType = (ParameterizedType) this.getClass().getGenericSuperclass();
Class<T> clazz = (Class<T>) paramType.getActualTypeArguments()[0];
// 2 反射创建实例
t = clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 2 spring注入service,多个service直接注入baseAction中,具体的action就不用去注入,并直接调用
// 员工
private StaffService staffService;
public void setStaffService(StaffService staffService) {
this.staffService = staffService;
}
public StaffService getStaffService() {
return staffService;
}
// 分页数据
private int pageNum = 1;// 默认是从前台传入
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageNum() {
return pageNum;
}
private int pageSize = 10; // 默认是从前台传入
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageSize() {
return pageSize;
}
//4 简化值栈操作
/*
* 方式一:context(map)存放 put(key,val) ,jsp页面获取“#key”
* ActionContext.getContext().put(key, value);
* 方式二:root(值栈),push(obj),一般数据为javaBean 或 map , jsp页面获取“属性名”或"key"
* ActionContext.getContext().getValueStack().push(obj);
* 方式三:root(值栈),set(key,value),一般数据为list,jsp页面获取“key”
* ActionContext.getContext().getValueStack().set(key, value)
* set() 底层是new Map(key,value),将其在push(map);
* */
//方式一 put(list)
public void put(String key, Object value){
ActionContext.getContext().put(key, value);
}
//方式二 push(object)
public void push(Object o){
ActionContext.getContext().getValueStack().push(o);
}
//方式三
public void set(String key ,Object o){
ActionContext.getContext().getValueStack().set(key, o);
}
public void putSession(String key, Object value){
ActionContext.getContext().getSession().put(key, value);
}
public void putApplication(String key, Object value){
ActionContext.getContext().getApplication().put(key, value);
}
}
【17】分页容器类
/**
*
*/
package com.itheima.crm.utils;
import java.util.List;
/**
* @author:lvfang
* @date:2017-7-13
* @desc:
* @version:V1.0
*/
public class PageBean<T> {
// 必选项
private int pageNum; // 第几页(当前页)
private int pageSize; // 每页显示个数(固定值)
private int totalRecord; // 总记录数(查询数据库)--注意条件
// 计算项
private int startIndex; // 开始索引(计算)
private int totalPage; // 总分页数(计算)
// 数据
private List<T> data; // 分页数据
// 动态显示条
private int start;
private int end;
public PageBean(int pageNum, int pageSize, int totalRecord) {
super();
this.pageNum = pageNum;
this.pageSize = pageSize;
this.totalRecord = totalRecord;
// 计算
// 1 开始索引
this.startIndex = (this.pageNum - 1) * this.pageSize;
// 2 总分页数
this.totalPage = (this.totalRecord + this.pageSize - 1) / this.pageSize;
// 3 动态显示条
// 3.1 初始化数据 -- 显示10个分页
this.start = 1;
this.end = 10;
// 3.2 初始数据 , totalPage = 4
if (this.totalPage <= 10) {
this.end = this.totalPage;
} else {
// totalPage = 22
// 3.3 当前页 前4后5
this.start = this.pageNum - 4;
this.end = this.pageNum + 5;
// * pageNum = 1
if (this.start < 1) {
this.start = 1;
this.end = 10;
}
// * pageNum = 22
if (this.end > this.totalPage) {
this.end = this.totalPage;
this.start = this.totalPage - 9;
}
}
}
//getter setter方法
}