这几天写了个ssh2分页实现,放到博客里留个记号,先贴代码有空再写说明了
spring配置文件
jdbc.properties文件
junit测试
struts.properties配置文件
web.xml 文件
spring配置文件
<?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:jee="http://www.springframework.org/schema/jee" 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-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="false">
<context:annotation-config/>
<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
<context:component-scan base-package="com.xangqun" />
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.user}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.xangqun.entity.Person</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.query.substitutions=true 1, false 0
hibernate.jdbc.batch_size=20
hibernate.hbm2ddl.auto=update
</value>
</property>
</bean>
<!-- 事务配置 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
jdbc.properties文件
db.driver=org.gjt.mm.mysql.Driver
db.url=jdbc:mysql://127.0.0.1:3306/ssh2
db.user=root
db.password=123456
package com.xangqun.base;
import java.util.concurrent.locks.Condition;
import com.xangqun.entity.Person;
import com.xangqun.page.OrderBy;
import com.xangqun.page.Pagination;
public interface BaseManage extends BaseDao<Person> {
public Pagination find(Person eg, boolean anyWhere,
Condition[] conds, int pageNo, int pageSize, String... exclude);
public Pagination findAll(int pageNo, int pageSize, OrderBy... orders);
}
package com.xangqun.base;
import org.springframework.stereotype.Repository;
import com.xangqun.entity.Person;
@Repository("baseManagerImpl")
public class BaseManagerImpl extends BaseDaoImpl<Person> implements BaseManage {
}
package com.xangqun.service;
import java.util.concurrent.locks.Condition;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xangqun.base.BaseManage;
import com.xangqun.entity.Person;
import com.xangqun.page.OrderBy;
import com.xangqun.page.Pagination;
@Service("personService")
public class PersonService implements BaseManage {
@Resource(name="baseManagerImpl")
private BaseManage baseManagerImpl;
@Override
public Pagination find(Person eg, boolean anyWhere, Condition[] conds,
int pageNo, int pageSize, String... exclude){
return baseManagerImpl.find(eg, anyWhere, conds, pageNo, pageSize, exclude);
}
@Override
public Pagination findAll(int pageNo, int pageSize, OrderBy... orders) {
return baseManagerImpl.findAll(pageNo, pageSize, orders);
}
public BaseManage getBaseManagerImpl() {
return baseManagerImpl;
}
public void setBaseManagerImpl(BaseManage baseManagerImpl) {
this.baseManagerImpl = baseManagerImpl;
}
}
junit测试
package com.junittest;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import com.xangqun.entity.Person;
import com.xangqun.page.Pagination;
import com.xangqun.service.PersonService;
@ContextConfiguration("classpath:applicationContext.xml")
public class DemoTest extends AbstractJUnit4SpringContextTests{
public Session session;
@Resource(name="personService")
private PersonService ps;
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
public PersonService getPs() {
return ps;
}
public void setPs(PersonService ps) {
this.ps = ps;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
return sessionFactory.openSession();
}
@Test //测试添加用户
public void testSave(){
for(int i=21;i<30;i++){
Person person=new Person();
person.setName("xangqun"+i);
person.setSex("sex"+i);
getSession().save(person);
}
}
@Test
public void testList(){
Query query = getSession().createQuery("from t_person t");
List<Person> persons = query.list();
for(Iterator<Person> it = persons.iterator() ; it.hasNext() ; ){
Person person = it.next();
System.out.println("username = " + person.getName());
}
}
@Test
public void testListTwo() throws Exception{
Person person=new Person();
Pagination p=ps.find(person, true, null, 1, 10, "");
for(Object ps:p.getList()){
System.out.println(((Person)ps).getName());
}
}
}
struts.properties配置文件
struts.action.extension=do,htm,jspa,jspx,php,asp,aspx
struts.objectFactory=spring
struts.enable.DynamicMethodInvocation=false
struts.devMode=true
struts.locale=zh_CN
struts.i18n.encoding=GBK
struts.ui.theme=simple
struts.enable.SlashesInActionNames=true
package com.xangqun.action;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.xangqun.base.BaseManage;
import com.xangqun.entity.Person;
import com.xangqun.page.Pagination;
@SuppressWarnings("serial")
@Controller(value="/personAction")
@Action(value="/personAction",results={@Result(name=PersonAction.SUCCESS,location="/index.jsp")})
public class PersonAction extends ActionSupport {
private Pagination pagination;
private int pageNo = 1;
@Resource(name="personService")
private BaseManage personService;
private Person person;
@Override
public String execute(){
Object strPageSize=ServletActionContext.getRequest().getAttribute("pagesize");
int pagesize = (strPageSize==null)?10:(Integer)strPageSize;
String strpageNo=ServletActionContext.getRequest().getParameter("page");
int pageNo = (strpageNo==null)?1:Integer.valueOf(strpageNo);
pagination=personService.findAll(pageNo, pagesize, null);
return SUCCESS;
}
public Pagination getPagination() {
return pagination;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public BaseManage getPersonService() {
return personService;
}
public void setPersonService(BaseManage personService) {
this.personService = personService;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SSH2Page</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.xangqun.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>