文章目录
- 前言
- 一、产品原型
- 二、数据库设计
- 三、接口设计、代码实现
-
- [1. 新增地址](#1. 新增地址)
- [2. 查询登录用户所有地址](#2. 查询登录用户所有地址)
- [3. 查询默认地址](#3. 查询默认地址)
- [4. 修改地址](#4. 修改地址)
- [5. 根据id删除地址](#5. 根据id删除地址)
- [6. 根据id查询地址](#6. 根据id查询地址)
- [7. 设置默认地址](#7. 设置默认地址)
前言
这部分主要是对用户端中地址簿的一些增删改查操作,业务逻辑比较简单,但是要注意一些细节,比如设置默认地址的时候,可以先把所以地址设置为非默认地址,再设置其中一个为默认地址(修改)。
一、产品原型
查询地址列表
新增地址
修改地址
删除地址
设置默认地址
查询默认地址
二、数据库设计
三、接口设计、代码实现
1. 新增地址
1、在AdressBookController中编写add方法新增地址
java
@PostMapping
@ApiOperation("新增地址")
public Result add(@RequestBody AddressBook addressBook){
addressBookService.addAddressBook(addressBook);
return Result.success();
}
2、在AdressBookService中定义addAddressBook方法,在AdressBookServiceImpl中实现方法
java
public void addAddressBook(AddressBook addressBook) {
addressBook.setUserId(BaseContext.getCurrentId());
addressBook.setIsDefault(0);
addressBookMapper.addAddressBook(addressBook);
}
3、在addAddressMapper中定义addAddressBook方法,在addAddressMapper.xml文件中编写sql语句,实现新增地址
xml
<insert id="addAddressBook" useGeneratedKeys="true" keyProperty="id">
insert into sky_take_out.address_book (user_id, consignee, sex, phone, province_code, province_name, city_code,
city_name, district_code, district_name, detail, label,is_default)
values
(#{userId}, #{consignee}, #{sex}, #{phone}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}, #{districtCode},
#{districtName}, #{detail}, #{label}, #{isDefault})
</insert>
2. 查询登录用户所有地址
1、在AdressBookController中编写list方法查询地址
java
@GetMapping("/list")
@ApiOperation("查询登录用户所有地址")
public Result<List<AddressBook>> list(){
AddressBook addressBook = new AddressBook();
addressBook.setUserId(BaseContext.getCurrentId());
List<AddressBook> addressBooks = addressBookService.list(addressBook);
return Result.success(addressBooks);
}
2、在AdressBookService中定义addAddressBook方法,在AdressBookServiceImpl中实现方法
java
public List<AddressBook> list(AddressBook addressBook) {
return addressBookMapper.list(addressBook);
}
3、在addAddressMapper中定义list方法根据条件查询地址,在addAddressMapper.xml文件中编写sql语句
xml
<select id="list" parameterType="AddressBook" resultType="AddressBook">
select * from sky_take_out.address_book
<where>
<if test="userId != null">and user_id = #{userId}</if>
<if test="phone != null">and phone = #{phone}</if>
<if test="isDefault != null">and is_default = #{isDefault}</if>
</where>
</select>
3. 查询默认地址
1、在AdressBookController中编写getDefault方法查询地址
java
@GetMapping("default")
@ApiOperation("查询默认地址")
public Result<AddressBook> getDefault() {
//SQL:select * from address_book where user_id = ? and is_default = 1
AddressBook addressBook = new AddressBook();
addressBook.setIsDefault(1);
addressBook.setUserId(BaseContext.getCurrentId());
List<AddressBook> list = addressBookService.list(addressBook);
if (list != null && list.size() == 1) {
return Result.success(list.get(0)); //默认地址只会有一个
}
return Result.error("没有查询到默认地址");
}
2、list方法在第2部分查询登录用户所有地址已实现
4. 修改地址
1、在AdressBookController中编写update方法根据id修改地址
java
@PutMapping
@ApiOperation("根据id修改地址")
public Result update(@RequestBody AddressBook addressBook){
addressBookService.update(addressBook);
return Result.success();
}
2、在AdressBookService中定义update方法,在AdressBookServiceImpl中实现方法
java
public void update(AddressBook addressBook) {
addressBookMapper.update(addressBook);
}
3、在addAddressMapper中定义update方法根据id修改地址,在addAddressMapper.xml文件中编写sql语句
xml
<update id="update">
update sky_take_out.address_book
<set>
<if test="consignee != null">consignee = #{consignee},</if>
<if test="sex != null">sex = #{sex},</if>
<if test="phone != null">phone = #{phone},</if>
<if test="detail != null">detail = #{detail},</if>
<if test="label != null">label = #{label},</if>
<if test="isDefault != null">is_default = #{isDefault},</if>
</set>
where id = #{id}
</update>
5. 根据id删除地址
1、在AdressBookController中编写deleteById方法根据id删除地址
java
@DeleteMapping
@ApiOperation("根据id删除地址")
public Result deleteById(Long id){
addressBookService.delete(id);
return Result.success();
}
2、在AdressBookService中定义delete方法,在AdressBookServiceImpl中实现方法
java
public void delete(Long id) {
addressBookMapper.delete(id);
}
3、在addAddressMapper中定义delete方法根据id删除地址
java
@Delete("delete from sky_take_out.address_book where id=#{id}")
void delete(Long id);
6. 根据id查询地址
1、在AdressBookController中编写getById方法根据id查询地址
java
@GetMapping("/{id}")
@ApiOperation("根据id查询地址")
public Result<AddressBook> getById(@PathVariable Long id){
AddressBook addressBook = addressBookService.getById(id);
return Result.success(addressBook);
}
2、在AdressBookService中定义getById方法,在AdressBookServiceImpl中实现方法
java
public AddressBook getById(Long id) {
AddressBook addressBook = addressBookMapper.getById(id);
return addressBook;
}
3、在addAddressMapper中定义delete方法根据id查询地址
java
@Select("select * from sky_take_out.address_book where id=#{id}")
AddressBook getById(Long id);
7. 设置默认地址
1、在AdressBookController中编写setDefault方法设置默认地址
java
@PutMapping("/default")
@ApiOperation("设置默认地址")
public Result setDefault(@RequestBody AddressBook addressBook){
addressBookService.setDefault(addressBook);
return Result.success();
}
2、在AdressBookService中定义getById方法,在AdressBookServiceImpl中实现方法
java
@Transactional
public void setDefault(AddressBook addressBook) {
//1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
addressBook.setIsDefault(0);
addressBook.setUserId(BaseContext.getCurrentId());
addressBookMapper.updateIsDefaultByUserId(addressBook);
//2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
addressBook.setIsDefault(1);
addressBookMapper.update(addressBook);
}
3、在addAddressMapper中定义updateIsDefaultByUserId方法将当前用户的所有地址修改为非默认地址,然后定义update方法将当前地址改为默认地址(修改地址已实现)
java
@Update("update sky_take_out.address_book set is_default = #{isDefault} where user_id = #{userId}")
void updateIsDefaultByUserId(AddressBook addressBook);