jar包
/order/WebRoot/WEB-INF/lib/aopalliance.jar
/order/WebRoot/WEB-INF/lib/asm-3.3.1.jar
/order/WebRoot/WEB-INF/lib/asm-commons-3.3.jar
/order/WebRoot/WEB-INF/lib/asm-tree-3.3.jar
/order/WebRoot/WEB-INF/lib/aspectjweaver.jar
/order/WebRoot/WEB-INF/lib/cglib-2.2.2.jar
/order/WebRoot/WEB-INF/lib/commons-fileupload-1.2.2.jar
/order/WebRoot/WEB-INF/lib/commons-io-2.0.1.jar
/order/WebRoot/WEB-INF/lib/commons-lang3-3.1.jar
/order/WebRoot/WEB-INF/lib/commons-logging-1.1.1.jar
/order/WebRoot/WEB-INF/lib/freemarker-2.3.19.jar
/order/WebRoot/WEB-INF/lib/javassist-3.17.1-GA.jar
/order/WebRoot/WEB-INF/lib/jstl.jar
/order/WebRoot/WEB-INF/lib/log4j-1.2.17.jar
/order/WebRoot/WEB-INF/lib/log4j-api-2.0-rc1.jar
/order/WebRoot/WEB-INF/lib/log4j-core-2.0-rc1.jar
/order/WebRoot/WEB-INF/lib/mybatis-3.2.7.jar
/order/WebRoot/WEB-INF/lib/mybatis-spring-1.2.3.jar
/order/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.20.jar
/order/WebRoot/WEB-INF/lib/ognl-3.0.5.jar
/order/WebRoot/WEB-INF/lib/slf4j-api-1.7.5.jar
/order/WebRoot/WEB-INF/lib/slf4j-log4j12-1.7.5.jar
/order/WebRoot/WEB-INF/lib/spring-aop-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-aspects-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-beans-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-context-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-context-support-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-core-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-expression-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-jdbc-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-orm-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-tx-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-web-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/spring-webmvc-4.1.6.RELEASE.jar
/order/WebRoot/WEB-INF/lib/standard.jar
/order/WebRoot/WEB-INF/lib/struts2-core-2.3.4.jar
/order/WebRoot/WEB-INF/lib/struts2-spring-plugin-2.3.4.jar
/order/WebRoot/WEB-INF/lib/xwork-core-2.3.4.jar
OrderAction.java
package cn.sxt.action;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.sxt.service.DetailService;
import cn.sxt.service.OrderService;
import cn.sxt.vo.Detail;
import cn.sxt.vo.Order;
@Controller
@Scope("prototype")
public class OrderAction {
private List<Order> list;
private List<Detail> details;
private int orderId;
private int detailId;
@Autowired
private OrderService orderService;
@Autowired
private DetailService detailService;
//获取所有订单--getlist
public String List() {
list = orderService.list();
return "success";
}
//获取订单详情
public String detail() {
details = detailService.listByOrderId(orderId);
return "success";
}
//删除订单详情中的一项
public String delete() {
detailService.delete(orderId, detailId);
return "success";
}
public List<Order> getList() {
return list;
}
public void setList(List<Order> list) {
this.list = list;
}
public List<Detail> getDetails() {
return details;
}
public void setDetails(List<Detail> details) {
this.details = details;
}
public OrderService getOrderService() {
return orderService;
}
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
public DetailService getDetailService() {
return detailService;
}
public void setDetailService(DetailService detailService) {
this.detailService = detailService;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getDetailId() {
return detailId;
}
public void setDetailId(int detailId) {
this.detailId = detailId;
}
}
DetailDao.java
package cn.sxt.dao;
import java.util.List;
import cn.sxt.vo.Detail;
public interface DetailDao {
public List<Detail> listByOrderId(int id);
public int delete(int id);
}
OrderDao.java
package cn.sxt.dao;
import java.util.List;
import cn.sxt.vo.Order;
public interface OrderDao {
public List<Order> list();
public int update(Order order);
}
DetailDaoImpl.java
package cn.sxt.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import cn.sxt.dao.DetailDao;
import cn.sxt.vo.Detail;
@Repository("detailDao")
public class DetailDaoImpl extends SqlSessionDaoSupport implements DetailDao{
@Autowired
@Override
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
// TODO 自动生成的构造函数存根
}
@Override
public List<Detail> listByOrderId(int id) {
// TODO 自动生成的方法存根
return getSqlSession().selectList("cn.sxt.vo.detail.mapper.listByOrderId",id);
}
@Override
public int delete(int id) {
// TODO 自动生成的方法存根
return getSqlSession().delete("cn.sxt.vo.detail.mapper.delete",id);
}
}
OrderDaoImpl.java
package cn.sxt.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import cn.sxt.dao.OrderDao;
import cn.sxt.vo.Order;
@Repository("orderDao")
public class OrderDaoImpl extends SqlSessionDaoSupport implements OrderDao{
@Autowired
@Override
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
public List<Order> list() {
// TODO 自动生成的方法存根
return getSqlSession().selectList("cn.sxt.vo.order.mapper.list");
}
@Override
public int update(Order order) {
// TODO 自动生成的方法存根
return getSqlSession().update("cn.sxt.vo.order.mapper.update",order);
}
}
DetailService.java
package cn.sxt.service;
import java.util.List;
import cn.sxt.vo.Detail;
public interface DetailService {
public List<Detail> listByOrderId(int id);
public int delete(int orderId,int detailId);
}
OrderService.java
package cn.sxt.service;
import java.util.List;
import cn.sxt.vo.Order;
public interface OrderService {
public List<Order> list();
public int update(Order order);
}
DetailServiceImpl.java
package cn.sxt.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.sxt.dao.DetailDao;
import cn.sxt.dao.OrderDao;
import cn.sxt.service.DetailService;
import cn.sxt.vo.Detail;
@Service("detailService")
public class DetailServiceImpl implements DetailService{
@Autowired
private DetailDao detailDao;
@Autowired
private OrderDao orderDao;
@Override
public List<Detail> listByOrderId(int id) {
return detailDao.listByOrderId(id);
}
@Override
public int delete(int orderId,int detailId) {
return 0;
}
}
OrderServiceImpl.java
package cn.sxt.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.sxt.dao.OrderDao;
import cn.sxt.service.OrderService;
import cn.sxt.vo.Order;
@Service("orderService")
public class OrderServiceImpl implements OrderService{
@Autowired
private OrderDao orderDao;
@Override
public List<Order> list() {
// TODO 自动生成的方法存根
return orderDao.list();
}
@Override
public int update(Order order) {
// TODO 自动生成的方法存根
return orderDao.update(order);
}
}
Detail.java
package cn.sxt.vo;
public class Detail {
private int id;
private int orderId;
private int menuId;
private Menu menu;
private int num;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getMenuId() {
return menuId;
}
public void setMenuId(int menuId) {
this.menuId = menuId;
}
public Menu getMenu() {
return menu;
}
public void setMenu(Menu menu) {
this.menu = menu;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
Menu.java
package cn.sxt.vo;
public class Menu {
private int id;
private String name;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Order.java
package cn.sxt.vo;
public class Order {
private int id;
private int deskId;
private double totalPrice;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getDeskId() {
return deskId;
}
public void setDeskId(int deskId) {
this.deskId = deskId;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
}
detail.mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.sxt.vo.detail.mapper">
<select id="listByOrderId" parameterType="int" resultMap="DetailMenu">
select * from t_orderdetail where orderId=#{orderId}
</select>
<resultMap type="Detail" id="DetailMenu">
<association property="menu" column="menuId" javaType="Menu" select="cn.sxt.vo.menu.mapper.getById"></association>
</resultMap>
<delete id="delete" parameterType="int">
delete from t_orderdetail where id=#{id}</delete>
</mapper>
menu.mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.sxt.vo.menu.mapper">
<select id="getById" parameterType="int" resultType="Menu">
select * from t_menu where id=#{id}
</select>
</mapper>
order.mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.sxt.vo.order.mapper">
<select id="list" resultType="Order">
select * from t_order
</select>
<update id="update" parameterType="Order">
update t_order set deskId=#{deskId},totalPrice=#{totalPrice} where id=#{id}
</update>
</mapper>
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="1012"/>
</bean>
<!-- 声明式事务配置开始 -->
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 配置哪些方法使用声明样的事务,配置事务的传播特性 -->
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="insert" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="get" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<!-- 声明式事务配置结束 -->
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
</bean>
<context:component-scan base-package="cn.sxt"/>
</beans>
mybatis.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="cn.sxt.vo"/>
</typeAliases>
<mappers>
<mapper resource="cn/sxt/vo/detail.mapper.xml"/>
<mapper resource="cn/sxt/vo/menu.mapper.xml"/>
<mapper resource="cn/sxt/vo/order.mapper.xml"/>
</mappers>
</configuration>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="list" class="orderAction" method="list">
<result>list.jsp</result>
</action>
<action name="detail" class="orderAction" method="detail">
<result>detail.jsp</result>
</action>
<action name="delete" class="orderAction" method="delete">
<result type="redireAction">list</result></action></package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<!-- 配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<display-name>16spring4_stucts2_mybatis</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
detail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerName(); %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="prama" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="style.css">
-->
</head>
<body>
<table width="80%" align="center">
<tr>
<td>编号</td>
<td>订单号</td>
<td>菜名</td>
<td>价格</td>
<td>数量</td>
<td>操作</td>
</tr>
<c:forEach items="${detail }" var="bean">
<tr>
<td>${bean.id }</td>
<td>${bean.orderId }</td>
<td>${bean.menu.name}</td>
<td>${bean.menu.price}</td>
<td>${bean.menu.num}</td>
<td><a href="delete.action?orderId=${bean.orderId }&detailId=${bean.id}">删除${detail.menu.name}</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerName(); %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="prama" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="style.css">
-->
</head>
<body>
<table width="80%" align="center">
<tr>
<td>订单号</td>
<td>桌号</td>
<td>价格</td>
<td>操作</td>
</tr>
<c:forEach items="${list }" var="bean">
<tr>
<td>${bean.id }</td>
<td>${bean.deskId }</td>
<td>${bean.totalPrice}</td>
<td><a href="detail.action?orderId=${bean.id }">详情</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Test.java
package cn.sxt.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.sxt.dao.DetailDao;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
DetailDao orderDao = (DetailDao)ac.getBean("detailDao");
System.out.println(orderDao.listByOrderId(1).size());
}
}
修改以下下图的地方