81. 确认订单页-显示收货地址列表
此前已经完成“显示收货地址列表”功能,客户端(页面)向http://localhost:8080/addresses
发请求,就可以得到收货地址列表的数据!所以,只需要在orderConfirm.html页面中,当加载页面时,就直接向以上路径发请求,获取数据,并显示在下拉列表中即可:
<script type="text/javascript">
$(document).ready(function() {
showAddressList();
});
// orderConfirm.html: 第118行:增加id
function showAddressList() {
console.log("准备收货地址列表……");
$("#address-list").empty();
$.ajax({
"url":"/addresses",
"type":"get",
"dataType":"json",
"success":function(json) {
var list = json.data;
console.log("count=" + list.length);
for (var i = 0; i < list.length; i++) {
console.log(list[i].name);
var html = '<option value="#{aid}">#{name} | #{tag} | #{province}#{city}#{area}#{address} | #{phone}</option>';
html = html.replace(/#{aid}/g, list[i].aid);
html = html.replace(/#{tag}/g, list[i].tag);
html = html.replace(/#{name}/g, list[i].name);
html = html.replace(/#{province}/g, list[i].provinceName);
html = html.replace(/#{city}/g, list[i].cityName);
html = html.replace(/#{area}/g, list[i].areaName);
html = html.replace(/#{address}/g, list[i].address);
html = html.replace(/#{phone}/g, list[i].phone);
$("#address-list").append(html);
}
}
});
}
</script>
82. 确认订单页-显示所选择的购物车数据-持久层
(a) 规划需要的SQL语句
在购物车列表页,用户可以自由选择若干条数据,点击“结算”时,页面会将用户勾选的数据id传递到确认订单页,在确认订单页中,就需要根据这些id来显示对应的数据,所以,需要实现”根据若干个id查询购物车列表“的功能,需要执行的SQL语句与此前完成的“查询某用户的购物车列表”是高度相似的,只是查询条件要改为“根据若干个id查询”,则SQL语句大致是:
select
cid, uid, pid, t_cart.num, t_cart.price,
title, t_product.price AS realPrice, image
from
t_cart
left join
t_product
on
t_cart.pid=t_product.id
where
cid in (?,?,? ... ?)
order by
t_cart.created_time desc
(b) 设计抽象方法
在CartMapper
中添加:
/**
* 查询若干个数据id匹配的购物车列表
* @param cids 若干个数据id
* @return 匹配的购物车列表
*/
List<CartVO> findByCids(Integer[] cids); // List<Integer> / Integer[] / Integer...
© 配置映射并测试
映射:
<!-- 查询若干个数据id匹配的购物车列表 -->
<!-- List<CartVO> findByCids(Integer[] cids) -->
<select id="findByCids" resultType="cn.tedu.store.vo.CartVO">
SELECT
cid, uid, pid, t_cart.num, t_cart.price,
title, t_product.price AS realPrice, image
FROM
t_cart
LEFT JOIN
t_product
ON
t_cart.pid=t_product.id
WHERE
cid IN
<foreach collection="array" item="cid" separator=","
open="(" close=")">
#{cid}
</foreach>
ORDER BY
t_cart.created_time DESC
</select>
测试:
@Test
public void findByCids() {
Integer[] cids = {
10, 8, 14, 12, 6, 15, 16, 18, 20 };
List<CartVO> list = mapper.findByCids(cids);
System.err.println("count=" + list.size());
for (CartVO item : list) {
System.err.println(item);
}
}
83. 确认订单页-显示所选择的购物车数据-业务层
(a) 规划业务流程、业务逻辑,并创建可能出现的异常
无
(b) 设计抽象方法
在CartService
添加抽象方法:
/**
* 查询若干个数据id匹配的购物车列表
* @param cids 若干个数据id
* @param uid 用户的id
* @return 匹配的购物车列表
*/
List<CartVO> getByCids(Integer[] cids, Integer uid);
© 实现抽象方法并测试
在CartServiceImpl
中,先将持久层新添加的抽象方法复制过来,改成私有方法并实现:
/**
* 查询若干个数据id匹配的购物车列表
* @param cids 若干个数据id
* @return 匹配的购物车列表
*/
private List<CartVO> findByCids(Integer[] cids) {
return cartMapper.findByCids(cids);
}
然后,规划重写接口中的抽象方法:
@Override
public List<CartVO> getByCids(Integer[] cids, Integer uid) {
// 调用findByCids(cids)执行查询,得到列表数据
List<CartVO> carts = findByCids(cids);
// 从列表中移除非当前登录用户的数据:在遍历过程中移除集合中的元素,需要使用迭代器
Iterator<CartVO> it = carts.iterator();
while (it.hasNext()) {
CartVO cart = it.next();
if (!cart.getUid().equals(uid)) {
it.remove();
}
}
// 返回列表
return carts;
}
最后,测试:
@Test
public void findByCids() {
Integer[] cids = {
10, 8, 14, 12, 6, 15, 16, 18, 20 };
Integer uid = 13;
List<CartVO> list =