案例_点菜| 学习笔记

简介: 快速学习案例_点菜

开发者学堂课程【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( ));

}

相关文章
|
5月前
|
算法 Python
魔法图书馆大冒险 编程
【7月更文挑战第26天】
45 10
|
5月前
|
移动开发 前端开发 HTML5
零基础学前端系列教程 | 和前端谈恋爱的第005天——约会账单
零基础学前端系列教程 | 和前端谈恋爱的第005天——约会账单
32 1
|
Web App开发 开发工具 数据库
0031Java程序设计-基于B2C的网上拍卖系统—秒杀与竞价毕业设计论文
0031Java程序设计-基于B2C的网上拍卖系统—秒杀与竞价毕业设计论文
65 0
|
7月前
|
SQL 前端开发 Java
五邑大学餐厅网络点餐系统设计与实现(包含完整源码详细开发过程)
五邑大学餐厅网络点餐系统设计与实现(包含完整源码详细开发过程)
|
7月前
|
存储 文件存储 C语言
《C语言程序设计》课程设计 -- 火车票票务管理系统
《C语言程序设计》课程设计 -- 火车票票务管理系统
212 1
|
7月前
|
存储 数据可视化 安全
大学餐厅菜品推荐和点评系统设计与实现
大学餐厅菜品推荐和点评系统设计与实现
|
7月前
|
存储 SQL 关系型数据库
校园二手商品交易系统的设计与实现(论文+源码)_kaic
校园二手商品交易系统的设计与实现(论文+源码)_kaic
|
7月前
|
前端开发 测试技术 数据库
电影网上购票系统设计与实现(论文+系统)_kaic
电影网上购票系统设计与实现(论文+系统)_kaic
|
Java 应用服务中间件 数据库
电商促销后台设计,写得太好了!
电商促销后台设计,写得太好了!
314 0
电商促销后台设计,写得太好了!
|
存储 SQL 缓存
【探花交友】day04—圈子功能实现(一)
【探花交友】day04—圈子功能实现
132 0