62,623
社区成员
发帖
与我相关
我的任务
分享
<?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 id="daoa" class="com.dao.DaoA">
<property name="hibernateTemplate" ref="ht"></property>
</bean>
<bean id="bo" class="com.dao.Bo">
<property name="a" ref="daoa"></property>
</bean>
<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sf"></property>
</bean>
<bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="proxydao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="tm"></property>
<property name="target" ref="bo"></property>
<property name="transactionAttributes">
<props>
<prop key="execute">
PROPAGATION_REQUIRED
</prop>
</props>
</property>
</bean>
<bean id="tm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf"></property>
</bean>
</beans>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">MySQL</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="com/jopo/person.xml" />
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.jopo">
<class name="Person">
<id name="id">
<generator class="native"></generator>
</id>
</class>
</hibernate-mapping>
package com.dao;
import com.jopo.Person;
public class Bo {
private DaoA a;
public void execute(Person p){
a.insert(p);
}
public DaoA getA() {
return a;
}
public void setA(DaoA a) {
this.a = a;
}
}
DAO类
[code=Java]
package com.dao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.jopo.Person;
public class DaoA extends HibernateDaoSupport {
public void insert(Person p){
this.getHibernateTemplate().save(p);
}
public static void main(String[] args){
ApplicationContext app=
new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","hibernate.cfg.xml"});
Bo a=(Bo) app.getBean("bo");
// DaoA a = (DaoA)app.getBean("daoa");
Person p=new Person();
p.setId(332);
a.execute(p);
}
}
package com.jopo;
public class Person {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}