注:查分两种
1 .查询所有信息返回
2.根据id查询信息返回当前order元组信息
package com.tedu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tedu.pojo.Order;
@Controller
public class OrderController {
/**1.查询所有订单信息*/
@RequestMapping("/orderList")
public String orderList(){
return"order_list";
}
/**2.根据id删除订单信息,所以要添加一个参数接受id*/
@RequestMapping("/orderDelete")
public String orderDelete(Integer id){
//删除完后跳转到查询所有订单的方法,显示最新的订单
return "forward:/orderList";
}
/**3.新增订单信息*/
@RequestMapping("/orderAdd")
public String orderAdd(Order order){
//添加完后跳转到查询所有订单的方法,显示最新的订单
return "forward:/orderList";
}
/**4.根据id查询订单信息,查询时为了修改,回显查询信息*/
@RequestMapping("/orderInfo")
public String orderInfo(){
//将订单信息带到订单修改jsp页面 ,进行数据的回显
return "order_update";
}
/**5.根据id修改订单信息*/
@RequestMapping("/orderUpdate")
public String orderUpdate(Order order){
//修改完后跳转到查询所有订单的方法,显示最新的订单
return "forward:/orderList";
}
}
偷懒方法,借用上个Door模块
复制DoorMapper文件,改名为OrderMapper
选择’门店’ Ctrl+F 替换为订单
选择"Door" Ctrl+F替换为Order 选择Case senstive区分大小写
选择"door" Ctrl+F替换为order
package com.tedu.dao;
import java.util.List;
import com.tedu.pojo.Order;
/**
* OrderMapper接口
* 声明增删改查方法,对订单信息进行操作
*/
public interface OrderMapper {
/** 1.查询所有订单信息*/
public List<Order> findAll();
/** 2.根据id删除信息*/
public void deleteById(Integer id);
/** 3.根据id删除信息*/
public void add(Order order);
/** 4.根据id查询订单信息*/
public Order findById(Integer id);
/** 5.根据id修改订单信息*/
public void updateById(Order order);
}
<?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">
<!-- 订单表的映射文件 namespace值为对应接口的全路径 -->
<mapper namespace="com.tedu.dao.OrderMapper">
<!--
resultMap 是为了解决 当数据表中的列名和
pojo类中的属性名不一致的问题
resultMap 中配置了 数据表中的列 和 pojo类
中的属性 之间的对应关系 (也就是映射)
-->
<resultMap type="com.tedu.pojo.Order"
id="orderRM">
<id column="id" property="id"/>
<result column="door_id" property="doorId" />
<result column="order_no" property="orderNo"/>
<result column="order_type" property="orderType"/>
<result column="pnum" property="pnum"/>
<result column="cashier" property="cashier"/>
<result column="order_time" property="orderTime"/>
<result column="pay_time" property="payTime"/>
<result column="pay_type" property="payType"/>
<result column="price" property="price"/>
</resultMap>
<!-- 1.查询所有订单
tb_order表中的列和Order类中的属性名
不一致会导致数据封装失败!!!
id id setId()
door_id doorId setDoorId()
如果表中的列和pojo类中的属性名不一致
这里需要将resultType改为resultMap
-->
<select id="findAll" resultMap="orderRM">
select * from tb_order
</select>
<!--2.根据id删除订单信息 -->
<delete id="deleteById">
delete from tb_order where id=#{id}
</delete>
<!--3.添加订单信息 -->
<insert id="add">
insert into tb_order values(null,#{door_id},#{order_no},#{order_type},#{#pnum},#{cashier},
#{order_time},#{pay_time},#{pay_type},#{price})
</insert>
<!--4.根据id查询信息 -->
<select id="findById" resultType="com.tedu.pojo.Order">
select * from tb_order where id = #{id}
</select>
<!--5.根据id修改订单信息,id值等于方法名 -->
<update id="updateById">
update tb_order set values(null,#{door_id},#{order_no},#{order_type},#{#pnum},#{cashier},
#{order_time},#{pay_time},#{pay_type},#{price}
where id=#{id}
</update>
</mapper>
package com.tedu.pojo;
import java.util.Date;
/**
* 订单信息类, 用于封装订单信息
*/
//数据库用_连接两个单词,java用驼峰法.如何让两种命名规范都满足.
public class Order {
//1.声明属性
private Integer id; //id
private Integer doorId; //door_id
private String orderNo; //order_no
private String orderType;//order_type
private Integer pnum; //pnum
private String cashier; //carshier
private Date orderTime; //order_time
private Date payTime; //pay_time
private String payType; //pay_type
private Double price; //price
//2.提供getter和setter方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getDoorId() {
return doorId;
}
public void setDoorId(Integer doorId) {
this.doorId = doorId;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getOrderType() {
return orderType;
}
public void setOrderType(String orderType) {
this.orderType = orderType;
}
public Integer getPnum() {
return pnum;
}
public void setPnum(Integer pnum) {
this.pnum = pnum;
}
public String getCashier() {
return cashier;
}
public void setCashier(String cashier) {
this.cashier = cashier;
}
public Date getOrderTime() {
return orderTime;
}
public void setOrderTime(Date orderTime) {
this.orderTime = orderTime;
}
public Date getPayTime() {
return payTime;
}
public void setPayTime(Date payTime) {
this.payTime = payTime;
}
public String getPayType() {
return payType;
}
public void setPayType(String payType) {
this.payType = payType;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
//3.重写toString
@Override
public String toString() {
return "Order [id=" + id + ", doorId=" + doorId + ", orderNo=" + orderNo + ", orderType=" + orderType
+ ", pnum=" + pnum + ", cashier=" + cashier + ", orderTime=" + orderTime + ", payTime=" + payTime
+ ", payType=" + payType + ", price=" + price + "]";
}
}
注:
涉及door和order的外键约束,查询数据时需要连接两个基本表
"${ door.id==order.doorId }"
<%@ page pageEncoding="utf-8"%>
<%-- 引入JSTL标签库 --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE HTML>
<html>
<head>
<title>订单管理</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
body {
font-family: "微软雅黑";
background-color: #EDEDED;
}
h2 {
text-align: center;
}
table {
width: 96%;
margin: 0 auto;
text-align: center;
border-collapse: collapse;
font-size: 16px;
}
td, th {
padding: 5px;
}
th {
background-color: #DCDCDC;
width: 120px;
}
th.width-40 {
width: 40px;
}
th.width-50 {
width: 50px;
}
th.width-64 {
width: 64px;
}
th.width-80 {
width: 80px;
}
th.width-120 {
width: 100px;
}
hr {
margin-bottom: 20px;
border: 1px solid #aaa;
}
#add-order {
text-align: center;
font-size: 20px;
}
</style>
<script type="text/javascript">
</script>
</head>
<body>
<h2>订单管理</h2>
<div id="add-order">
<a href="findAllDoorToOrder_add" target="rightFrame">新增订单</a>
</div>
<hr />
<table border="1">
<tr>
<th class="width-40">序号</th>
<th class="width-120">所属门店</th>
<th class="width-50">订单号</th>
<th class="width-40">类型</th>
<th class="width-40">人数</th>
<th class="width-50">收银员</th>
<th class="width-120">下单时间</th>
<th class="width-120">结账时间</th>
<th class="width-50">支付方式</th>
<th class="width-50">金额</th>
<th class="width-80">操 作</th>
</tr>
<!-- 模版数据 -->
<c:forEach items="${ list }" var="order" varStatus="status">
<tr>
<td>${ status.count }</td>
<!-- 显示订单所属性门店名称 -->
<c:forEach items="${doorList}" var="door">
<c:if test="${ door.id==order.doorId }">
<!--连接两张表 -->
<td class="1111">${ door.name }</td>
</c:if>
</c:forEach>
<td>${ order.orderNo }</td>
<script>
//获取后端设置的值
var orderNo = "${ order.orderNo }";
console.log(orderNo);
</script>
<td>${ order.orderType }</td>
<td>${ order.pnum }</td>
<td>${ order.cashier }</td>
<td><fmt:formatDate value="${ order.orderTime }"
pattern="yyyy-MM-dd HH:mm:ss" /></td>
<td><fmt:formatDate value="${ order.payTime }"
pattern="yyyy-MM-dd HH:mm:ss" /></td>
<td>${ order.payType }</td>
<td>${ order.price }</td>
<td><a href="orderDelete?id=${ order.id }">删除</a> | <a
href="orderInfo?id=${ order.id }">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>