spring hibernate struts 整合开发(1)

一. spring hibernate struts 整合开发(1) - 搭建环境
二. spring hibernate struts 整合开发(2) - Spring集成的Hibernate编码和测试
三. spring hibernate struts 整合开发(3) - Struts集成Spring
四. spring hibernate struts 整合开发(4) - Struts与Spring集成2
五. spring hibernate struts 整合开发(5) - Hibernate二级缓存
六. spring hibernate struts 整合开发(6) - 额外功能


1. Eclipse中创建一个web工程,Resource设置成utf-8

2. 导入下面的jar包到WEB-INF/lib默认路径:
Hibernate 3安装包下的:
hibernate3.jar
lib\required\*.jar
lib\optional\ehcache-1.2.3.jar(做二级缓存用)
lib\test\slf4j-log4j12.jar (配置log)

spring 2.5 安装包下的:
dist\spring.jar
dist\modules\spring-webmvc-struts.jar
lib\jakarta-commons\common-logging.jar commons-dbcp.jar commons-pool.jar
lib\aspectj\aspectjweaver.jar aspectjrt.jar
lib\cglib\cglib-nodep-2.1_3.jar
lib\j2ee\common-annotations.jar
lib\log4j\log4j-1.2.15.jar

Struts 1.3.8
下载struts-1.3.8-lib.zip,解压目录下的所有jar

3. 拷贝beans.xml到src路径:

beans.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:context="http://www.springframework.org/schema/context"
	   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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- 数据库连接和连接池配置 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"/>
		<property name="username" value="root"/>
		<property name="password" value="123456"/>
		<!-- Initial value when starting up -->
		<property name="initialSize" value="1"/>
		<!-- Maximum value of the pool -->
		<property name="maxActive" value="500"/>
		<!-- Maximum idle value of the pool -->
		<property name="maxIdle" value="2"/>
		<!-- Minimum idle value of the pool -->
		<property name="minIdle" value="1"/>
	</bean>

	<!-- Hibernate二级缓存配置 -->	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="mappingResources">
			<list>
				<value>com/john/bean/Person.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.hbm2ddl.auto=update
				hibernate.show_sql=true
				hibernate.format_sql=false
			</value>
		</property>
	</bean>
	
	<!-- 侦测并激活bean类中的注解:Spring的@Required和@Autowired, JSR 250的@PostConstruct, @PreDestroy和@Resource, JAX-WS的@WebServiceRef, EJB3的@EJB, JPA的@PersistenceContext和@PersistenceUnit。这个标签并不激活Spring的@Transactional或者EJB的@TransactionAttribute。如果这个标签没有,这些注解的注入将无法执行。-->
	<context:annotation-config />
	
	<!-- 事务管理器配置 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 注解事务驱动 -->
	<tx:annotation-driven transaction-manager="txManager"/>
	
	<!-- 业务bean -->
	<bean id="personService" class="com.john.service.impl.PersonServiceBean"/>
</beans>


4. 新建一个com.john.bean.Person类以及映射文件Person.hbm.xml

public class Person {
	private Integer id;
	private String name;
	// Setters and getters are omitted
}


<?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 package="com.john.bean">
	<class name="Person" table="person">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length="20" not-null="true" />
	</class>
</hibernate-mapping>


整理自:传智播客spring教程

猜你喜欢

转载自czj4451.iteye.com/blog/1526897