开发者社区> 学堂小助手> 正文

案例_点菜| 学习笔记

简介: 快速学习案例_点菜
+关注继续查看

开发者学堂课程【Java Web 开发系列课程:Spring 框架入门案例_点菜】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址/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( ));

}?

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
轻量级RPC框架(0 - 项目概述)
轻量级RPC框架(0 - 项目概述)
4 0
python中的迭代对象、迭代器、生成器
在python中对于list和tuple,有很多的方式得到里面的元素,如果要输出里面的所有元素,可以采用for循环的形式,得到list或tuple中的每个元素,这就是迭代,凡是能够进行for循环的对象就是可迭代对象。 在java中的迭代方式有两种,一种直接for循环,一种采用迭代器 for循环的使用:
5 0
算法必知 --- 归并排序(优化与案例)
算法必知 --- 归并排序(优化与案例)
4 0
1783
文章
0
问答
来源圈子
更多
技术图谱:由专家组参与技术图谱的绘制与编写,知识与实践的结合让开发者们掌握学习路线与逻辑,快速提升技能 电子书:电子书由阿里内外专家打造,供开发者们下载学习,更与课程相结合,使用户更易理解掌握课程内容 训练营:学习训练营 深入浅出,专家授课,带领开发者们快速上云 精品课程:汇集知识碎片,解决技术难题,体系化学习场景,深入浅出,易于理解 技能自测:提供免费测试,摸底自查 体验实验室:学完即练,云资源免费使用
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
OceanBase 入门到实战教程
立即下载
阿里云图数据库GDB,加速开启“图智”未来.ppt
立即下载
实时数仓Hologres技术实战一本通2.0版(下)
立即下载
http://www.vxiaotou.com