08 SSM整合案例(企业权限管理系统):07.订单操作

无人久伴 提交于 2020-05-02 10:34:06

04.AdminLTE的基本介绍

05.SSM整合案例的基本介绍

06.产品操作

07.订单操作

08.用户操作

09.权限控制

10.权限关联与控制

11.AOP日志

07.订单操作


 

 SSM订单操作

 表结构分析 

 

 

 productId描述了订单与产品之间的关系。
 memberid描述了订单与会员之间的关系。
 创建表sql
 

-- 订单
drop table orders;
CREATE TABLE orders(
  id varchar2(32) default SYS_GUID() PRIMARY KEY,
  orderNum VARCHAR2(20) NOT NULL UNIQUE,
  orderTime timestamp,
  peopleCount INT,
  orderDesc VARCHAR2(500),
  payType INT,
  orderStatus INT,
  productId varchar2(32),
  memberId varchar2(32),
  FOREIGN KEY (productId) REFERENCES product(id),
  FOREIGN KEY (memberId) REFERENCES member(id)
)
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
values ('0E7231DC797C486290E8713CA3C6ECCC', '12345', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '676C5BD1D35E429A8C2E114939C5685A', 'E61D65F673D54F68B0861025C69773DB');
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
values ('5DC6A48DD4E94592AE904930EA866AFA', '54321', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '676C5BD1D35E429A8C2E114939C5685A', 'E61D65F673D54F68B0861025C69773DB');
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
values ('2FF351C4AC744E2092DCF08CFD314420', '67890', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
values ('A0657832D93E4B10AE88A2D4B70B1A28', '98765', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
values ('E4DD4C45EED84870ABA83574A801083E', '11111', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
values ('96CC8BD43C734CC2ACBFF09501B4DD5D', '22222', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB');
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
values ('55F9AF582D5A4DB28FB4EC3199385762', '33333', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '9F71F01CB448476DAFB309AA6DF9497F', 'E61D65F673D54F68B0861025C69773DB');
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
values ('CA005CF1BE3C4EF68F88ABC7DF30E976', '44444', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '9F71F01CB448476DAFB309AA6DF9497F', 'E61D65F673D54F68B0861025C69773DB');
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid)
values ('3081770BC3984EF092D9E99760FDABDE', '55555', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '9F71F01CB448476DAFB309AA6DF9497F', 'E61D65F673D54F68B0861025C69773DB');

 

   

 

  创建表sql
 

-- 会员
drop table member;
CREATE TABLE member(
       id varchar2(32) default SYS_GUID() PRIMARY KEY,
       NAME VARCHAR2(20),
       nickname VARCHAR2(20),
       phoneNum VARCHAR2(20),
       email VARCHAR2(20) 
);
insert into MEMBER (id, name, nickname, phonenum, email)
values ('E61D65F673D54F68B0861025C69773DB', '张三', '小三', '18888888888', 'zs@163.com');

 

 

 创建表sql

 

-- 旅客
drop table traveller;
CREATE TABLE traveller(
  id varchar2(32) default SYS_GUID() PRIMARY KEY,
  NAME VARCHAR2(20),
  sex VARCHAR2(20),
  phoneNum VARCHAR2(20),
  credentialsType INT,
  credentialsNum VARCHAR2(50),
  travellerType INT
)
insert into TRAVELLER (id, name, sex, phonenum, credentialstype, credentialsnum, travellertype)
values ('3FE27DF2A4E44A6DBC5D0FE4651D3D3E', '张龙', '', '13333333333', 0, '123456789009876543', 0);
insert into TRAVELLER (id, name, sex, phonenum, credentialstype, credentialsnum, travellertype)
values ('EE7A71FB6945483FBF91543DBE851960', '张小龙', '', '15555555555', 0, '987654321123456789', 1);

 

 

 旅客与订单之间是多对多关系,所以我们需要一张中间表(order_traveller)来描述。

 

   

 

 

 创建表sql

-- 订单与旅客中间表
drop table order_traveller;
CREATE TABLE order_traveller(
  orderId varchar2(32),
  travellerId varchar2(32),
  PRIMARY KEY (orderId,travellerId),
  FOREIGN KEY (orderId) REFERENCES orders(id),
  FOREIGN KEY (travellerId) REFERENCES traveller(id)
)

insert into ORDER_TRAVELLER (orderid, travellerid)
values ('0E7231DC797C486290E8713CA3C6ECCC', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E');
insert into ORDER_TRAVELLER (orderid, travellerid)
values ('2FF351C4AC744E2092DCF08CFD314420', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E');
insert into ORDER_TRAVELLER (orderid, travellerid)
values ('3081770BC3984EF092D9E99760FDABDE', 'EE7A71FB6945483FBF91543DBE851960');
insert into ORDER_TRAVELLER (orderid, travellerid)
values ('55F9AF582D5A4DB28FB4EC3199385762', 'EE7A71FB6945483FBF91543DBE851960');
insert into ORDER_TRAVELLER (orderid, travellerid)
values ('5DC6A48DD4E94592AE904930EA866AFA', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E');
insert into ORDER_TRAVELLER (orderid, travellerid)
values ('96CC8BD43C734CC2ACBFF09501B4DD5D', 'EE7A71FB6945483FBF91543DBE851960');
insert into ORDER_TRAVELLER (orderid, travellerid)
values ('A0657832D93E4B10AE88A2D4B70B1A28', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E');
insert into ORDER_TRAVELLER (orderid, travellerid)
values ('CA005CF1BE3C4EF68F88ABC7DF30E976', 'EE7A71FB6945483FBF91543DBE851960');
insert into ORDER_TRAVELLER (orderid, travellerid)
values ('E4DD4C45EED84870ABA83574A801083E', 'EE7A71FB6945483FBF91543DBE851960');

 

  

 1.订单操作-查询所有订单流程分析

 

 

 2.订单操作-查询所有订单代码实现1

 在数据库中创建完所需的TABLE并且insert into 必要的数据后,我们就要写DAO层的实体类。

 多与多对应关系的中间表没有实体类。

  在子模块heima_ssm_domain下创建以下的3个实体类:

  Orders

  Member

  Traveller

  

  在子模块heima_ssm_web下创建控制类OrdersController

 

  在子模块heima_ssm_service下创建接口和实现类 IOrdersService 和 OrdersServiceImpl

 

  在子模块heima_ssm_dao下创建 IOrdersDao接口类供mybatis框架基于注解写sql语句

  

 3.订单操作-查询所有订单代码实现2

 DAO层的接口上在 @Select注解的下一行有必要声明查询的主键。

 注解@Results ,使用这个注解来实现属性映射对应关系。

 

public interface IProductDao {

    //查询所有产品
    @Select("select * from product")
    public List<Product> findAll() throws Exception;

    //增添产品
    @Insert({"insert into product(productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus)",
    "values(#{productNum},#{productName},#{cityName},#{departureTime},#{productPrice},#{productDesc},#{productStatus})"})
    public void save(Product product);

    //删除产品通过产品的ID属性值。
    @Delete("delete from PRODUCT where ID = #{id}")
    public void deleteByNum(String id);

    //通过产品ID查询产品,目的是为了订单查询的@Select使用
    @Select("select * from PRODUCT where id=#{id}")
    Product findById(String id) throws Exception;

}

 

 

public interface IOrdersDao {

    //查询所有的订单信息
    @Select("select * from ORDERS")  //oracle数据库TABLE名字不区分大小写
    @Results({    //为了网页显示的后缀Str类型的实体类属性不用对应出来
            @Result(id = true ,property ="id",column = "id"),  //声明主键id = true
            @Result(property ="orderNum",column = "orderMum"),
            @Result(property ="orderTime",column = "orderTime"),
            @Result(property ="orderStatus",column = "orderStatus"),
            @Result(property ="peopleCount",column = "peopleCount"),
            @Result(property ="payType",column = "payType"),
            @Result(property ="orderDesc",column = "orderDesc"),
            //多表关联查询返回类型的映射通过:javaType= Xxx.class
            @Result(property ="product",column = "productId",javaType = Product.class ,one =@One(select = "cn.bjut.ssm.dao.IProductDao.findById"))

    })
    public List<Orders> findAll() throws Exception;
}

 

  4.订单操作-查询所有订单代码实现3

 

 cn.bjut.ssm.domain包下的实体类Orders的成员变量

 后缀是Str的那些属性的 get方法需要我们添加if语句给返回值字符串 设定内容根据数据库表结构

//订单
public class Orders {
    private String id;
    private String orderNum;
    private Date orderTime;
    private String orderTimeStr;
    private int orderStatus;
    private int peopleCount;
    private Product product;
    private List<Traveller> travellers;
    private Member member;
    private Integer payType;
    private String payTypeStr;
    private String orderDesc;
    private String orderStatusStr;
    //============================================
    //需要添加在JSP页面上EL表达式获取的拓展字符串属性
    public String getOrderStatusStr() {
        //订单状态 (0未支付 1已支付)
        if(orderStatus ==0){
            orderStatusStr= "未支付";
        }else if (orderStatus ==1){
            orderStatusStr= "已支付";
        }
        return orderStatusStr;
    }

    public void setOrderStatusStr(String orderStatusStr) {
        this.orderStatusStr = orderStatusStr;
    }
    
    //===========================================
    public String getPayTypeStr() {
        if(payType == 0){
            payTypeStr = "支付宝";
        } else if(payType == 1){
            payTypeStr = "微信";
        } else if(payType == 2){
            payTypeStr = "其他";
        }
        return payTypeStr;
    }
    //================================================
    public void setPayTypeStr(String payTypeStr) {
        this.payTypeStr = payTypeStr;
    }
    //调用我们的自定义工具类转换一下
    public String getOrderTimeStr() {
        if(orderTime !=null) {

            orderTimeStr = DateUtils.date2String(orderTime, "yyyy-MM-dd HH:mm");
        }
        return orderTimeStr;
    }

    public void setOrderTimeStr(String orderTimeStr) {
        this.orderTimeStr = orderTimeStr;
    }
    //=================================================

 

   

 

 5.PageHelper概述与基本使用步骤介绍

 导入依赖坐标:

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
        </dependency>

在spring的applicationContext.xml管理sqlSessionFactory的<bean>标签

 

<!-- 传入PageHelper的插件 -->
           <property name="plugins">
               <array>
                   <!-- 传入插件的对象 -->
                   <bean class="com.github.pagehelper.PageInterceptor">
                       <property name="properties">
                           <props>
                               <prop key="helperDialect">mysql</prop>
                               <prop key="reasonable">true</prop>
                           </props>
                       </property>
                   </bean>
               </array>
           </property>

 

   

 package cn.bjut.ssm.service.impl;

@Service
@Transactional  //注解的方式配置spring事务管理
public class OrdersServiceImpl implements IOrdersService {

    @Autowired
    private IOrdersDao ordersDao;

    @Override  //查询所有订单
    public List<Orders> findAll() throws Exception {
        //参数pageNum是页码值,pageSize是每页显示条数
        //放在Service层真正执行SQL语句的静态方法前紧挨着
        PageHelper.startPage(1,5);
        return ordersDao.findAll();
    }
}

   

  

 package cn.bjut.ssm.controller;

 

//查询所有订单:分页插件
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1") int page ,@RequestParam(name="size",required = true,defaultValue = "5") int size) throws Exception{
        ModelAndView mv = new ModelAndView("orders-page-list2");
        List<Orders> ordersList = ordersService.findAll(page ,size);
        
        //PageInfo就是一个分页Bean
        PageInfo pageInfo =new PageInfo(ordersList);
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

 

 7.订单操作-分页查询所有订单2(页面代码处理)

 

 

 JSP实现分页的 PageInfo.pageNum 当前页

<li>
                                <a href="${pageContext.request.contextPath}/orders/findAll.do?page=1&size=${pageInfo.pageSize}" aria-label="Previous">首页</a>
                            </li>
                            <li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum-1}&size=${pageInfo.pageSize}">上一页</a></li>
                            <c:forEach  begin="1" end="${pageInfo.pages}" var="pageNum"  varStatus="s">
                                <li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageNum}&size=${pageInfo.pageSize}">${pageNum}</a></li>
                            </c:forEach>
                            <li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum+1}&size=${pageInfo.pageSize}">下一页</a></li>
                            <li>
                                <a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pages}&size=${pageInfo.pageSize}" aria-label="Next">尾页</a>
                            </li>

  浏览器向服务器请求分页信息提交的JS

function changePageSize() {
            //获取下拉框的值
            var pageSize = $("#changePageSize").val();

            //向服务器发送请求,改变每页显示条数
            location.href = "${pageContext.request.contextPath}/orders/findAll.do?page=1&size="
                    + pageSize;
        }

  下拉菜单实现分页选择size

<!-- .box-footer-->
                <div class="box-footer">
                    <div class="pull-left">
                        <div class="form-group form-inline">
                            总共${pageInfo.pages}页,共${pageInfo.total} 条数据。 每页
                            <select class="form-control" id = "changePageSize" onchange="changePageSize()">
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                            </select></div>
                    </div>

 

 8.订单操作-订单详情查询流程分析

 

 

 3.订单详情查询流程分析

  详情 button 的部分代码如下:

<button type="button" class="btn bg-olive btn-xs" onclick="location.href='${pageContext.request.contextPath}/orders/findById.do?id=${orders.id}'">详情</button>

 

package cn.bjut.ssm.dao;


import cn.bjut.ssm.domain.Traveller;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ITravellerDao {
    @Select("select * from traveller where id in ( select travellerId from order_traveller where orderId=#{ordersId})")
    public  List<Traveller> findByOrdersId(String ordersId) throws Exception;

}

 

//通过订单主键ID查询订单详情(多表关联查询)
    @Select("select * from ORDERS where id = #{ordersId}" )  //oracle数据库TABLE名不区分大小写
    @Results({    //为了网页显示的后缀Str类型的实体类属性不用对应出来
            @Result(property ="id",column = "id",id = true ),  //主键声明id = true
            @Result(property ="orderNum",column = "orderMum"),
            @Result(property ="orderTime",column = "orderTime"),
            @Result(property ="orderStatus",column = "orderStatus"),
            @Result(property ="peopleCount",column = "peopleCount"),
            @Result(property ="payType",column = "payType"),
            @Result(property ="orderDesc",column = "orderDesc"),
            //多表关联查询,声明“引用实体类”的封装通过:javaType= Xxx实体类.class
            @Result(property ="product",column = "productId",javaType = Product.class ,one =@One(select = "cn.bjut.ssm.dao.IProductDao.findById")),
            @Result(property ="member",column = "memberId",javaType = Member.class ,one =@One(select = "cn.bjut.ssm.dao.IMemberDao.findById")),
            //通过中间表查询多对多关系,返回一个其它实体类的List集合
            @Result(property = "travellers",column ="id",javaType = java.util.List.class,many = @Many(select = "cn.bjut.ssm.dao.ITravellerDao.findByOrdersId"))
    })
    public Orders findById(String ordersId)throws Exception;

 

 11.订单操作-订单详情查询代码实现3

 

package cn.bjut.ssm.domain;

//旅客
public class Traveller {
    private String id;
    private String name;
    private String sex;
    private String phoneNum;
    private Integer credentialsType; //证件类型 0身份证 1护照 2军官证
    private String credentialsTypeStr;
    private String credentialsNum;
    private Integer travellerType; //旅客类型(人群) 0 成人 1 儿童
    private String travellerTypeStr;

    //=============================================================
    public String getCredentialsTypeStr() {
        //证件类型 0身份证 1护照 2军官证
        if (credentialsType != null) {
            if (credentialsType == 0) {
                credentialsTypeStr = "身份证";
            } else if (credentialsType == 1) {
                credentialsTypeStr = "护照";
            } else if (credentialsType == 2) {
                credentialsTypeStr = "军官证";
            }
        }
        return credentialsTypeStr;
    }
    
    public String getTravellerTypeStr() {
        ////旅客类型(人群) 0 成人 1 儿童
        if (travellerType != null) {
            if (travellerType == 0) {
                travellerTypeStr = "成人";
            } else if (travellerType == 1) {
                travellerTypeStr = "儿童";
            }
        }
        return travellerTypeStr;
    }
    
    //======================================================

 

 

==================

end

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!