【store商城项目06】收获地址列表展示

1.收货地址列表展示-持久层

1.1规划SQL语句

SELECT * FROM  t_address where uid = ? order by is_default DESC ,created_time DESC

1.2设计接口和抽象方法

public interface AddressMapper {
    /**
     * 根据uid获取收获地址列表
     * @param uid 用户的uid
     * @return 用户的收货地址
     */
    List<Address> findAddressByUid(Integer uid);
}

1.3编写SQL映射

    <select id="findAddressByUid" resultMap="AddressEntityMap">
        SELECT * FROM  t_address where uid = #{uid} order by is_default DESC ,created_time DESC
    </select>

1.4单元测试

    @Test
    public void findAddressByUid(){
        List<Address> list = addressMapper.findAddressByUid(36);
        System.out.println("list = " + list);
    }

2.收获地址列表展示-业务层

2.1规划异常

2.2设计接口和抽象方法

public interface IAddressService {
    /**
     * 根据用户uid获取收获地址
     * @param uid 用户uid
     * @return 用户收获地址
     */
    List<Address> findAddressByUid(Integer uid);

2.3 编写实现类和实现方法

@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Value("${user.address.maxCount}")
    private int maxCount;

    @Override
    public List<Address> findAddressByUid(Integer uid) {
        List<Address> addressByUid = addressMapper.findAddressByUid(uid);
        for (Address address : addressByUid) {
            //address.setAid(null);
            address.setUid(null);
            //address.setProvinceName(null);
            address.setProvinceCode(null);
            //address.setCityName(null);
            address.setCityCode(null);
            //address.setAreaName(null);
            address.setAreaCode(null);
            address.setZip(null);
            //address.setTel(null);
            address.setIsDefault(null);
            address.setCreatedTime(null);
            address.setCreatedUser(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);
        }
        
        return addressByUid;
    }


2.3单元测试

    @Test
    public void findAddressByUid(){
        List<Address> addressByUid = addressService.findAddressByUid(36);
        System.out.println("addressByUid = " + addressByUid);
    }

3.收获地址列表展示-控制层

public class AddressController extends BaseController{
    @Autowired
    private IAddressService addressService;

    @RequestMapping({"","/"})
    public JsonResult<List<Address>> getByUid(HttpSession session) {
        Integer uid = getUidFromSession(session);
        List<Address> data = addressService.findAddressByUid(uid);
        return new JsonResult<>(ok,data);
    }

4.前端页面

		<script type="text/javascript">
			$(document).ready(function(){
				showAddressList();
			});
			function showAddressList(){
				$("#address-list").empty();
				$.ajax({
					url: "/store/address",
					type: "get",
					dataType: "JSON",
					success: function (json) {
						if(json.state == 200){
							var list = json.date;
							for (var i = 0; i < list.length; i++) {
								//先写一个var tr = ''; 然后去上面的地址展示中找一个tr标签复制粘贴到单引号里面,再删去多余的制表符
								var tr = '\n' +
										'#{tag}\n' +
										'#{name}\n' +
										'#{address}\n' +
										'#{phone}\n' +
										' 修改\n' +
										' 删除\n' +
										'设为默认\n' +
										'';
								//下面用正则表达式更改字符串,上面的#{tag}#{name}等等只
								//是占位符,没有任何意义,我也可以把#{tag}写成任何想写的东西
								//replace的第一个参数/占位符/g可以,"占位符"也可以
								tr = tr.replace(/#{tag}/g,list[i].tag);
								tr = tr.replace(/#{name}/g,list[i].name);
								tr = tr.replace("#{address}",list[i].address);
								tr = tr.replace("#{phone}",list[i].phone);

								$("#address-list").append(tr);
							}

							//用hide方法将第一个收货地址的"设为默认"元素隐藏,.add-def:eq(0)表
							//示第一个class为add-def的标签,这样就可以保证隐藏的是第一个收货地址
							$(".add-def:eq(0)").hide();
						} else {
							<!--这个其实永远不会执行,因为没有编写
                            异常,控制层返回的状态码永远是OK-->
							alert("用户收货地址数据加载失败")
						}
						}
				});
			}
		</script>

你可能感兴趣的:(java,junit,开发语言)