數據後臺管理(三)訂單管理

1.查詢全部訂單信息

1.1查看顯示訂單的前端頁面須要哪些訂單信息

根據下面的orders-list.jsp頁面的部分代碼能夠知道訂單的信息表中不只須要訂單的id,orderNum,orderTimeStr,orderStatusStr,還須要的產品的productName,productPrice。所以在查詢訂單的信息時須要將該訂單的產品信息也要查詢出來並封裝到Orders類中。前端

 1 <tbody>
 2 
 3 
 4                                     <c:forEach items="${ordersList}" var="orders">
 5 
 6                                         <tr>
 7                                             <td><input name="ids" type="checkbox"></td>
 8                                             <td>${orders.id }</td>
 9                                             <td>${orders.orderNum }</td>
10                                             <td>${orders.product.productName }</td>
11                                             <td>${orders.product.productPrice }</td>
12                                             <td>${orders.orderTimeStr }</td>
13                                             <td class="text-center">${orders.orderStatusStr }</td>
14                                             <td class="text-center">
15                                                 <button type="button" class="btn bg-olive btn-xs">訂單</button>
16                                                 <button type="button" class="btn bg-olive btn-xs" onclick="location.href='${pageContext.request.contextPath}/orders/findById.do?id=${orders.id}'">詳情</button>
17                                                 <button type="button" class="btn bg-olive btn-xs">編輯</button>
18                                             </td>
19                                         </tr>
20                                     </c:forEach>
21                                 </tbody>

1.2在domain包下新建Orders類

該類中不只包含orders表中基本的字段信息,還新增了orderTimeStr,orderStatusStr,product,travellers,member。java

因爲Date類型的沒法在前端頁面正常顯示所以新增了orderTimeStr類,在get方法中將orderTime轉換成字符串形式。因爲orders表中orderStatus只是0或1,而前端頁面須要具體的訂單狀態「關閉」或「開啓」,所以在get方法中將orderStatus的0或1轉換成orderStatusStr的「關閉」或開啓。product是根據orders表中的productId從product表中查出的,並將其封裝到該類的product中。mysql

  1 package club.nipengfei.domain;
  2 
  3 import club.nipengfei.utils.DateUtils;
  4 
  5 import java.util.Date;
  6 import java.util.List;
  7 
  8 public class Orders {
  9     private String id;
 10     private String orderNum;
 11     private Date orderTime;
 12     private String orderTimeStr;
 13     private int orderStatus;
 14     private String orderStatusStr;
 15     private int peopleCount;
 16     private Product product;
 17     private List<Traveller> travellers;
 18     private Member member;
 19     private Integer payType;
 20 
 21     public String getOrderStatusStr() {
 22         if (orderStatus == 0){
 23             orderStatusStr = "未支付";
 24         }
 25         if (orderStatus == 1){
 26             orderStatusStr = "已支付";
 27         }
 28         return orderStatusStr;
 29     }
 30 
 31     public void setOrderStatusStr(String orderStatusStr) {
 32         this.orderStatusStr = orderStatusStr;
 33     }
 34 
 35     private String payTypeStr;
 36     private String orderDesc;
 37 
 38     public String getId() {
 39         return id;
 40     }
 41 
 42     public void setId(String id) {
 43         this.id = id;
 44     }
 45 
 46     public String getOrderNum() {
 47         return orderNum;
 48     }
 49 
 50     public void setOrderNum(String orderNum) {
 51         this.orderNum = orderNum;
 52     }
 53 
 54     public Date getOrderTime() {
 55         return orderTime;
 56     }
 57 
 58     public void setOrderTime(Date orderTime) {
 59         this.orderTime = orderTime;
 60     }
 61 
 62     public String getOrderTimeStr() {
 63         if (orderTime != null){
 64             orderTimeStr = DateUtils.date2String(orderTime,"yyyy-MM-dd HH:mm");
 65         }
 66         return orderTimeStr;
 67     }
 68 
 69     public void setOrderTimeStr(String orderTimeStr) {
 70         this.orderTimeStr = orderTimeStr;
 71     }
 72 
 73     public int getOrderStatus() {
 74         return orderStatus;
 75     }
 76 
 77     public void setOrderStatus(int orderStatus) {
 78         this.orderStatus = orderStatus;
 79     }
 80 
 81     public int getPeopleCount() {
 82         return peopleCount;
 83     }
 84 
 85     public void setPeopleCount(int peopleCount) {
 86         this.peopleCount = peopleCount;
 87     }
 88 
 89     public Product getProduct() {
 90         return product;
 91     }
 92 
 93     public void setProduct(Product product) {
 94         this.product = product;
 95     }
 96 
 97     public List<Traveller> getTravellers() {
 98         return travellers;
 99     }
100 
101     public void setTravellers(List<Traveller> travellers) {
102         this.travellers = travellers;
103     }
104 
105     public Member getMember() {
106         return member;
107     }
108 
109     public void setMember(Member member) {
110         this.member = member;
111     }
112 
113     public Integer getPayType() {
114         return payType;
115     }
116 
117     public void setPayType(Integer payType) {
118         this.payType = payType;
119     }
120 
121     public String getPayTypeStr() {
122         if (payType != null){
123             if (payType == 0){
124                 payTypeStr = "支付寶";
125             }else if (payType == 1){
126                 payTypeStr = "微信";
127             }else if (payType == 2){
128                 payTypeStr = "其它";
129             }
130         }
131         return payTypeStr;
132     }
133 
134     public void setPayTypeStr(String payTypeStr) {
135         this.payTypeStr = payTypeStr;
136     }
137 
138     public String getOrderDesc() {
139         return orderDesc;
140     }
141 
142     public void setOrderDesc(String orderDesc) {
143         this.orderDesc = orderDesc;
144     }
145 }
View Code 

1.3在dao包下新建一個IOrdersDao接口,並寫一個findAll方法

因爲須要封裝product類而orders表只有productId,所以須要另寫一個方法IProductDao.findById根據productId查詢出訂單的productgit

 1 package club.nipengfei.dao;
 2 
 3 import club.nipengfei.domain.Product;
 4 import org.apache.ibatis.annotations.Insert;
 5 import org.apache.ibatis.annotations.Select;
 6 
 7 import java.util.List;
 8 
 9 public interface IProductDao {
10     /**
11      * 查詢全部產品
12      * @return
13      */
14     @Select("select * from product")
15     List<Product> findAll() throws Exception;
16 
17     @Insert("insert into product (productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus) values(#{productNum},#{productName},#{cityName},#{departureTime},#{productPrice},#{productDesc},#{productStatus})")
18     void save(Product product)throws Exception;
19 
20     @Select("select * from product where id=#{id}")
21     Product findById(int id) throws Exception;
22 }
View Code
 1 package club.nipengfei.dao;
 2 
 3 import club.nipengfei.domain.Orders;
 4 import club.nipengfei.domain.Product;
 5 import org.apache.ibatis.annotations.One;
 6 import org.apache.ibatis.annotations.Result;
 7 import org.apache.ibatis.annotations.Results;
 8 import org.apache.ibatis.annotations.Select;
 9 
10 import java.util.List;
11 
12 public interface IOrdersDao {
13 
14     @Select("select * from orders")
15     @Results({
16             @Result(id = true,property = "id",column = "id"),
17             @Result(property = "orderNum",column = "orderNum"),
18             @Result(property = "orderTime",column = "orderTime"),
19             @Result(property = "orderStatus",column = "orderStatus"),
20             @Result(property = "peopleCount",column = "peopleCount"),
21             @Result(property = "payType",column = "payType"),
22             @Result(property = "orderDesc",column = "orderDesc"),
23             @Result(property = "product",column = "productId",javaType = Product.class,one = @One(select = "club.nipengfei.dao.IProductDao.findById"))
24     })
25     List<Orders> findAll() throws Exception;
26 }
View Code

1.4在service.impl包下新建一個OrdersServiceImpl類,寫一個findAll方法

 1 package club.nipengfei.service.impl;
 2 
 3 import club.nipengfei.dao.IOrdersDao;
 4 import club.nipengfei.domain.Orders;
 5 import club.nipengfei.service.IOrdersService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 import org.springframework.transaction.annotation.Transactional;
 9 
10 import java.util.List;
11 
12 @Service
13 @Transactional
14 public class OrdersServiceImpl implements IOrdersService {
15 
16     @Autowired
17     private IOrdersDao ordersDao;
18 
19     public List<Orders> findAll() throws Exception {
20         return ordersDao.findAll();
21     }
22 }
View Code

1.5在controller包下新建一個OrdersController類,並寫一個findAll方法

因爲該方法是將從數據庫中查出的值返回給前端頁面,所以該方法返回值類型爲ModelAndView。
 1 package club.nipengfei.controller;
 2 
 3 import club.nipengfei.domain.Orders;
 4 import club.nipengfei.service.impl.OrdersServiceImpl;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.servlet.ModelAndView;
 9 
10 import java.util.List;
11 
12 @Controller
13 @RequestMapping("/orders")
14 public class OrdersController {
15 
16     @Autowired
17     private OrdersServiceImpl ordersService;
18 
19     @RequestMapping("/findAll.do")
20     public ModelAndView findAll() throws Exception {
21         ModelAndView mv = new ModelAndView();
22         List<Orders> orders = ordersService.findAll();
23         mv.addObject("ordersList",orders);
24         mv.setViewName("orders-list");
25         return mv;
26     }
27 }
View Code

 2.對訂單信息進行分頁查詢

mybatis分頁插件:PageHelper。它是國內很是優秀的一款開源的mybatis分頁插件,它支持基本主流與經常使用的數據庫,例如mysql、oracle、mariaDB、DB二、SQLite、Hsqldb等。github

 2.1引入PageHelper分頁插件的座標

1     <dependency>
2       <groupId>com.github.pagehelper</groupId>
3       <artifactId>pagehelper</artifactId>
4       <version>5.1.2</version>
5     </dependency>

2.2在MyBatis配置文件中配置PageHelper插件

在spring的核心配置文件applicationContext.xml中sqlSessionFactory中加入PageHelper插件web

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8    http://www.springframework.org/schema/beans/spring-beans.xsd
 9    http://www.springframework.org/schema/context
10    http://www.springframework.org/schema/context/spring-context.xsd
11    http://www.springframework.org/schema/aop
12    http://www.springframework.org/schema/aop/spring-aop.xsd
13    http://www.springframework.org/schema/tx
14    http://www.springframework.org/schema/tx/spring-tx.xsd">
15     <!--開啓註解掃描,但願處理service和dao,controller不須要Spring框架處理-->
16     <context:component-scan base-package="club.nipengfei">
17         <!--配置哪些註解不掃描-->
18         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
19     </context:component-scan>
20     <!--Spring整合MyBatis框架-->
21     <!--配置鏈接池-->
22     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
23         <property name="driverClass" value="com.mysql.jdbc.Driver"/>
24         <property name="jdbcUrl" value="jdbc:mysql:///ssm1"/>
25         <property name="user" value="root"/>
26         <property name="password" value="npf123"/>
27     </bean>
28     <!--配置SqlSessionFactory工廠-->
29     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
30         <property name="dataSource" ref="dataSource"/>
31         <!-- 傳入PageHelper的插件 -->
32         <property name="plugins">
33             <array>
34                 <!-- 傳入插件的對象 -->
35                 <bean class="com.github.pagehelper.PageInterceptor">
36                     <property name="properties">
37                         <props>
38                             <prop key="helperDialect">mysql</prop>
39                             <prop key="reasonable">true</prop>
40                         </props>
41                     </property>
42                 </bean>
43             </array>
44         </property>
45     </bean>
46     <!--配置AccountDao接口所在包-->
47     <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
48         <property name="basePackage" value="club.nipengfei.dao"/>
49     </bean>
50     <!--配置Spring框架聲明式事務管理-->
51     <!--配置事務管理器-->
52     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
53         <property name="dataSource" ref="dataSource" />
54     </bean>
55     <!--配置事務通知-->
56     <tx:advice id="txAdvice" transaction-manager="transactionManager">
57         <tx:attributes>
58             <tx:method name="find*" read-only="true"/>
59             <tx:method name="*" isolation="DEFAULT"/>
60         </tx:attributes>
61     </tx:advice>
62     <!--配置AOP加強-->
63     <aop:config>
64         <aop:advisor advice-ref="txAdvice" pointcut="execution(* club.nipengfei.service.impl.*ServiceImpl.*(..))"/>
65     </aop:config>
66 </beans>
View Code

2.3修改上面的OrdersServiceImpl類的findAll方法

給findAll傳入兩個參數page,size。調用PageHelper.startPage(page,size);使service的findAll方法從數據庫中查出的訂單從page條開始,總共size條。
 1 package club.nipengfei.service.impl;
 2 
 3 import club.nipengfei.dao.IOrdersDao;
 4 import club.nipengfei.domain.Orders;
 5 import club.nipengfei.service.IOrdersService;
 6 import com.github.pagehelper.PageHelper;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Service;
 9 import org.springframework.transaction.annotation.Transactional;
10 
11 import java.util.List;
12 
13 @Service
14 @Transactional
15 public class OrdersServiceImpl implements IOrdersService {
16 
17     @Autowired
18     private IOrdersDao ordersDao;
19 
20     public List<Orders> findAll(int page, int size) throws Exception {
21         // pageNum是頁碼值,pageSize是每頁顯示條數
22         PageHelper.startPage(page,size);
23         return ordersDao.findAll();
24     }
25 }
View Code

2.4修改上面的OrdersController類的findAll方法

該方法的返回值類型爲ModelAndView,參數須要使用註解@RequestParam從前端頁面接收page和size參數,並根據接收的參數調用service的findAll方法。spring

在該controller類的findAll方法中還須要將查詢出的orders集合做爲參數傳遞給PageInfo對象,將該對象傳遞給前端。sql

 1 package club.nipengfei.controller;
 2 
 3 import club.nipengfei.domain.Orders;
 4 import club.nipengfei.service.impl.OrdersServiceImpl;
 5 import com.github.pagehelper.PageInfo;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RequestParam;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import java.util.List;
13 
14 @Controller
15 @RequestMapping("/orders")
16 public class OrdersController {
17 
18     @Autowired
19     private OrdersServiceImpl ordersService;
20 
21     @RequestMapping("/findAll.do")
22     public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1") int page,@RequestParam(name = "size",required = true,defaultValue = "3") int size) throws Exception {
23         ModelAndView mv = new ModelAndView();
24         List<Orders> orders = ordersService.findAll(page, size);
25         PageInfo pageInfo = new PageInfo(orders);
26         mv.addObject("pageInfo",pageInfo);
27         mv.setViewName("orders-page-list");
28         return mv;
29     }
30 }
View Code

2.5前端分頁展現

 1 <div class="box-footer">
 2                     <div class="pull-left">
 3                         <div class="form-group form-inline">
 4                             總共2 頁,共14 條數據。 每頁
 5                             <select class="form-control" id="changePageSize" onchange="changePageSize()">
 6                                 <option>1</option>
 7                                 <option>2</option>
 8                                 <option>3</option>
 9                                 <option>4</option>
10                                 <option>5</option>
11                             </select>12                         </div>
13                     </div>
14 
15                     <div class="box-tools pull-right">
16                         <ul class="pagination">
17                             <li>
18                                 <a href="${pageContext.request.contextPath}/orders/findAll.do?page=1&size=${pageInfo.pageSize}" aria-label="Previous">首頁</a>
19                             </li>
20                             <li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum-1}&size=${pageInfo.pageSize}">上一頁</a></li>
21                             <c:forEach begin="1" end="${pageInfo.pages}" var="pageNum">
22                                 <li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageNum}&size=${pageInfo.pageSize}">${pageNum}</a></li>
23                             </c:forEach>
24 
25                             <li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum+1}&size=${pageInfo.pageSize}">下一頁</a></li>
26                             <li>
27                                 <a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pages}&size=${pageInfo.pageSize}" aria-label="Next">尾頁</a>
28                             </li>
29                         </ul>
30                     </div>
31 
32                 </div>
View Code

2.6最終顯示效果

3.訂單詳情展現

點擊訂單展現頁面的詳情按鈕時,顯示該訂單的詳情。數據庫

3.1肯定orders類中須要封裝的訂單信息

根據訂單詳情的前端頁面,能夠知道orders類中不只須要封裝訂單信息,還須要訂單中的產品信息,訂單的遊客信息,訂單的聯繫人信息。express

3.2使用MyBatis註解從表中查出相應信息,封裝到orders類中

因爲須要封裝的對象有 Product,List<Traveller>和Member,而這些沒法從orders表中直接查詢須要另寫sql語句。其中查詢Product類在第一部分的查詢全部訂單信息中已經說過。

查詢List<Traveller>涉及到中間表,三張表結構以下所示:

 

 1 package club.nipengfei.dao;
 2 
 3 import club.nipengfei.domain.Traveller;
 4 import org.apache.ibatis.annotations.Select;
 5 
 6 import java.util.List;
 7 
 8 public interface ITravellerDao {
 9 
10     @Select("SELECT * FROM traveller WHERE id IN (SELECT travellerId FROM order_traveller WHERE orderId=#{id})")
11     List<Traveller> findByOrdersId(int id) throws Exception;
12 }
ITravellerDao

查詢Member和查詢Product思路同樣,須要在IMemberDao接口寫一個findById方法

 1 package club.nipengfei.dao;
 2 
 3 import club.nipengfei.domain.Member;
 4 import org.apache.ibatis.annotations.Select;
 5 
 6 import java.util.List;
 7 
 8 public interface IMemberDao {
 9 
10     @Select("select * from member where id=#{memberId}")
11     Member findById(int memberId) throws Exception;
12 }
IMemberDao
 1 package club.nipengfei.dao;
 2 
 3 import club.nipengfei.domain.Member;
 4 import club.nipengfei.domain.Orders;
 5 import club.nipengfei.domain.Product;
 6 import org.apache.ibatis.annotations.*;
 7 
 8 import java.util.List;
 9 
10 public interface IOrdersDao {
11 
12     @Select("select * from orders")
13     @Results({
14             @Result(id = true,property = "id",column = "id"),
15             @Result(property = "orderNum",column = "orderNum"),
16             @Result(property = "orderTime",column = "orderTime"),
17             @Result(property = "orderStatus",column = "orderStatus"),
18             @Result(property = "peopleCount",column = "peopleCount"),
19             @Result(property = "payType",column = "payType"),
20             @Result(property = "orderDesc",column = "orderDesc"),
21             @Result(property = "product",column = "productId",javaType = Product.class,one = @One(select = "club.nipengfei.dao.IProductDao.findById"))
22     })
23     List<Orders> findAll() throws Exception;
24 
25     @Select("select * from orders where id=#{ordersId}")
26     @Results({
27             @Result(id = true,property = "id",column = "id"),
28             @Result(property = "orderNum",column = "orderNum"),
29             @Result(property = "orderTimeStr",column = "orderTimeStr"),
30             @Result(property = "product",column = "productId",javaType = Product.class,one = @One(select = "club.nipengfei.dao.IProductDao.findById")),
31             @Result(property = "peopleCount",column = "peopleCount"),
32             @Result(property = "orderDesc",column = "orderDesc"),
33             @Result(property = "travellers",column = "id",javaType = java.util.List.class, many = @Many(select = "club.nipengfei.dao.ITravellerDao.findByOrdersId")),
34             @Result(property = "member",column = "memberId",javaType = Member.class,one = @One(select = "club.nipengfei.dao.IMemberDao.findById")),
35             @Result(property = "payType",column = "payType")
36     })
37     Orders findById(int ordersId);
38 }
IOrdersDao

 3.3在OrdersServiceImpl類下寫一個findById方法傳入參數id

 1 package club.nipengfei.service.impl;
 2 
 3 import club.nipengfei.dao.IOrdersDao;
 4 import club.nipengfei.domain.Orders;
 5 import club.nipengfei.service.IOrdersService;
 6 import com.github.pagehelper.PageHelper;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Service;
 9 import org.springframework.transaction.annotation.Transactional;
10 
11 import java.util.List;
12 
13 @Service
14 @Transactional
15 public class OrdersServiceImpl implements IOrdersService {
16 
17     @Autowired
18     private IOrdersDao ordersDao;
19 
20     public List<Orders> findAll(int page, int size) throws Exception {
21         // pageNum是頁碼值,pageSize是每頁顯示條數
22         PageHelper.startPage(page,size);
23         return ordersDao.findAll();
24     }
25 
26     /**
27      * 查看訂單詳情
28      * @param id
29      * @return
30      * @throws Exception
31      */
32     public Orders findById(int id) throws Exception {
33         return ordersDao.findById(id);
34     }
35 }
View Code

3.4在OrdersController類下寫一個findById方法

該方法的參數須要用註解@RequestParam從前端接收參數。查看點擊訂單詳情的路徑,在該方法的上加上註解@RequestMapping("/findById.do")

 1 package club.nipengfei.controller;
 2 
 3 import club.nipengfei.domain.Orders;
 4 import club.nipengfei.service.impl.OrdersServiceImpl;
 5 import com.github.pagehelper.PageInfo;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RequestParam;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import java.util.List;
13 
14 @Controller
15 @RequestMapping("/orders")
16 public class OrdersController {
17 
18     @Autowired
19     private OrdersServiceImpl ordersService;
20 
21     @RequestMapping("/findAll.do")
22     public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1") int page,@RequestParam(name = "size",required = true,defaultValue = "3") int size) throws Exception {
23         ModelAndView mv = new ModelAndView();
24         List<Orders> orders = ordersService.findAll(page, size);
25         PageInfo pageInfo = new PageInfo(orders);
26         mv.addObject("pageInfo",pageInfo);
27         mv.setViewName("orders-page-list");
28         return mv;
29     }
30 
31     @RequestMapping("/findById.do")
32     public ModelAndView findById(@RequestParam(name = "id", required = true) int ordersId) throws Exception {
33         ModelAndView mv = new ModelAndView();
34         Orders orders = ordersService.findById(ordersId);
35         mv.addObject("orders",orders);
36         mv.setViewName("orders-show");
37         return mv;
38     }
39 }
View Code

 

 3.5最終效果

相關文章
相關標籤/搜索