Struts2的配置:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- struts的注入交给spring去控制 --> <constant name="struts.objectFactory" value="spring"/> <include file="struts-default.xml"/> <!-- Add packages here --> <package name="struts" extends="struts-default"> <action name="addBuygoods" class="com.dgy.action.BuygoodsAction" method="add"> <result name="success" type="redirect">listBuyGoods.action</result> </action> <action name="buygoods" class="com.dgy.action.BuygoodsAction" > <result name="previous">previousmybuygoodsbeforeadd.jsp</result> <result name="add" type="redirect">listBuyGoods.action</result> </action> <action name="previousMyGood" class="com.dgy.action.BuygoodsAction" method="previous"> <result name="success">previousbeforeadd.jsp</result> </action> </package> </struts>
Spring的配置(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:aop="http://www.springframework.org/schema/aop" 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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="sortinfoDao" class="com.dgy.dao.SortinfoDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="buygoodsDao" class="com.dgy.dao.BuygoodsDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="sellMessageDao" class="com.dgy.dao.SellmessageDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="userinfoDao" class="com.dgy.dao.UserinfoDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="buyGoodsAction" class="com.dgy.action.BuygoodsAction"> <property name="buygoodsDao"> <ref bean="buygoodsDao"/> </property> <property name="userinfoDao"> <ref bean="userinfoDao"/> </property> </bean> <bean id="pagerService" class="com.dgy.util.PagerService"> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <!-- 配置事务特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置哪些类的方法进行事务管理 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution (* com.dgy.dao.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/> </aop:config> </beans>
Spring 下的测试:
public void testQuery(){
BeanFactory factory= new ClassPathXmlApplicationContext("ApplicationContext.xml");
BuygoodsDao dao=(BuygoodsDao)factory.getBean("buygoodsDao");
List<EbayBuygoods> list=dao.queryBooks("BName","H");
for(EbayBuygoods e:list){
System.out.println(e.getBName());
}
Hibernate 下的测试:
public class BuygoodTest extends TestCase {
public void testAdd(){
//Spring deconstrustion
BeanFactory factory= new ClassPathXmlApplicationContext("ApplicationContext.xml");
BuygoodsDao dao=(BuygoodsDao)factory.getBean("buygoodsDao");
// Time Format
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
String endtime=Integer.toString(calendar.get(Calendar.YEAR))+"-"+Integer.toString(calendar.get(Calendar.MONTH)+9)+"-"+Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
//Hibernate method
Configuration cfg = new Configuration().configure();
SessionFactory factory1 = cfg.buildSessionFactory();
Session session = factory1.openSession();
//Get Object by Id
EbaySortinfo ebaySortinfo=(EbaySortinfo)session.get(EbaySortinfo.class,"263");
EbayUserinfo ebayUserinfo=(EbayUserinfo)session.get(EbayUserinfo.class,"1");
EbayBuygoods ebayBuygoods=new EbayBuygoods();
ebayBuygoods.setBAble("0");
ebayBuygoods.setBContent("Entiry New Brandxx");
ebayBuygoods.setBEndtime(endtime);
ebayBuygoods.setBName("THC");
ebayBuygoods.setBPrice("18.00");
ebayBuygoods.setBQuantity("10");
ebayBuygoods.setCreatetime(sdf.format(new Date()));
ebayBuygoods.setEbaySortinfo(ebaySortinfo);
ebayBuygoods.setEbayUserinfo(ebayUserinfo);
dao.addBuyinglead(ebayBuygoods);
}