67,513
社区成员
发帖
与我相关
我的任务
分享
dao类
package com.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.entity.Orders;
public class OrdersDao extends HibernateDaoSupport{
@SuppressWarnings("unchecked")
public List<Orders> getAllOrders(){
return this.getHibernateTemplate().find("from Orders as o");
}
public void update(Orders order){
this.getHibernateTemplate().update(order);
}
public void delete(int id) {
}
public Orders getOrderById(int id){
return this.getHibernateTemplate().get(Orders.class, id);
}
}
action类
package com.action;
import java.sql.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.entity.Orders;
import com.service.OrdersService;
@Transactional(propagation=Propagation.REQUIRED)
public class OrdersAction extends DispatchAction {
private OrdersService ordersService;
public OrdersService getOrdersService() {
return ordersService;
}
public void setOrdersService(OrdersService ordersService) {
this.ordersService = ordersService;
}
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
List<Orders> orders = this.ordersService.getOrders();
request.setAttribute("orders", orders);
return mapping.findForward("list");
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
Orders or = ordersService.getOrderById(1);
or.setCustName("yy1");
this.ordersService.updateUser(or);
return null;
}
}
配置文件
app.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="ordersDao" class="com.dao.OrdersDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="ordersService" class="com.service.OrdersService">
<property name="ordersDao" ref="ordersDao"></property>
</bean>
<bean name="/ordersAction" class="com.action.OrdersAction">
<property name="ordersService" ref="ordersService"></property>
</bean>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/ordersAction" type="com.action.OrdersAction" scope="request" parameter="method">
<forward name="list" path="/ordersList.jsp"></forward>
<forward name="update" path="/orderAction.do?method=update"></forward>
</action>
</action-mappings>
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
<message-resources parameter="ApplicationResources" />
</struts-config>