个人名片:
博主:酒徒ᝰ.
专栏:瑞吉外卖
个人简介:沉醉在酒中,借着一股酒劲,去拼搏一个未来。
本篇励志:人会想很多事,而且多半是痛苦的,时光如水,我们无法阻止岁月的脚步,很多人,逐渐淡出了我们的视线,懂得了什么叫过客,很多事,已经不再属于我们的精彩,知道了这就叫过往。
本项目基于B站黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis plus开发核心技术的真java实战项目。
视频链接【黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis
plus开发核心技术的真java实战项目】 https://www.bilibili.com/video/BV13a411q753?
点击观看
分析:addressBook地址,GET方式,list地址
package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.IAddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
*
* 地址管理 前端控制器
*
*
* @author 酒徒
* @since 2022-09-04
*/
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
@Autowired
private IAddressBookService addressBookService;
/**
* 查询全部地址
* @param session
* @return
*/
@GetMapping("/list")
public R<List<AddressBook>> list(HttpSession session){
//根据用户id查询地址
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AddressBook::getUserId, session.getAttribute("user"));
List<AddressBook> list = addressBookService.list(queryWrapper);
return R.success(list);
}
}
分析:addressBook地址,POST方式
/**
* 添加地址
* @param addressBook
* @param session
* @return
*/
@PostMapping
public R<String> addressBook(@RequestBody AddressBook addressBook, HttpSession session){
log.info("addressBook:{}", addressBook);//addressBook:AddressBook(id=null, userId=null, consignee=长津湖, sex=1, phone=13565895624, provinceCode=null, provinceName=null, cityCode=null, cityName=null, districtCode=null, districtName=null, detail=11, label=公司, isDefault=null, createTime=null, updateTime=null, createUser=null, updateUser=null, isDeleted=null)
addressBook.setUserId((Long)session.getAttribute("user"));
//添加创建时间和修改时间
addressBook.setCreateTime(LocalDateTime.now());
addressBook.setUpdateTime(LocalDateTime.now());
//添加创建人和修改人
addressBook.setCreateUser((Long)session.getAttribute("user"));
addressBook.setUpdateUser((Long)session.getAttribute("user"));
addressBookService.save(addressBook);
return R.success("添加地址成功");
}
分析:addressBook地址,GET方式,id地址
/**
* 修改地址——回显数据
* @param id
* @return
*/
@GetMapping("/{id}")
public R<AddressBook> huixian(@PathVariable Long id){
//根据id查询地址信息
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AddressBook::getId, id);
AddressBook addressBook = addressBookService.getOne(queryWrapper);
return R.success(addressBook);
}
分析:addressBook地址,PUT方式
/**
* 修改地址——保存
* @param addressBook
* @param session
* @return
*/
@PutMapping
public R<String> addressBookupdate(@RequestBody AddressBook addressBook, HttpSession session){
//设置修改时间
addressBook.setUpdateTime(LocalDateTime.now());
//设置修改人
addressBook.setUpdateUser((Long)session.getAttribute("user"));
addressBookService.updateById(addressBook);
return R.success("修改成功");
}
分析:addressBook地址,DELETE方式,ids属性
/**
* 删除地址
* @param ids
* @param session
* @return
*/
@DeleteMapping()
public R<String> addressBook(@PathParam("ids") Long ids, HttpSession session){
//通过ids删除地址
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AddressBook::getId, ids)
.eq(AddressBook::getUserId, (Long)session.getAttribute("user"));
addressBookService.remove(queryWrapper);
return R.success("删除成功");
}
从上图分析:addressBook地址,PUT方式,default地址
/**
* 设置默认地址
* @param addressBook
* @param session
* @return
*/
@PutMapping("/default")
public R<String> defaultSet(@RequestBody AddressBook addressBook, HttpSession session){
//查询需要修改的addressBook
//log.info("addressBook:{}", addressBook);//session:AddressBook(id=1565637284313284610, userId=null, consignee=null, sex=null, phone=null, provinceCode=null, provinceName=null, cityCode=null, cityName=null, districtCode=null, districtName=null, detail=null, label=null, isDefault=0, createTime=null, updateTime=null, createUser=null, updateUser=null, isDeleted=null)
//根据用户id查询所有addressBook信息,将其中的isDefault全部改为0;
LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(AddressBook::getUserId, session.getAttribute("user"));
List<AddressBook> addressBooks = addressBookService.list();
for (AddressBook address : addressBooks) {
address.setIsDefault(0);
addressBookService.updateById(address);
}
//根据id查询addressBook信息
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AddressBook::getId, addressBook.getId());
AddressBook address = addressBookService.getOne(queryWrapper);
log.info("address:{}", address);//address:AddressBook(id=1565637284313284610, userId=1, consignee=爱丽丝, sex=0, phone=18535004237, provinceCode=null, provinceName=null, cityCode=null, cityName=null, districtCode=null, districtName=null, detail=55, label=公司, isDefault=0, createTime=2022-09-02T17:46:31, updateTime=2022-09-08T19:15:07, createUser=1, updateUser=1, isDeleted=0)
address.setIsDefault(1);
addressBookService.updateById(address);
return R.success("修改默认值成功");
}
从上图分析:addressBook地址,GET方式,default地址
注意数据库addressBook中isDefault的类型,自动生产代码是Boolean类型,需要改为int类型
/**
* 查询默认地址
* @param session
* @return
*/
@GetMapping("/default")
public R<AddressBook> defaultAddress(HttpSession session){
//查询addressBook中isDefault值 默认 0 否 1是
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AddressBook::getUserId, session.getAttribute("user"))
.eq(AddressBook::getIsDefault, 1);
AddressBook addressBook = addressBookService.getOne(queryWrapper);
log.info("addressBook:{}",addressBook);
if (addressBook == null){
return R.error("没有默认地址");
}else {
return R.success(addressBook);
}
}