您的位置:首页 > 其它

SSH整合

2016-12-13 22:52 190 查看
1.spring

导入spring的jar包

测试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"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

<!-- 在spring中注册action  由spring来接管action  -->
<bean id="PetAction" class="com.my.action.PetAction">
<property name="petService" ref="PetService"></property>
</bean>

</beans>


2.整合Hibernate

导入hibernate的jar包

测试hibernate是否正常工作

在整合spring中,因为要整合spring 也就是spring接管hibernate

所以把hibernate.cfg.xml的配置配置到spring配置文件中

在Spring中首先要配置一个数据源(dbcp/c30p/jdbc/druid)

再配置一个sessionFactory(需要给SessionFactory注入一个数据源)

因为spring要接管hibernate的事务 所以要配置一个事务管理器

再通过aop的方式来加入事务管理

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!--配置一个数据源 -->
<bean id="myDataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<!-- 数据库驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- 数据库路径 -->
<property name="url" value="jdbc:mysql:///epet" />
<!-- 数据库用户名 -->
<property name="username" value="root" />
<!-- 数据库密码 -->
<property name="password" value="0000" />
</bean>

<!-- 配置一个SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 应用数据库 因为要连接数据库 -->
<property name="dataSource" ref="myDataSource" />
<property name="mappingResources">
<list>
<value>com/my/bean/PetInfo.hbm.xml</value>
<value>com/my/bean/PetDairy.hbm.xml</value>
<value>com/my/bean/PetType.hbm.xml</value>
</list>
</property>

<!--配置hibernate的属性配置 -->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql =true
hibernate.hbm2ddl.auto=update
</value>
<!-- <props> <prop key=" hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key=" hibernate.show_sql">true</prop> </props> -->
</property>
</bean>

<!--hibernate的事务也要交给spring 接管,所以需要配置一个事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!--使用spring aop 来管理的事务,所以要配置aop -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.my.Impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>

<!--配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- propagation="REQUIRED -->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" />
<tx:method name="show*" read-only="true" />
</tx:attributes>
</tx:advice>

<bean id="PetDao" class="com.my.Impl.PetDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="PetService" class="com.my.seviceImpl.PetServiceImpl">
<property name="petDaoImpl" ref="PetDao"></property>
</bean>

<!-- <import resource="spring-ActionBean.xml" /> -->

</beans>


3.struts 整合

导入struts的jar包

测试是否正常工作

导入插件包整合struts-plugin-2.5.2.jar

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

<struts>
<!--把struts生产action的权利交给spring -->
<constant name="struts.objectFactory" value="spring" />

<package name="one" extends="json-default" namespace="/">
<action name="PetAction_*" class="PetAction" method="{1}">
<result name="show">/showPet.jsp</result>
<result name="error">/error.jsp</result>
<result type="json" />
<allowed-methods>showPet,getALLpet,deleteById,updatePet,addPetDiary,login</allowed-methods>
</action>
</package>

</struts>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SSH