在 foodie-dev-pojo 模块的 com.imooc.pojo.bo 包内创建 AddressBO 类

    1. package com.imooc.pojo.bo;
    2. /**
    3. * @author 92578
    4. * @since 1.0
    5. */
    6. public class AddressBO {
    7. private String addressId;
    8. private String userId;
    9. private String receiver;
    10. private String mobile;
    11. private String province;
    12. private String city;
    13. private String district;
    14. private String detail;
    15. public String getAddressId() {
    16. return addressId;
    17. }
    18. public void setAddressId(String addressId) {
    19. this.addressId = addressId;
    20. }
    21. public String getUserId() {
    22. return userId;
    23. }
    24. public void setUserId(String userId) {
    25. this.userId = userId;
    26. }
    27. public String getReceiver() {
    28. return receiver;
    29. }
    30. public void setReceiver(String receiver) {
    31. this.receiver = receiver;
    32. }
    33. public String getMobile() {
    34. return mobile;
    35. }
    36. public void setMobile(String mobile) {
    37. this.mobile = mobile;
    38. }
    39. public String getProvince() {
    40. return province;
    41. }
    42. public void setProvince(String province) {
    43. this.province = province;
    44. }
    45. public String getCity() {
    46. return city;
    47. }
    48. public void setCity(String city) {
    49. this.city = city;
    50. }
    51. public String getDistrict() {
    52. return district;
    53. }
    54. public void setDistrict(String district) {
    55. this.district = district;
    56. }
    57. public String getDetail() {
    58. return detail;
    59. }
    60. public void setDetail(String detail) {
    61. this.detail = detail;
    62. }
    63. }

    在 foodie-dev-service 模块内完善 AddressService 接口,新增 addNewUserAddress 方法

    1. package com.imooc.service;
    2. import com.imooc.pojo.UserAddress;
    3. import com.imooc.pojo.bo.AddressBO;
    4. import java.util.List;
    5. /**
    6. * Created by 92578 on 2020/8/22 16:17
    7. **/
    8. public interface AddressService {
    9. /**
    10. * 根据用户 id 查询用户的收获地址列表
    11. *
    12. * @param userId
    13. * @return
    14. */
    15. public List<UserAddress> queryAll(String userId);
    16. /**
    17. * 用户新增地址
    18. *
    19. * @param addressBO
    20. */
    21. public void addNewUserAddress(AddressBO addressBO);
    22. }

    在 foodie-dev-service 模块内完善 AddressServiceImpl 类,实现 addNewUserAddress 方法

    1. package com.imooc.service.impl;
    2. import com.imooc.mapper.UserAddressMapper;
    3. import com.imooc.pojo.UserAddress;
    4. import com.imooc.pojo.bo.AddressBO;
    5. import com.imooc.service.AddressService;
    6. import org.n3r.idworker.Sid;
    7. import org.springframework.beans.BeanUtils;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.stereotype.Service;
    10. import org.springframework.transaction.annotation.Propagation;
    11. import org.springframework.transaction.annotation.Transactional;
    12. import java.util.Date;
    13. import java.util.List;
    14. /**
    15. * Created by 92578 on 2020/8/22 16:18
    16. **/
    17. @Service
    18. public class AddressServiceImpl implements AddressService {
    19. @Autowired
    20. private UserAddressMapper userAddressMapper;
    21. @Autowired
    22. private Sid sid;
    23. /**
    24. * 根据用户 id 查询用户的收获地址列表
    25. *
    26. * @param userId
    27. * @return
    28. */
    29. @Transactional(propagation = Propagation.SUPPORTS)
    30. @Override
    31. public List<UserAddress> queryAll(String userId) {
    32. UserAddress ua = new UserAddress();
    33. ua.setUserId(userId);
    34. return userAddressMapper.select(ua);
    35. }
    36. /**
    37. * 用户新增地址
    38. *
    39. * @param addressBO
    40. */
    41. @Transactional(propagation = Propagation.REQUIRED)
    42. @Override
    43. public void addNewUserAddress(AddressBO addressBO) {
    44. // 1. 判断当前用户是否存在地址,如果没有,则新增为“默认地址”
    45. Integer isDefault = 0;
    46. List<UserAddress> addressList = this.queryAll(addressBO.getUserId());
    47. if (addressList == null || addressList.isEmpty()) {
    48. isDefault = 1;
    49. }
    50. String addressId = sid.nextShort();
    51. // 2. 保存地址到数据库
    52. UserAddress newAddress = new UserAddress();
    53. BeanUtils.copyProperties(addressBO, newAddress);
    54. newAddress.setId(addressId);
    55. newAddress.setIsDefault(isDefault);
    56. newAddress.setCreatedTime(new Date());
    57. newAddress.setUpdatedTime(new Date());
    58. userAddressMapper.insert(newAddress);
    59. }
    60. }

    在 foodie-dev-common 模块的 com.imooc.utils 包内新增 MobileEmailUtils 类

    package com.imooc.utils;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class MobileEmailUtils {
    
        public static boolean checkMobileIsOk(String mobile) {
            String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(mobile);
            boolean isMatch = m.matches();
            return isMatch;
        }
    
        public static boolean checkEmailIsOk(String email) {
            boolean isMatch = true;
            if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
                isMatch = false;
            }
            return isMatch;
        }
    }
    

    在 foodie-dev-api 模块下完善 AddressController 类,新增 add 方法

    package com.imooc.controller;
    
    import com.imooc.pojo.UserAddress;
    import com.imooc.pojo.bo.AddressBO;
    import com.imooc.service.AddressService;
    import com.imooc.utils.IMOOCJSONResult;
    import com.imooc.utils.MobileEmailUtils;
    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.*;
    
    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);
        }
    
        @ApiOperation(value = "用户新增地址", notes = "用户新增地址", httpMethod = "POST")
        @PostMapping("/add")
        public IMOOCJSONResult add(@RequestBody AddressBO addressBO) {
            IMOOCJSONResult checkRes = checkAddress(addressBO);
            if (checkRes.getStatus() != 200) {
                return checkRes;
            }
    
            addressService.addNewUserAddress(addressBO);
    
            return IMOOCJSONResult.ok();
        }
    
        private IMOOCJSONResult checkAddress(AddressBO addressBO) {
            String receiver = addressBO.getReceiver();
            if (StringUtils.isBlank(receiver)) {
                return IMOOCJSONResult.errorMsg("收货人不能为空");
            }
            if (receiver.length() > 12) {
                return IMOOCJSONResult.errorMsg("收货人姓名不能太长");
            }
    
            String mobile = addressBO.getMobile();
            if (StringUtils.isBlank(mobile)) {
                return IMOOCJSONResult.errorMsg("收货人手机号不能为空");
            }
            if (mobile.length() != 11) {
                return IMOOCJSONResult.errorMsg("收货人手机号长度不正确");
            }
            boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
            if (!isMobileOk) {
                return IMOOCJSONResult.errorMsg("收货人手机号格式不正确");
            }
    
            String province = addressBO.getProvince();
            String city = addressBO.getCity();
            String district = addressBO.getDistrict();
            String detail = addressBO.getDetail();
            if (StringUtils.isBlank(province) || StringUtils.isBlank(city) || StringUtils.isBlank(district) || StringUtils.isBlank(detail)) {
                return IMOOCJSONResult.errorMsg("收货地址信息不能为空");
            }
    
            return IMOOCJSONResult.ok();
        }
    }
    

    启动项目,打开浏览器,访问 http://localhost:8080/foodie-shop/index.html
    登录账户“imooc”,密码“123123”,随便添加某商品到购物车,打开购物车界面后进行结算,点击“使用新地址”,输入内容后进行保存
    image.png
    打开数据库,找到 user_address 表,地址信息已经保存入库
    image.png
    再新建一个地址,保存后发现这次不是默认地址
    image.png
    打开数据库,发现 is_default 字段保存的内容不同
    image.png