SSH(Struts2+Spring+Hibernate)整合篇

DAO.java
public interface Dao {
public List<Ly> getAllLy();
public int save(Ly ly);
}


applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/lai"></property>
<property name="username" value="lai"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<value>entity/Ly.hbm.xml</value>
</property>
</bean>
<bean id="de" class="action.De">
<property name="sessionFactory" ref="sessionFactory"></property>
//使用原生的api时,需要利用setter方法
</bean>
</beans>


De.java
package action;


import java.util.List;


import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import dao.Dao;
import entity.Ly;


public class De extends HibernateDaoSupport implements Dao{


@Override
public List<Ly> getAllLy() {
// TODO Auto-generated method stub
return (List<Ly>)super.getHibernateTemplate().find("from Ly");
}
public int save(Ly ly) {
// TODO Auto-generated method stub
return (Integer)super.getHibernateTemplate().save(ly);
}
/*原生方法
private SessionFactory sessionFactory;


@Override
public List<Ly> getAllLy() {
// TODO Auto-generated method stub
Query q = sessionFactory.openSession().createQuery("from Ly");
//当没有事务时,不可调用getCurrentSession
return q.list();
}


@Override
public int save(Ly ly) {
// TODO Auto-generated method stub
return 0;
}




public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}*/
}


Test.java
public static void main(String[] args) {
ApplicationContext c = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Dao d = (Dao) c.getBean("de");
List<Ly> lys = d.getAllLy();
for (Ly ly : lys) {
System.out.println(ly.getLText());
}
}




Spring对Hibernate的声明式事务管理(看不球太懂)
<?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: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/tx
    http://www.springframework.org/schema/tx/spring-tx-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/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<!-- data数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/lai"></property>
<property name="username" value="lai"></property>
<property name="password" value="root"></property>
</bean>






<!-- session会话工厂的配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<value>entity/Ly.hbm.xml</value>
</property>
</bean>




<!-- hibernate3tx的事务类 -->
<bean id="txmanager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- hibernate4tx的事务类 <bean id="txmanager4" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
/> -->


<aop:config>
<aop:pointcut id="deptServiceMethods" expression="execution(* action.De.*(..))" />
<aop:advisor advice-ref="tx" pointcut-ref="deptServiceMethods" />
</aop:config>


<tx:advice id="tx" transaction-manager="txmanager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 实际类的配置 -->
<bean id="de" class="action.De">
<property name="session" ref="txmanager"></property>
</bean>
</beans>
以上是Spring和Hibernate整合
在Struts2和Spring整合时,需要导入Struts-Spring的jar包,无需配置Struts2监听器
struts.xml中的class全部由bean的id替换

猜你喜欢

转载自blog.csdn.net/qq_39471933/article/details/80931027