使用SprinigMVC结合SpringIOC以及Mybatis实现快递员的登录并能判断该用户是否存在,并实现操作其所接的代取单数据
时间: 2024-01-22 18:19:56 浏览: 17
首先,需要创建一个快递员登录的页面,包括用户名和密码的输入框,以及登录按钮。在SpringMVC中,可以使用Controller来处理用户的请求。
1. 创建一个CourierController类,使用@Controller注解标识该类为Controller。
```
@Controller
@RequestMapping("/courier")
public class CourierController {
//TODO
}
```
2. 在CourierController类中,创建一个login方法,使用@RequestMapping注解标识该方法为处理/login请求的方法。该方法接收一个Courier对象作为参数,包含用户名和密码信息。
```
@RequestMapping("/login")
public String login(Courier courier, Model model) {
//TODO
return "courier";
}
```
3. 在login方法中,使用Mybatis查询数据库,判断该用户是否存在。
```
Courier existCourier = courierService.findByUsernameAndPassword(courier);
if (existCourier == null) {
model.addAttribute("error", "用户名或密码错误");
return "login";
}
```
4. 如果用户存在,则跳转到代取单页面,同时将当前用户的代取单数据查询出来,显示在页面上。
```
List<Order> orderList = orderService.findByCourierId(existCourier.getId());
model.addAttribute("orderList", orderList);
return "courier";
```
5. 在代取单页面上,可以使用Thymeleaf模板引擎展示数据。
```
<table>
<tr>
<th>订单号</th>
<th>收货人</th>
<th>电话</th>
<th>地址</th>
</tr>
<tr th:each="order : ${orderList}">
<td th:text="${order.orderNumber}"></td>
<td th:text="${order.consigneeName}"></td>
<td th:text="${order.consigneePhone}"></td>
<td th:text="${order.consigneeAddress}"></td>
</tr>
</table>
```
完整的CourierController类代码如下:
```
@Controller
@RequestMapping("/courier")
public class CourierController {
@Autowired
private CourierService courierService;
@Autowired
private OrderService orderService;
@RequestMapping("/login")
public String login(Courier courier, Model model) {
Courier existCourier = courierService.findByUsernameAndPassword(courier);
if (existCourier == null) {
model.addAttribute("error", "用户名或密码错误");
return "login";
}
List<Order> orderList = orderService.findByCourierId(existCourier.getId());
model.addAttribute("orderList", orderList);
return "courier";
}
}
```