struts1.3+spring2.5+hibernate3.3 组合开发 annotation实现

在本例中用struts1.3+spring2.5+hibernate3.3的annotaion来实现开发。


1、新建一个javaWeb项目。名字为 ssh。
2、在新建的项目中加入spring、struts、hibernate需要的jar包
spring2.5
struts1.3+spring2.5+hibernate3.3 组合开发 annotation实现

struts1.3
struts1.3+spring2.5+hibernate3.3 组合开发 annotation实现


hibernate3.3

struts1.3+spring2.5+hibernate3.3 组合开发 annotation实现
3、在str目录里加下beans.xml配置文件,并对数据库连接池与事务进行配置(事务用注解进行配置).在beans里要加上事务的xml声明。也就是表示事务的头文件。

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- spring事务 tx-->
	<!--自动扫描 -->
	<context:component-scan base-package="cn.ehoo" />
	

	<context:property-placeholder location="classpath:jdbc.properties" />
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${driverClassName}" />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
		<!-- 连接池启动时的初始值 -->
		<property name="initialSize" value="${initialSize}" />
		<!-- 连接池的最大值 -->
		<property name="maxActive" value="${maxActive}" />
		<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
		<property name="maxIdle" value="${maxIdle}" />
		<!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
		<property name="minIdle" value="${minIdle}" />
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
				<value>cn/ehoo/bean/Person.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<value>
				hibernate.dialect =org.hibernate.dialect.MySQLDialect
				hibernate.cache.use_second_level_cache=false
				hibernate.hbm2ddl.auto=update 
				hibernate.show_sql=false 
				hibernate.format_sql=false
			</value>
		</property>
	</bean>
	
	
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>
</beans>




同时在src下加入jdbc.properties配置文件

driverClassName=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8
username=root
password=123
initialSize=1
maxActive=500
maxIdle=2
minIdle=1





4、编写person.java

package cn.ehoo.bean;

/**
 * @author whp
 * @Email [email protected]
 * @Jan 4, 2011
 * 
 */
public class Person {
	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}



5、编写personService.java与PersonServiceBean.java类。

PersonService.java接口类

package cn.ehoo.service;

import java.util.List;

import cn.ehoo.bean.Person;

/**
 * @author whp
 * @Email [email protected]
 * @Jan 4, 2011
 * 
 */
public interface PersonService {
	/**
	 * 保存
 	 * @param person
	 */
	public void save(Person person);
	/**
	 *更新
	 * @param person
	 */
	public void update(Person person);
	/**
	 * 获取
	 * @param person
	 */
	public Person getPerson(Long personId);
	/**
	 * 获取所有
	 * @param person
	 */
	public List<Person> getPersons();
	/**
	 * 删除指定记录
	 * @param person
	 */
	public void delete(Long personId) throws Exception;

}



PersonServiceBean.java类
package cn.ehoo.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.ehoo.bean.Person;
import cn.ehoo.service.PersonService;

/**
 * @author whp
 * @Email [email protected]
 * @Jan 4, 2011
 * 
 */
@Service("personService")
@Transactional//代表类需要事务管理
public class PersonServiceBean implements PersonService {
	@Resource private SessionFactory sessionFactory;

	public void delete(Long personId) throws Exception {
		sessionFactory.getCurrentSession().delete(personId);
	}

	public Person getPerson(Long personId) {
		return (Person) sessionFactory.getCurrentSession().get(Person.class, personId);
	}

	@SuppressWarnings("unchecked")
	public List<Person> getPersons() {
		return (List<Person>) sessionFactory.getCurrentSession().createQuery("from Person o").list();
	}

	public void save(Person person) {
		sessionFactory.getCurrentSession().save(person);

	}

	public void update(Person person) {
		sessionFactory.getCurrentSession().merge(person);
	}
}



6 加入PersonAction.java

package cn.ehoo.web.acion;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import cn.ehoo.service.PersonService;

/**
 * @author whp
 * @Email [email protected]
 * @Nov 16, 2010
 * 
 */
@Controller("/person/list")
public class PersonAcion extends Action {
	@Resource PersonService personService; 
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		/*WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
		PersonService personService = (PersonService) ctx.getBean("personService");*/
		request.setAttribute("persons", personService.getPersons());
		return mapping.findForward("list");
	} 

}




7、在WEB-INF目录下新建一个目录page目录,在page目录下加入jsp页面personlist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
	<c:forEach items="${persons}" var="person">
		ID=${person.id} name=${person.name }  
	</c:forEach>
	</body>
</html>



8、WEB-INF目录下的struts-config.xml的配置为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
	<data-sources />
	<action-mappings>
		<action path="/person/list"  validate="false" >
			<forward name="list" path="/WEB-INF/page/personlist.jsp" />
		</action>
	</action-mappings>
	<!-- 注册spring控制器 -->
	<controller>
		<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
	</controller>
</struts-config>




9、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">


	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
	<!-- 对Spring容器进行实例化 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<servlet>
		<servlet-name>struts</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>struts</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>



10、在tomcat下布置运行
















你可能感兴趣的:(java,apache,spring,Hibernate,struts)