在 foodie-dev-service 模块下的 com.imooc.service 包内创建 AddressService 接口
package com.imooc.service;
import com.imooc.pojo.UserAddress;
import java.util.List;
/**
* Created by 92578 on 2020/8/22 16:17
**/
public interface AddressService {
/**
* 根据用户 id 查询用户的收获地址列表
*
* @param userId
* @return
*/
public List<UserAddress> queryAll(String userId);
}
在 foodie-dev-service 模块下的 com.imooc.service.impl 包内创建 AddressServiceImpl 类,实现 AddressService
package com.imooc.service.impl;
import com.imooc.mapper.UserAddressMapper;
import com.imooc.pojo.UserAddress;
import com.imooc.service.AddressService;
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.List;
/**
* Created by 92578 on 2020/8/22 16:18
**/
@Service
public class AddressServiceImpl implements AddressService {
@Autowired
private UserAddressMapper userAddressMapper;
/**
* 根据用户 id 查询用户的收获地址列表
*
* @param userId
* @return
*/
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public List<UserAddress> queryAll(String userId) {
UserAddress ua = new UserAddress();
ua.setUserId(userId);
return userAddressMapper.select(ua);
}
}
在 foodie-dev-api 模块下的 com.imooc.controller 创建 AddressController 类,新增 list 方法
package com.imooc.controller;
import com.imooc.pojo.UserAddress;
import com.imooc.service.AddressService;
import com.imooc.utils.IMOOCJSONResult;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author 92578
* @since 1.0
*/
@Api(value = "地址相关",tags = {"地址相关的 api 接口"})
@RequestMapping("address")
@RestController
public class AddressController {
@Autowired
private AddressService addressService;
@ApiOperation(value = "根据用户 id 查询收货地址列表", notes = "根据用户 id 查询收货地址列表", httpMethod = "POST")
@PostMapping("/list")
public IMOOCJSONResult list(
@RequestParam String userId) {
if (StringUtils.isBlank(userId)) {
return IMOOCJSONResult.errorMsg("");
}
List<UserAddress> list = addressService.queryAll(userId);
return IMOOCJSONResult.ok(list);
}
}
启动项目,打开浏览器,访问 http://localhost:8088/doc.html
找到“根据用户 id 查询收货地址列表”接口,输入 userId 值“1908189H7TNWDTXP”进行调试,返回相应地址内容