Spring2.5+Hibernate3.6+struts1.3整合(1)
首先我们整合spring+hibernate;
(1)拷贝所需要的jar文件到/WEB-INF/lib文件夹,顺便把所需要的数据库驱动包也拷贝进去,我用的是Oracle,所以我拷贝classes12.jar到lib目录。
????? hibernate3.6所需要的jar文件如下:
?
????? 
?? spring2.5所需要的jar文件如下:(注意antlr.jar只能有一个,)
?
?(2)拷贝一个spring配置文件模板到classpath路径,便于以后利用ClassPathXmlApplicationContext类来初始化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: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">
??? ?
</beans>
?(3)接下来我们需要在spring配置文件(我这里是beans.xml文件名随便)配置数据连接池:
?? <!-- 连接池的配置 -->
??? ?<bean id="dataSource" destroy-method="close">
??? ??? <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
??? ??? <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
??? ??? <property name="username" value="scott"/>
??? ??? <property name="password" value="tiger"/>
??? ???? <!-- 连接池启动时的初始值 -->
??? ??? ?<property name="initialSize" value="1"/>
??? ??? ?<!-- 连接池的最大值 -->
??? ??? ?<property name="maxActive" value="500"/>
??? ??? ?<!-- 最大空闲值 -->
??? ??? ?<property name="maxIdle" value="2"/>
??? ??? ?<!--? 最小空闲值 -->
??? ??? ?<property name="minIdle" value="1"/>
??? ? </bean>
?(4)下面我们配置SessionFactory配置如下这里用到上面配置的连接池id
??? <!-- 配置sessionFactory -->
??? <bean id="sessionFactory" ref="dataSource"/>
??? ??? ?<property name="mappingResources">
??? ??? ??? <list>
??????????????? <!--配置映射文件可以多个-->
??? ??? ????? <value>com/java/entity/Person.hbm.xml</value>
??? ??? ??? </list>
??? ??? ?</property>
?????????????? <!--配置hibernate属性-->
??? ???? <property name="hibernateProperties">
??? ??? ??? <value>
??? ??? ??????? hibernate.dialect=org.hibernate.dialect.OracleDialect
??? ??? ??????? hibernate.hbm2ddl.auto=update
??? ??? ??????? <!-- 测试阶段使用 -->
??? ??? ??????? hibernate.show_sql=true
??? ??? ??????? hibernate.format_sql=true
??? ??? ????? </value>
??? ???? </property>
??? </bean>
(5)接下来我们配置事务管理:
? <!-- 配置事务管理 -->
??? <bean id="txManager" ref="sessionFactory"/>
??? </bean>
(6)事务管理我们可以使用配置文件来设置也可以利用注解方式来配置:(我用的是注解方式这样简便);这样的话就需要我们打开注解方式配置事务,标签属性如下:
?<!-- 基于注解方式 -->
??? <tx:annotation-driven transaction-manager="txManager"/>
(7)下面我们写一个实体来进行测试:
package com.java.entity;
public class Person {
??? private int id;
??? private String name;
??? public Person(){}
??? public Person(String name) {
??? ??? this.name=name;
??? }
??? public int getId() {
??? ??? return id;
??? }
??? public void setId(int id) {
??? ??? this.id = id;
??? }
??? public String getName() {
??? ??? return name;
??? }
??? public void setName(String name) {
??? ??? this.name = name;
??? }
}
相对应的映射文件如下:Person.hbm.xml
?<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
??????? "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
??????? "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.java.entity">
??? <class name="Person" table="t_person">
??????? <id name="id">
??????????? <generator not-null="true" />
??? </class>
</hibernate-mapping>
?
? 由于文字太长得分开好几篇才行。。。
1 楼 rancococ 2011-05-24 晕,spring3.2.。。。