文章目錄
- 識訓地址功能實作: 查詢用戶的所有識訓地址串列
- Controller : AddressController
- Service : AddressService
- ServiceImpl : AddressServiceImpl
- Swagger2 測驗
識訓地址功能實作: 查詢用戶的所有識訓地址串列
Controller : AddressController
package com.beyond.controller;
import com.beyond.pojo.UserAddress;
import com.beyond.pojo.bo.UserAddressBO;
import com.beyond.service.AddressService;
import com.beyond.utils.BEYONDJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Api(value = "與地址相關",tags = {"地址相關的介面"})
@RestController
@RequestMapping("address")
public class AddressController {
/**
* 用戶在確認訂單頁面,可以針對識訓地址所做的操作
* 1.查詢用戶的所有識訓地址串列
*/
@Autowired
private AddressService addressService;
@ApiOperation(value = "根據用戶的id查詢識訓地址串列",notes = "根據用戶的id查詢識訓地址串列 ",httpMethod = "POST")
@PostMapping("/list")
public BEYONDJSONResult list(
@RequestParam String userId){
if (StringUtils.isBlank(userId)){
return BEYONDJSONResult.errorMsg("");
}
List<UserAddress> list = addressService.queryAll(userId);
return BEYONDJSONResult.ok(list);
}
}
Service : AddressService
package com.beyond.service;
import com.beyond.pojo.UserAddress;
import com.beyond.pojo.bo.UserAddressBO;
import java.util.List;
public interface AddressService {
/**
* 根據傳進來的用戶Id 查詢購物車地址串列
* @param userId
* @return
*/
public List<UserAddress> queryAll(String userId);
/**
* 用戶新增地址
* @param userAddressBO
*/
public void addNewUserAddress(UserAddressBO userAddressBO);
}
ServiceImpl : AddressServiceImpl
package com.beyond.service.impl;
import com.beyond.mapper.UserAddressMapper;
import com.beyond.pojo.UserAddress;
import com.beyond.pojo.bo.UserAddressBO;
import com.beyond.service.AddressService;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@SuppressWarnings("ALL")
@Service
public class AddressServiceImpl implements AddressService {
@Autowired
private UserAddressMapper userAddressMapper;
@Autowired
private Sid sid;
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public List<UserAddress> queryAll(String userId) {
UserAddress userAddress = new UserAddress();
userAddress.setUserId(userId);
return userAddressMapper.select(userAddress);
}
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public void addNewUserAddress(UserAddressBO userAddressBO) {
// 1.需要判斷,當前的用戶是后存在地址,如果不存在則進行新增地址,新增地址為默認地址
Integer isDefault = 0;
List<UserAddress> addressList = this.queryAll(userAddressBO.getUserId());
if (addressList == null || addressList.isEmpty() || addressList.size() == 0){
isDefault = 1;
}
String newAddressId = sid.nextShort();
// 2.保存資料到資料庫
UserAddress newAddress = new UserAddress();
BeanUtils.copyProperties(userAddressBO, newAddress);
newAddress.setId(newAddressId);
newAddress.setIsDefault(isDefault);
newAddress.setCreatedTime(new Date());
newAddress.setUpdatedTime(new Date());
userAddressMapper.insert(newAddress);
}
}
Swagger2 測驗

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241314.html
標籤:java
