获取省市区的名称

1. mapper接口
public interface DistrictMapper {
    /**
     * 根据code查询区域名称
     * @param code
     * @return
     */
    String findNameByCode(String code);
}
2. sql映射文件
<select id="findNameByCode" resultType="java.lang.String">
        select name from t_dict_district where code=#{code}
    </select>
3. 测试
@Test
    public void findNameByCode() {
        String name = districtMapper.findNameByCode("610000");
        System.out.println(name);
    }

结果应输出:陕西省

4. 完善AddressServiceImpl类➡addNewAddress()方法

findNameByCode()方法辅助addNewAddress()方法,在新增收货地址时设置省市区名称:

package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.ex.AddressServiceEx.AddressCountLimitException;
import com.cy.store.ex.UserServiceEx.InsertException;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.mapper.DistrictMapper;
import com.cy.store.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class AddressServiceImpl implements IAddressService {

    @Value("${user.address.max-count}")
    private Integer maxCount;

    @Autowired
    private AddressMapper addressMapper;
//新增代码
    @Autowired
    private DistrictMapper districtMapper;

    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        Integer count = addressMapper.countByUid(uid);
        if(count >= maxCount) {
            throw new AddressCountLimitException("收货地址数量超出限制");
        }
//新增代码:设置省市区名称
        String provinceName = districtMapper.findNameByCode(address.getProvinceCode());
        String cityName = districtMapper.findNameByCode(address.getCityCode());
        String areaName = districtMapper.findNameByCode(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);

        address.setUid(uid);
        Integer isDefault = count == 0 ? 1 : 0;
        address.setIsDefault(isDefault);

        address.setCreateBy(username);
        address.setCreateTime(new Date());
        address.setModifyBy(username);
        address.setModifyTime(new Date());

        Integer rows = addressMapper.insert(address);
        if(rows != 1) {
            throw new InsertException("新增收货地址时出现了未知异常");
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值