Hibernate使用TransactionProxyFactoryBean

Hibernate使用TransactionProxyFactoryBean
2008年04月24日 星期四 15:45
------------------------------------------------------------------------------------------------------------------------


TransactionProxyFactoryBean
Hibernate使用TransactionProxyFactoryBean
------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------
CustomersDAO.java
------------------------------------------------------------------------------------------------------------------------
package db;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

public class CustomersDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CustomersDAO.class);

protected void initDao() {
// do nothing
}
private TransactionTemplate transactionTemplate;
public void setTransactionManager(
PlatformTransactionManager transactionManager) {
this.transactionTemplate = new TransactionTemplate(transactionManager);
}


public void insertUser(Customers transientInstance) {
getHibernateTemplate().saveOrUpdate(transientInstance);
}

}
------------------------------------------------------------------------------------------------------------------------

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


<bean
>
<property
value="com.mysql.jdbc.Driver">
</property>
<property
value="jdbc:mysql://localhost:3306/sampledb">
</property>
<property value="root"></property>
</bean>
<bean
>
<property >
<ref bean="localMysql" />
</property>
<property >
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property >
<list>
<value>db/Orders.hbm.xml</value>
<value>db/Customers.hbm.xml</value>
</list>
</property>
</bean>

<!-- 定义事务管理器,使用适用于Hibernte的事务管理器-->

<bean
>
<property >
<ref bean="localSessionFactory" />
</property>
</bean>

<!--定义DAO Bean , 作为事务代理的目标-->

<bean >
<property >
<ref bean="localSessionFactory" />
</property>
</bean>
<!-- 定义DAO bean的事务代理-->

<bean
>
<property >
<ref bean="transactionManager" />
</property>
<property >
<ref bean="userDAOTarget" />
</property>
<property >
<props>
<prop key="inserUser">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

</beans>

------------------------------------------------------------------------------------------------------------------------

Demo .java
------------------------------------------------------------------------------------------------------------------------
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import db.Customers;
import db.CustomersDAO;
public class Demo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CustomersDAO dao = (CustomersDAO) ctx.getBean("userDAOTarget");
Customers c = new Customers();
c.setId(3l);
c.setName("xxx");
dao.insertUser(c);
System.out.println("END");
}
}



------------------------------------------------------------------------------------------------------------------------


本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源 Hibernate使用TransactionProxyFactoryBean_熊熊之家

你可能感兴趣的:(DAO,Hibernate,bean,mysql,xml)