蔡超
北京天融信,软件架构师
SUN certified Enterprise Architect
Microsoft certified Solution Developer
IBM certified RUP Specialist
联系方式
:cai_chao@topsec.com.cn,chaocai2001@yahoo.com.cn
010-82776427
Sping2
及
Hibernate3
都是如今流行的请量级框架,如何将两者进行整合呢,目前很多资料讨论的都是老版本的整合方式,下面给出这两种框架的新版整合方式。
1
配置数据源
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=webguide</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>talent</value>
</property>
</bean>
2
配置
Hibernate
SessionFactory
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<!—Hibernate
映射文件列表
-->
<list>
<value>Greeting.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
3
配置事务
<bean id="myTranAttri" class="org.springframework.transaction.interceptor.DefaultTransactionAttribute">
<property name="propagationBehaviorName">
<value>PROPAGATION_REQUIRED</value>
</property>
</bean>
<bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
<property name="transactionAttribute">
<ref bean="myTranAttri"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
<bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor" lazy-init="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource"/>
</property>
</bean>
4
使用
Hibernate
实体
方法一:采用
AOP
的方式
package hibernate.integration.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
/**
* @author chao cai
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GreetingLogic3 implements IGreetingLogic{
private SessionFactory sessionFactory;
/* (non-Javadoc)
* @see hibernate.integration.entity.IGreetingLogic#loadGreeting(java.lang.Integer)
*/
public Greeting loadGreeting(Integer id) {
Session session = sessionFactory.getCurrentSession();
Greeting greeting=(Greeting) session.get(Greeting.class,id);
return greeting;
}
/**
* @return Returns the sessionFactory.
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* @param sessionFactory The sessionFactory to set.
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
代码样例:
package hibernate.integration.entity;
/**
* @author chao cai
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public interface IGreetingLogic {
public Greeting loadGreeting(Integer id);
}
配置
<bean id="myGreetingLogic" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>hibernate.integration.entity.IGreetingLogic</value>
</property>
<property name="interceptorNames">
<list>
<value>txInterceptor</value>
<value>myHibernateInterceptor</value>
</list>
</property>
<property name="target">
<ref bean="greetingLogic3"/>
</property>
</bean>
客户端代码:
AbstractApplicationContext context=new FileSystemXmlApplicationContext("spring-hibernate.xml");
IGreetingLogic gl=(IGreetingLogic) context.getBean("myProductDao");
greeting=gl.loadGreeting(new Integer(1));
System.out.println(greeting.getGreeting());
方法二:采用
HibernateDaoSupport
代码:
package hibernate.integration.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* @author Chao Cai
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GreetingLogic extends HibernateDaoSupport implements IGreetingLogic{
public Greeting loadGreeting(Integer id){
Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
Greeting greeting=(Greeting) session.get(Greeting.class,id);
return greeting;
}
}
<bean id="greetingLogic" class="hibernate.integration.entity.GreetingLogic">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>