开发者学堂课程【Java Web 开发系列课程:Spring 框架入门:案例_点菜】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/538/detail/7357
案例_点菜
内容介绍:
一、 创建 JavaEE Web Project: order
二、 配置文件
三、 创建 class
四、 创建 interface
五、 编辑拷贝页面
六、 测试
七、 运行结果
一、 创建 JavaEE Web Project: order
1) 导入的 jar 包:
aopalliance.jar
asm-3.3.1.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
aspectiweaver.jar
cglib-2.2.2.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.19.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.20-bin.jar
ognl-3.0.5.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
struts2-core-2.3.4.jar
struts2-spring-plugin-2.3.4.jar
xwork-core-2.3.4.jar
2)
导入配置文件:
applicationContext.xml
mybatis.cfg.xml
struts.xml
3)
运行 SQL文件
4)
配置数据库
二、 配置文件
1) order.mapper.xml
PUBLIC "- // mybatis.org//DTD Mapper 3.e//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select *from t_order
pdate t_order set deskId=#{deskId},
totalPrice=#{totalPrice} where id=#{id}
select *from t_order where id=#{id}
2) 配置 detail.mapper.xml
resultMap="DetailMenu">select * from t_orderdetail where orderId=#{orderId}
javaType="Menu" select="cn.sxt.vo.menu.mapper.getById">
delete from t_orderdetail where id=#{id}
3) 配置 menu.mapper.xml
select * from t_menu where id=#{id}
4) 配置 mybatis.cfg.xml
5) 配置application
6) 配置 structs
/list.jsp< / result>
name="detail" class="orderAction" method="detail"
/detail.jsp
name= "delete" class="orderAction" method="delete">
list
三、 创建 class
1) Menu
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;
}
}
2) Order
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;
}
}
3) Detail
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;
}
}
4) OrderDaoImpl
import java.util.List;
import org.apache.ibatis.session.sqlSessionFactory;
import org.mybatis.spring.support.SqlsessionDaoSupport;
import org.mybatis.spring.support.sqlSessionDaosupport;
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 ) ;
}
@Override
public List list() {
return getSqlSession().selectList("cn.sxt.vo.order.mapper.list");
}
@Override
public int update(Order order) {
return getSqlSession().update("cn.sxt.vo.order.mapper.update", order);
}
@Override
public Order getById(int id) { return getSqlSession().selectOne("cn.sxt.vo.order.mapper.getById",id);
}
}
5) DetailDaoImpl
import java.util.List;
import org.apache.ibatis.session.sqlSessionFactory;
import org.mybatis.spring.support.SqlsessionDaoSupport;
import org.mybatis.spring.support.sqlSessionDaosupport;
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 ) ;
}
@Override
public int delete( int id) {
return getSqlsession( ).delete("cn.sxt.vo.detail.mapper.delete", id);
}
@Override
public List listByorderId(int id) { return getSqlsession( ).selectList("cn.sxt.vo.detail.mapper.listByOrderId", id);
}
}
6) OrderSerxiceImpl
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.sxt.dao.OrderDao;
import cn.sxt.service.orderService;
@Service( "orderservice" )
public class OrderSerxiceImpl implements orderService{
@Autowired
private OrderDao orderDao;
@Override
public List list( ) {
return orderDao.list( );
}
@Override
public int update(Order order) i
return orderDao. update(order);
}
}
7) DetailServiceImpl
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;
@service( "detailservice" )
public class DetailServiceImpl implements DetailService{
@Autowired
private DetailDao detailDao;
@Autowired
private OrderDao orderDao;
@Override
public int delete( int orderId,int detailId){ if(detailDao.delete(detailId)>0){
order order = orderDao.getById( orderId);
List detail = detailDao.listByorderId(orderId);
double totalPrice =calcuPrice( detail );
order.setTotalPrice(totalPrice);
if(orderDao.update(order)>0){
return 1;
}
}
return 0;
//throw new Exeption();
}
@Override
public List listByorderId (int id) {
return detailDao. listByOrderId(id);
}
private double calcuPnice(Li.st..Detail.list){
double sum=e;
for( Detail d: list) {
sum+=d.getMenu( ).getPrice( )*d.getNum( );
}
return sum;
}
}
8) OrderAction
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import cn.sxt.service.orderService;
import cn.sxt.service. detailservice;
import cn.sxt.vo.Order;
@Controller
@Scope(“prototype”)
public class OrderAction {
private List list;
private List
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 details() {
details = detailsService.listByOrderId(orderId);
return "success";
}
//删除订单详情中的一项
public string delete(){
detailService.delete(orderid, detailId);
return "success";l
}
public List getList( ) {
return list;
}
public void setList( List list) {
this.list = list;
}
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 List getDetails( ) {
return details;
}
public void setDetails(List details) {
this.details = details ;
}
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;
}
四、 创建 interface
1) OrderDao
import java.util.List;
import cn.sxt.vo.Order;
public interface OrderDao {
public List list( );
public int update(Order order);
public Order getById(int id);
}
2) DetailDao
import java.util.List;
import cn.sxt.vo.Detail;
public interface DetailDao {
public List listByorderId(int id);
public int delete(int id);
}
3) OrderService
import java.util.List;
import cn.sxt.vo.Order;
public interface OrderService {
public List list( );
public int update(Order order);
}
4) Detailservice
import java.util.List;.
public interface Detailservice {
public List listByOrderId(int id);
public int delete(int orderId,int detailId);
}
五、 编辑拷贝页面
1) list.jsp
订单号
桌号
价格
操作
${bean.id }
${bean.deskId }
${bean.totalPrice }I
2) detail.jsp
订单信息
六、 测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.classPathXml pplicationContext;
import cn.sxt.dao.DetailDao;
public class Test {
public static void main( String[ ] args ) {
ApplicationContext ac =
New
classPathXm1Applicationcontext(“ApplicationContext.xml”
DetailDao orderDao =
(DetailDao)ac.getBean( "detailDao" );
System.out.println(orderDao.listByOrderId(1).size( ));
}