如果这里没有这两个属性,返回的int值是影响的行数。如果加了这两个属性,会获取主键并赋值到keyProperty设置的领域模型属性中
ShippingController:
package com.mmall.controller.portal; import com.github.pagehelper.PageInfo; import com.mmall.common.Const; import com.mmall.common.ResponseCode; import com.mmall.common.ServerResponse; import com.mmall.pojo.Shipping; import com.mmall.pojo.User; import com.mmall.service.IShippingService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpSession; /** * Created by geely */ @Controller @RequestMapping("/shipping/") public class ShippingController { @Autowired private IShippingService iShippingService; @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); } @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); } @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); } @RequestMapping("select.do") @ResponseBody public ServerResponse<Shipping> select(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.select(user.getId(),shippingId); } @RequestMapping("list.do") @ResponseBody public ServerResponse<PageInfo> 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); } }
IShippingService:
package com.mmall.service; import com.github.pagehelper.PageInfo; import com.mmall.common.ServerResponse; import com.mmall.pojo.Shipping; /** * Created by geely */ public interface IShippingService { ServerResponse add(Integer userId, Shipping shipping); ServerResponse<String> del(Integer userId, Integer shippingId); ServerResponse update(Integer userId, Shipping shipping); ServerResponse<Shipping> select(Integer userId, Integer shippingId); ServerResponse<PageInfo> list(Integer userId, int pageNum, int pageSize); }
ShippingServiceImpl:
package com.mmall.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.google.common.collect.Maps; import com.mmall.common.ServerResponse; import com.mmall.dao.ShippingMapper; import com.mmall.pojo.Shipping; import com.mmall.service.IShippingService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; /** * Created by geely */ @Service("iShippingService") public class ShippingServiceImpl implements IShippingService { @Autowired private ShippingMapper shippingMapper; 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("新建地址失败"); } public ServerResponse<String> del(Integer userId,Integer shippingId){ int resultCount = shippingMapper.deleteByShippingIdUserId(userId,shippingId); if(resultCount > 0){ return ServerResponse.createBySuccess("删除地址成功"); } return ServerResponse.createByErrorMessage("删除地址失败"); } public ServerResponse update(Integer userId, Shipping shipping){ shipping.setUserId(userId); int rowCount = shippingMapper.updateByShipping(shipping); if(rowCount > 0){ return ServerResponse.createBySuccess("更新地址成功"); } return ServerResponse.createByErrorMessage("更新地址失败"); } public ServerResponse<Shipping> select(Integer userId, Integer shippingId){ Shipping shipping = shippingMapper.selectByShippingIdUserId(userId,shippingId); if(shipping == null){ return ServerResponse.createByErrorMessage("无法查询到该地址"); } return ServerResponse.createBySuccess("更新地址成功",shipping); } public ServerResponse<PageInfo> list(Integer userId,int pageNum,int pageSize){ PageHelper.startPage(pageNum,pageSize); List<Shipping> shippingList = shippingMapper.selectByUserId(userId); PageInfo pageInfo = new PageInfo(shippingList); return ServerResponse.createBySuccess(pageInfo); } }
ShippingMapper:
package com.mmall.dao; import com.mmall.pojo.Shipping; import org.apache.ibatis.annotations.Param; import java.util.List; public interface ShippingMapper { int deleteByPrimaryKey(Integer id); int insert(Shipping record); int insertSelective(Shipping record); Shipping selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Shipping record); int updateByPrimaryKey(Shipping record); int deleteByShippingIdUserId(@Param("userId") Integer userId, @Param("shippingId") Integer shippingId); int updateByShipping(Shipping record); Shipping selectByShippingIdUserId(@Param("userId") Integer userId, @Param("shippingId") Integer shippingId); List<Shipping> selectByUserId(@Param("userId") Integer userId); }
ShippingVo:
package com.mmall.vo; /** * Created by geely */ public class ShippingVo { private String receiverName; private String receiverPhone; private String receiverMobile; private String receiverProvince; private String receiverCity; private String receiverDistrict; private String receiverAddress; private String receiverZip; public String getReceiverName() { return receiverName; } public void setReceiverName(String receiverName) { this.receiverName = receiverName; } public String getReceiverPhone() { return receiverPhone; } public void setReceiverPhone(String receiverPhone) { this.receiverPhone = receiverPhone; } public String getReceiverMobile() { return receiverMobile; } public void setReceiverMobile(String receiverMobile) { this.receiverMobile = receiverMobile; } public String getReceiverProvince() { return receiverProvince; } public void setReceiverProvince(String receiverProvince) { this.receiverProvince = receiverProvince; } public String getReceiverCity() { return receiverCity; } public void setReceiverCity(String receiverCity) { this.receiverCity = receiverCity; } public String getReceiverDistrict() { return receiverDistrict; } public void setReceiverDistrict(String receiverDistrict) { this.receiverDistrict = receiverDistrict; } public String getReceiverAddress() { return receiverAddress; } public void setReceiverAddress(String receiverAddress) { this.receiverAddress = receiverAddress; } public String getReceiverZip() { return receiverZip; } public void setReceiverZip(String receiverZip) { this.receiverZip = receiverZip; } }
ShippingMapper:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mmall.dao.ShippingMapper" > <resultMap id="BaseResultMap" type="com.mmall.pojo.Shipping" > <constructor > <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="user_id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="receiver_name" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="receiver_phone" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="receiver_mobile" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="receiver_province" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="receiver_city" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="receiver_district" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="receiver_address" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="receiver_zip" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" /> <arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" /> </constructor> </resultMap> <sql id="Base_Column_List" > id, user_id, receiver_name, receiver_phone, receiver_mobile, receiver_province, receiver_city, receiver_district, receiver_address, receiver_zip, create_time, update_time </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from mmall_shipping where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from mmall_shipping where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.mmall.pojo.Shipping" > 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() ) </insert> <insert id="insertSelective" parameterType="com.mmall.pojo.Shipping" > insert into mmall_shipping <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="userId != null" > user_id, </if> <if test="receiverName != null" > receiver_name, </if> <if test="receiverPhone != null" > receiver_phone, </if> <if test="receiverMobile != null" > receiver_mobile, </if> <if test="receiverProvince != null" > receiver_province, </if> <if test="receiverCity != null" > receiver_city, </if> <if test="receiverDistrict != null" > receiver_district, </if> <if test="receiverAddress != null" > receiver_address, </if> <if test="receiverZip != null" > receiver_zip, </if> <if test="createTime != null" > create_time, </if> <if test="updateTime != null" > update_time, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="userId != null" > #{userId,jdbcType=INTEGER}, </if> <if test="receiverName != null" > #{receiverName,jdbcType=VARCHAR}, </if> <if test="receiverPhone != null" > #{receiverPhone,jdbcType=VARCHAR}, </if> <if test="receiverMobile != null" > #{receiverMobile,jdbcType=VARCHAR}, </if> <if test="receiverProvince != null" > #{receiverProvince,jdbcType=VARCHAR}, </if> <if test="receiverCity != null" > #{receiverCity,jdbcType=VARCHAR}, </if> <if test="receiverDistrict != null" > #{receiverDistrict,jdbcType=VARCHAR}, </if> <if test="receiverAddress != null" > #{receiverAddress,jdbcType=VARCHAR}, </if> <if test="receiverZip != null" > #{receiverZip,jdbcType=VARCHAR}, </if> <if test="createTime != null" > now(), </if> <if test="updateTime != null" > now(), </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.mmall.pojo.Shipping" > update mmall_shipping <set > <if test="userId != null" > user_id = #{userId,jdbcType=INTEGER}, </if> <if test="receiverName != null" > receiver_name = #{receiverName,jdbcType=VARCHAR}, </if> <if test="receiverPhone != null" > receiver_phone = #{receiverPhone,jdbcType=VARCHAR}, </if> <if test="receiverMobile != null" > receiver_mobile = #{receiverMobile,jdbcType=VARCHAR}, </if> <if test="receiverProvince != null" > receiver_province = #{receiverProvince,jdbcType=VARCHAR}, </if> <if test="receiverCity != null" > receiver_city = #{receiverCity,jdbcType=VARCHAR}, </if> <if test="receiverDistrict != null" > receiver_district = #{receiverDistrict,jdbcType=VARCHAR}, </if> <if test="receiverAddress != null" > receiver_address = #{receiverAddress,jdbcType=VARCHAR}, </if> <if test="receiverZip != null" > receiver_zip = #{receiverZip,jdbcType=VARCHAR}, </if> <if test="createTime != null" > create_time = #{createTime,jdbcType=TIMESTAMP}, </if> <if test="updateTime != null" > update_time = now(), </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.mmall.pojo.Shipping" > update mmall_shipping set user_id = #{userId,jdbcType=INTEGER}, 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} </update> </mapper>