日期:2014-05-20 浏览次数:20690 次
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property> <property name="url"><value>jdbc:mysql://localhost:3306/orm</value></property> <property name="username"><value>root</value></property> <property name="password"><value>root</value></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"><ref local="dataSource"/></property> <property name="mappingResources"> <list> <value>com/itlover/hibernate/User.hbm.xml</value> <value>com/itlover/hibernate/Card.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean> <bean id="baseDao" class="com.itlover.dao.DefaultDaoImpl"> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean> <bean id="baseService" class="com.itlover.service.DefaultServiceImpl"> <property name="baseDao"><ref local="baseDao"/></property> </bean> <bean id="defaultDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref bean="transactionManager"/></property> <property name="target"><ref local="baseDao"/></property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> <bean name="/hibernate" class="com.itlover.struts.action.HibernateAction" singleton="false"> <property name="baseService"><ref bean="baseService"/></property> </bean> </beans>
Action.java public ActionForward test(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { User user = new User(); user.setUserName("susan"); user.setUserAge(21); Card card = new Card(); // card.setCardBankname("招商银行"); card.setCardNumber("6225888811558899"); card.setUser(user); user.getCards().add(card); baseService.saveEntity(user); return null; } Service.java public class DefaultServiceImpl implements BaseService { private BaseDao baseDao; public void saveEntity(Object entity) { baseDao.saveEntity(entity); } public void setBaseDao(BaseDao baseDao) { this.baseDao = baseDao; } } Dao.java public class DefaultDaoImpl extends HibernateDaoSupport implements BaseDao { public void saveEntity(Object entity) { getHibernateTemplate().save(entity); } }