mapper层编写完了,下面继续编写service层和controller层的代码。
service: 示例中没有调用所有的数据接口。
package com.example.demo.service;import com.example.demo.bean.OrderInfo;import com.example.demo.mapper.OrderMapper;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class OrderService { @Autowired OrderMapper orderMapper; public PageInfogetAllOrders(int page, int pageSize){ PageHelper.startPage(page, pageSize); List list = orderMapper.selectAllOrders(); PageInfo pageInfo = new PageInfo<>(list); return pageInfo; } public OrderInfo getOrderByID(int id){ return orderMapper.selectByPrimaryKey(id); } public int updateOrder(OrderInfo orderInfo) { return orderMapper.update(orderInfo); } public int deleteOrder(int id){ return orderMapper.deleteByPrimaryKey(id); } public PageInfo getOrderByUserId(int uid,int page,int pageSize){ PageHelper.startPage(page, pageSize); List list = orderMapper.selectByUserID(uid); PageInfo pageInfo = new PageInfo<>(list); return pageInfo; } public int updateOrderStatus(OrderInfo orderInfo){ return orderMapper.updateStatus(orderInfo); }}
controller:
1 package com.example.demo.controller; 2 3 import com.example.demo.bean.OrderInfo; 4 import com.example.demo.service.OrderService; 5 import com.github.pagehelper.PageInfo; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.*; 8 import org.springframework.web.multipart.MultipartFile; 9 10 import java.io.File;11 import java.io.IOException;12 import java.text.SimpleDateFormat;13 import java.util.Date;14 import java.util.UUID;15 16 @RestController17 @RequestMapping("/order")18 public class BasicOrderController {19 @Autowired20 OrderService orderService;21 22 @RequestMapping(value = "/all", method = RequestMethod.GET)23 public PageInfogetAllOrders(@RequestParam(value = "page", defaultValue = "1") int page,24 @RequestParam(value = "pageSize", defaultValue = "8") int pageSize) {25 return orderService.getAllOrders(page, pageSize);26 }27 28 @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)29 public OrderInfo getOrderByID(@PathVariable(value = "id", required = true) int id) {30 return orderService.getOrderByID(id);31 }32 33 @RequestMapping(value = "/user/{uid}", method = RequestMethod.GET)34 public PageInfo getOrderByUserID(@PathVariable(value = "uid", required = true) int uid,35 @RequestParam(value = "page", defaultValue = "1") int page,36 @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {37 return orderService.getOrderByUserId(uid, page, pageSize);38 }39 40 }
在浏览器中输入编写的接口地址:http://localhost:8080/order/all 可见其能正确返回数据。