`
xangqun
  • 浏览: 79814 次
  • 性别: Icon_minigender_1
  • 来自: 江西
社区版块
存档分类
最新评论

spring+hibernate+struts2 annotation实现分页(2)

阅读更多
这几天写了个ssh2分页实现,放到博客里留个记号,先贴代码有空再写说明了

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>
分享到:
评论

相关推荐

    Spring2.5+Struts2.0+hibernate3.0+Dwr+jquery+displayTag

    2 利用struts2 的LoginAction-validation.xml 3 在bean里把service包;暴露DWR,写了一个验证用户名的流程 4 采用jpa作为POJO,还是减少配置 5 加入display的分页,并且是物理分页 打开后自已建表sql.txt jdbc....

    基于spring,struts(struts2),hibernate的web项目脚手架

    内置一个基于数据库的代码生成器rapid-generator,极易进行二次开发 struts1,struts2的零配置 spring集成及加强,自动搜索hibernate的entity annotation class 集成动态构造sql的工具:rapid-xsqlbuilder 集成...

    使用Annotation并对DAO层封装具有分页功能的S2SH整合实例_好资源0分送

    个人认为由Sun官方支持的EJB规范会越来越流行,此时如果使用基于Annotation的SSH框架很容易转移到Struts+EJB+Spring的项目中,而且使用Annotation,很容易实现0配置,像在这个实例中就一个配置,这样避免了配置文件...

    低清版 大型门户网站是这样炼成的.pdf

    10.4.2 控制层struts 2实现 662 10.4.3 持久层hibernate3实现 672 10.4.4 spring 2接管业务逻辑层 675 10.5 小结 677 第11章 温故知新—打造购物车与订单管理系统 679 11.1 购物车与订单管理系统需求分析 679 ...

    免费下载:自己整理的java学习资料

    自己整理的一些资料,不需要积分,希望对大家有帮助。 里面包有如下的一些java资料 Ant使用指南.pdf cvs.pdf Eclipse入门-- Eclipse的使用简介及插件开发.PDF ...spring2中文开发参考手册.chm a.txt

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

     国内知名的高端IT技术作家,已出版《Spring 2.0宝典》、《基于J2EE的Ajax宝典》、《轻量级J2EE企业应用实战》、《Struts 2权威指南》、《Ruby On Rails敏捷开发最佳实践》等著作。 目录: 第0章 学习Java...

    java开源包2

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包3

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包4

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包1

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包11

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包6

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包5

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包10

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包8

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包7

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包9

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包101

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    Java资源包01

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    JAVA上百实例源码以及开源项目

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

Global site tag (gtag.js) - Google Analytics