新增收货地址的编写,首先导入t_address数据表,接着创建实体类
package com.cy.store.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Objects;
@Data//set get
@AllArgsConstructor
@NoArgsConstructor
public class Address extends BaseEntity implements Serializable {
private Integer aid;
private Integer uid;
private String name;
private String provinceName;
private String provinceCode;
private String cityName;
private String cityCode;
private String areaName;
private String areaCode;
private String zip;
private String address;
private String phone;
private String tel;
private String tag;
private Integer isDefault;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Address address1 = (Address) o;
return Objects.equals(aid, address1.aid) && Objects.equals(uid, address1.uid) && Objects.equals(name, address1.name) && Objects.equals(provinceName, address1.provinceName) && Objects.equals(provinceCode, address1.provinceCode) && Objects.equals(cityName, address1.cityName) && Objects.equals(cityCode, address1.cityCode) && Objects.equals(areaName, address1.areaName) && Objects.equals(areaCode, address1.areaCode) && Objects.equals(zip, address1.zip) && Objects.equals(address, address1.address) && Objects.equals(phone, address1.phone) && Objects.equals(tel, address1.tel) && Objects.equals(isDefault, address1.isDefault);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), aid, uid, name, provinceName, provinceCode, cityName, cityCode, areaName, areaCode, zip, address, phone, tel, isDefault);
}
}
接着设计mapper层
public interface AddressMapper {
Integer insert(Address address);
Integer countByUid(Integer uid);
}
<?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.cy.store.mapper.AddressMapper">
<sql id="column">
uid,name,province_name,province_code,city_name,city_code,area_name,area_code,zip,
address,phone,tel,tag,is_default,created_user,created_time,modified_user,modified_time
</sql>
<resultMap id="AddressEntityMap" type="com.cy.store.entity.Address">
<!--将表的资源和类的属性不一致的字段进行匹配指定,名称一致的字段可以省略不写-->
<!--在定义映射规则时主键是不可以省略的-->
<id column="aid" property="aid"/>
<result column="province_name" property="provinceName"/>
<result column="province_code" property="provinceCode"/>
<result column="city_code" property="cityCode"/>
<result column="city_name" property="cityName"/>
<result column="area_code" property="areaCode"/>
<result column="area_name" property="areaName"/>
<result column="is_default" property="isDefault"/>
<result column="created_user" property="createdUser"/>
<result column="created_time" property="createdTime"/>
<result column="modified_user" property="modifiedUser"/>
<result column="modified_time" property="modifiedTime"/>
</resultMap>
<!---id属性:表示映射的接口中方法的名称,直接在标签的内容部来编写SQL语句-->
<!--
useGeneratedKeys属性:表示开启某个字段的值递增(主键设置为递增)
keyProperty属性:标签将表中的哪个字段作为主键进行递增
-->
<insert id="insert" useGeneratedKeys="true" keyProperty="aid">
insert into t_address (<include refid="column"/>)
values(
#{uid},
#{name},
#{provinceName},
#{provinceCode},
#{cityName},
#{cityCode},
#{areaName},
#{areaCode},
#{zip},
#{address},
#{phone},
#{tel},
#{tag},
#{isDefault},
#{createdUser},
#{createdTime},
#{modifiedUser},
#{modifiedTime}
)
</insert>
<select id="countByUid" resultType="Integer">
select count(*) from t_address where uid=#{uid}
</select>
</mapper>
接着设计service层
public interface IAddressService {
void AddAddress(Integer uid,String username,Address address);
}
@Service
public class IAddressServiceImpl implements IAddressService {
@Autowired
private AddressMapper addressMapper;
@Value("${user.address.max-count}")
private Integer maxCount;
@Override
public void AddAddress(Integer uid, String username, Address address) {
//调用收货地址通知的方法
Integer count = addressMapper.countByUid(uid);
if (count >= maxCount){
throw new AddressCountLimitException("收货地址条数超出限制");
}
address.setUid(uid);
Integer isDefault = count == 0 ? 1 : 0;
address.setIsDefault(isDefault);
address.setCreatedUser(username);
address.setCreatedTime(new Date());
address.setModifiedUser(username);
address.setModifiedTime(new Date());
//插入收货地址的方法
Integer rows = addressMapper.insert(address);
if (rows != 1){
throw new InsertException("插入用户的收货地址产生异常");
}
}
}
最后编写controller层
@RestController
@RequestMapping("address")
public class AddressController extends BaseController{
@Autowired
private IAddressService addressService;
@RequestMapping("add_new_address")
public JsonResult<Void> addAddress(Address address, HttpSession session){
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
addressService.AddAddress(uid,username,address);
return new JsonResult<>(OK);
}
}
前端代码
<script type="text/javascript">
$("#btn-add-new-address").click(function (){
$.ajax({
url:"/address/add_new_address",
dataType:"json",
data:$("#form-add-new-address").serialize(),
type:"post",
success:function (data){
if (data.state==200){
alert("新增成功")
}else{
alert("新增失败")
}
},
error:function (data){
alert("产生未知错误"+data.status)
}
})
})
</script>