SSH整合

来源:互联网 发布:黑道圣徒3御姐捏脸数据 编辑:程序博客网 时间:2024/04/29 14:23


Struts2.x + Hibernate4.x +Spring4.x整合

先整合Spring4.xHibernate4.x

加入Spring的支持:

*导入必要的jar

*web.xml中配置一个Listener

listener配置如下:

<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>

 

配置SpringapplicationContext.xml配置文件

配置数据源对象:

<!-- 配置数据源信息 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="user" value="root"></property>

<property name="password" value="admin"></property>

<property name="jdbcUrl" value="jdbc:mysql:///day1"></property>

<property name="driverClass" value="com.mysql.jdbc.Driver"></property>

 

<property name="minPoolSize" value="10"></property>

<property name="maxPoolSize" value="50"></property>

<property name="initialPoolSize" value="10"></property>

<property name="acquireIncrement" value="5"></property>

</bean>

 

配置SesisonFactory对象:

<!-- 配置SessionFactory对象信息 -->

 

<bean id="sessionFactory"

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

</bean>

备注:这里其实也可以配置其它属性比如mappingLocation、个人喜好。

 

配置Spring的声明式事务

<!-- 配置Spring的声明式事务 -->

<!-- 1.配置hibernate的声明式管理器 -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

 

<!-- 2.配置事务属性 -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="get" read-only="true" />

<tx:method name="*" />

</tx:attributes>

</tx:advice>

 

<!-- 3.配置事务切入点,再把事务属性和事务切入点关联起来 -->

<aop:config>

<aop:pointcut expression="execution(* com.xuyi.service.*.*(..))"

id="txPointcut" />

<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />

</aop:config>

 

添加一些普通bean主要是daoserviceaction;

备注:配置Action bean的时候比较特殊,注意它的scope属性是prototype

 

 

 

加入Hibernate4.x的支持:

*导入必要的jar

*创建hibernate.cfg.xml配置文件

备注:主配置文件中只放入hibernate的基本信息

1:数据库方言、建表策略、显示sql语句

2:数据源和SesisonFactory创建放入到Spring中配置

*创建映射文件(其实也可以放入spring中,看个人喜好)

 

加入Struts2.x的支持:

*加入核心jar包,注意还有struts-spring的插件包

*web.xml中添加StrutsPreparedAndExecuteFilter核心过滤器

*配置struts.xml配置文件

备注:struts.xml中的actionclass指定为springid

 

 

备注:logging.jarlog4j.properties配置文件、插件jar包。

0 0
原创粉丝点击