收货地址模块技术要点:
1、同步获取自增主键
2、数据绑定的对象绑定
3、越权问题升级巩固
一、新增收货地址
Controller层:
//新建收货地址
@RequestMapping("add.do")
@ResponseBody
public ServerResponse add(HttpSession session,Shipping shipping){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user ==null)
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
return iShippingService.add(user.getId(),shipping);
}
Service层:
//新建收货地址
public ServerResponse add(Integer userId, Shipping shipping){
shipping.setUserId(userId);
int rowCount = shippingMapper.insert(shipping);
if(rowCount > 0){
Map result = Maps.newHashMap();
result.put("shippingId",shipping.getId());
return ServerResponse.createBySuccess("新建地址成功",result);
}
return ServerResponse.createByErrorMessage("新建地址失败");
}
Mapper.xml:
insert into mmall_shipping (id, user_id, receiver_name,
receiver_phone, receiver_mobile, receiver_province,
receiver_city, receiver_district, receiver_address,
receiver_zip, create_time, update_time
)
values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{receiverName,jdbcType=VARCHAR},
#{receiverPhone,jdbcType=VARCHAR}, #{receiverMobile,jdbcType=VARCHAR}, #{receiverProvince,jdbcType=VARCHAR},
#{receiverCity,jdbcType=VARCHAR}, #{receiverDistrict,jdbcType=VARCHAR}, #{receiverAddress,jdbcType=VARCHAR},
#{receiverZip,jdbcType=VARCHAR}, now(), now()
)
同步获取自增主键
Mybatis 配置文件 useGeneratedKeys 参数只针对 insert 语句生效,默认为 false。当设置为 true 时,表示如果插入的表以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键返回。
二、删除收货地址
Controller层:
@RequestMapping("del.do")
@ResponseBody
public ServerResponse del(HttpSession session,Integer shippingId){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user ==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iShippingService.del(user.getId(),shippingId);
}
Service层:
public ServerResponse del(Integer userId,Integer shippingId){
int resultCount = shippingMapper.deleteByShippingIdUserId(userId,shippingId);
if(resultCount > 0){
return ServerResponse.createBySuccess("删除地址成功");
}
return ServerResponse.createByErrorMessage("删除地址失败");
}
Mapper.xml:
DELETE FROM mmall_shipping
where id = #{shippingId}
and user_id = #{userId}
三、更新收货地址
Controller层:
@RequestMapping("update.do")
@ResponseBody
public ServerResponse update(HttpSession session,Shipping shipping){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user ==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iShippingService.update(user.getId(),shipping);
}
Service层:
public ServerResponse update(Integer userId, Shipping shipping){
shipping.setUserId(userId);
int rowCount = shippingMapper.updateByShipping(shipping);
if(rowCount > 0){
return ServerResponse.createBySuccess("更新地址成功");
}
return ServerResponse.createByErrorMessage("更新地址失败");
}
Mapper.xml:
update mmall_shipping
set
receiver_name = #{receiverName,jdbcType=VARCHAR},
receiver_phone = #{receiverPhone,jdbcType=VARCHAR},
receiver_mobile = #{receiverMobile,jdbcType=VARCHAR},
receiver_province = #{receiverProvince,jdbcType=VARCHAR},
receiver_city = #{receiverCity,jdbcType=VARCHAR},
receiver_district = #{receiverDistrict,jdbcType=VARCHAR},
receiver_address = #{receiverAddress,jdbcType=VARCHAR},
receiver_zip = #{receiverZip,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = now()
where id = #{id,jdbcType=INTEGER}
and user_id = #{userId,jdbcType=INTEGER}
SpringMVC绑定对象
CSpringMVC会按请求参数名和POJO属性名进行自动匹配,自动为该对象填充属性值,支持级联属性。
注意:pojo对象的属性名和表单中input的name属性一致
表单:
编号:
名称:
出厂时间:
备注:
处理器映射方法:
@RequestMapping(value={"/insertItem.action"})
public String insertItem(Model model,Item item){
return "itemList";
}
注意:提交的表单不要有日期类型的数据,否则会报错400,如果需要提交日期类型的数据,需要自定义参数绑定的内容
SpringMVC时间处理
1. 在控制器中使用@InitBind注解
@Controller
public class TestFileController {
@InitBinder
public void initBind(WebDataBinder binder){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
@RequestMapping("/doAdd")
public void doAdd(@ModelAttribute User user, HttpServletRequest request){
System.out.println(user.getName());
System.out.println(user.getBirthtime());
}
}
2. 在POJO中日期属性上添加@DateTimeFormat
public class User{
private String name;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private Date birthtime;
// 属性的setter/getter方法略
}
SpringMVC乱码解决
post中文乱码:
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncodingFilter
*.action
get中文乱码:
方法1:修改tomcat的server.xml
方式2:手动转码
new String(request.getParameter("id").getBytes("iso8859-1"), "utf-8");
四、查询收货地址列表
Controller层:
@RequestMapping("list.do")
@ResponseBody
public ServerResponse list(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
@RequestParam(value = "pageSize",defaultValue = "10")int pageSize,
HttpSession session){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user ==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iShippingService.list(user.getId(),pageNum,pageSize);
}
Service层:
public ServerResponse list(Integer userId,int pageNum,int pageSize){
PageHelper.startPage(pageNum,pageSize);
List shippingList = shippingMapper.selectByUserId(userId);
PageInfo pageInfo = new PageInfo(shippingList);
return ServerResponse.createBySuccess(pageInfo);
}
Mapper.xml:
SELECT
from mmall_shipping
where user_id = #{userId}
Java面试的完整博客目录如下:Java笔试面试目录