1、新建项目:logistic
添加spring的jar包:
右击项目,选择Myeclipse->Add Spring Capilities
选择spring3.0,勾选5个jar 包:
Spring 3.0 AOP Libraries ,
Spring 3.0 Core Libraies ,
Spring 3.0 Persistence Core Libraies ,
Spring 3.0 Persistence JDBC Libraies ,
OpenJPA Lbraries
点“Next”,如图:
这里可以选择spring配置文件的存放位置,现在我选择默认的位置在src下面。
点“Finish”,就能完成spring 的jar包的导入。
添加hibernate的jar包:
右击项目,选择Myeclipse->Add Hibernate Capilities
选择Hibernate3.3,其他默认,点“Next”:
选择“spring configuration file (applicationContext.xml)”, 点“Next”:
选择 “Existing Spring configuration file”,点“Next”
在DB Driver选择配好的数据库,或者选择Myeclipse Derby,(到时候再进行修改),点击next
撤销勾选“Create SessionFactory class?”
点“Finish”,就完成hibernate的jar包的导入。
添加struts的jar包:
右击项目,选择Myeclipse->Add Struts Capilities
选择struts2.1, URl pattern选择/*,点击“Next”:
勾选“Struts 2 Core Libraries”和“Struts 2 Spring Libraries”,点“Finish”。
Spring3.0,hibernate3.3,struts 2.1 添加完成后,如图:
以上的jar包可直接使用,没有包冲突(暂时没发现),不需删除相同的jar包或做其他jar包的处理。
Web.xml:
需要在web.xml 文件内添加spring配置信息:
把如下代码添加在<welcome-file-list>的标签后,
<!-- Spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring上下文参数,指定配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
applicationContext.xml:
(1)、原文件的beans标签:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
在applicationContext.xml的beans标签内添加如下代码:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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/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
">
(2)、为了能使用annotation,在applicationContext.xml添加
<context:annotation-config />
<context:component-scan base-package="org.logistic" />
base-package:是包前部分的命名,如,加上上述代码后就能浏览org.logistic.*.*的所有的类。
(3)、数据库的配置
根据自己需求去修改用户名、密码等,
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/logisticssys_test">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="packagesToScan">
<list>
<value>org.logistic.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
(4)事务的添加,用AOP。
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * org.logistic.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--在类的loadAll的方法上添加事务,-->
<tx:method name="loadAll" read-only="true" />
<!-- <tx:method name="add*" propagation="REQUIRED"/> -->
</tx:attributes>
</tx:advice>
(5)HibernateTemplate
<bean
id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>