1.总结:昨天主要是实现了根据uid获取该用户的所有收货地址以及设置默认地址;再对默认地址的实现里面让我认识到它的具体操作,首先我们根据aid查询收货地址

    再根据收货地址查询到地址归属人的信息,判断uid是否相同进而判断该用户是否合法进行后续操作;再根据uid将用户的所有设置为非默认地址,最后根据

    aid将用户的地址设置为默认地址

mapper

package com.ku.store.mapper;

import com.ku.store.entity.Address;
import org.apache.ibatis.annotations.Param;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration;

import java.util.Date;
import java.util.List;

public interface AddressMapper {
/**
* 从插入收货地址数据
* @param address 收货地址数据
* @return 受影响行数
*/
Integer insert(Address address);

/**
* 统计某用户的收货地址数量
* @param uid 用户id
* @return 该用户收货地址数量
*/
Integer countByUid(Integer uid);

/**
* 跟汇聚于uid获取当前用户收货地址列表
* @param uid
* @return
*/
List<Address> findByUid(Integer uid);

/**
* 将用户的所有收货地址设置为非默认地址
* @param uid 收货地址归属用户id
* @return 受影响行数
*/
Integer updateNonDefaultByUid(Integer uid);

/**
* 将指定收货地址设置为默认地址
* @param aid 收货地址id
* @param modifiedUser 修改执行人
* @param modifiedTime 修改时间
* @return 受影响行数
*/
Integer updateDefaultByAid(@Param("aid") Integer aid,
@Param("modifiedUser") String modifiedUser,
@Param("modifiedTime") Date modifiedTime);

/**
* 根据收货地址aid,查询收货地址详情
* @param aid 收货地址 id
* @return 匹配的收货地址详情,如果没有,则返回null
*/
Address findByAid(Integer aid);

}

<?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.ku.store.mapper.AddressMapper">
<resultMap id="AddressEntityMap" type="com.ku.store.entity.Address">
<id column="aid" property="aid"/>
<result column="province_code" property="provinceCode"/>
<result column="province_name" property="provinceName"/>
<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>

<insert id="insert" useGeneratedKeys="true" keyProperty="aid">
insert into store.t_address(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)values(
#{uid}, #{name}, #{provinceName}, #{provinceCode}, #{cityName}, #{cityCode}, #{areaName},
#{areaCode}, #{zip}, #{address}, #{phone}, #{tel}, #{tag}, #{isDefault}, #{createdUser},
#{createdTime}, #{modifiedUser}, #{modifiedTime}
)
</insert>

<select id="countByUid" resultType="java.lang.Integer">
select
COUNT(*)
from
store.t_address
where
uid = #{uid}
</select>

<!--resultMap与resultType区别:-->
<select id="findByUid" resultMap="AddressEntityMap">
select
*
from
store.t_address
where
uid = #{uid}
order by
is_default desc, created_time desc
</select>

<update id="updateNonDefaultByUid">
update
store.t_address
set
is_default = 0
where
uid = #{uid}
</update>

<update id="updateDefaultByAid">
update
store.t_address
set
is_default = 1,
modified_user = #{modifiedUser},
modified_time = #{modifiedTime}
where
aid = #{aid}
</update>

<select id="findByAid" resultMap="AddressEntityMap">
select
*
from
store.t_address
where
aid = #{aid}
</select>

</mapper>

service

package com.ku.store.service;

import com.ku.store.entity.Address;

import java.util.List;

public interface IAddressService {
/**
* 创建新的收货地址
* @param uid 当前登录用户uid
* @param username 当前登录用户username
* @param address 用户提交的收货地址
*/
void addNewAddress(Integer uid, String username, Address address);

/**
* 查询某用户的收货地址列表数据
* @param uid 收货地址附属用户的用户id
* @return 改用户的收货地址列表数据
*/
List<Address> getByUid(Integer uid);

/**
* 设置默认地址
* @param aid 收货地址id
* @param uid 归属用户id
* @param username 当前登录用户名
*/
void setDefault(Integer aid, Integer uid, String username);

}

serviceImpl

package com.ku.store.service.impl;

import com.ku.store.entity.Address;
import com.ku.store.mapper.AddressMapper;
import com.ku.store.service.IAddressService;
import com.ku.store.service.IDistrictService;
import com.ku.store.service.exception.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {

@Autowired
private AddressMapper addressMapper;

@Autowired
private IDistrictService districtService;


//定义最大收货地址数量,这个需要在application.properties中添加收获地址数据上限值配置
@Value("${user.address.max-count}")
private int maxCount;

@Override
public void addNewAddress(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());
String provinceName = districtService.getNameByCode(address.getProvinceCode());
String cityName = districtService.getNameByCode(address.getCityCode());
String areaName = districtService.getNameByCode(address.getAreaCode());
address.setProvinceName(provinceName);
address.setCityName(cityName);
address.setAreaName(areaName);

Integer rows = addressMapper.insert(address);
if (rows != 1){
throw new InsertException("插入用户地址异常!");
}
}

@Override
public List<Address> getByUid(Integer uid) {
List<Address> list = addressMapper.findByUid(uid);
for (Address address : list) {
address.setUid(null);
address.setProvinceCode(null);
address.setCityCode(null);
address.setAreaCode(null);
address.setCreatedUser(null);
address.setCreatedTime(null);
address.setModifiedUser(null);
address.setModifiedTime(null);
}
return list;
}

@Transactional//基于spring的JDBC事务
@Override
public void setDefault(Integer aid, Integer uid, String username) {
// 根据参数aid,调用addressMapper中的findByAid()查询收货地址数据
// 判断查询结果是否为null
// 是:抛出AddressNotFoundException
Address address = addressMapper.findByAid(aid);
if (address == null){
throw new AddressNotFoundException("用户地址没找到!");
}

// 判断查询结果中的uid与参数uid是否不一致(使用equals()判断)
// 是:抛出AccessDeniedException:非法访问
if (!address.getUid().equals(uid)){
throw new AccessDeniedException("非法异常!");
}

// 调用addressMapepr的updateNonDefaultByUid()将该用户的所有收货地址全部设置为非默认,并获取返回的受影响的行数
// 判断受影响的行数是否小于1(不大于0)
// 是:抛出UpdateException
Integer rows = addressMapper.updateNonDefaultByUid(uid);
if (rows < 1){
throw new UpdateException("修改非默认地址异常!");
}

// 调用addressMapepr的updateDefaultByAid()将指定aid的收货地址设置为默认,并获取返回的受影响的行数
// 判断受影响的行数是否不为1
// 是:抛出UpdateException
rows = addressMapper.updateDefaultByAid(aid, username, new Date());

if (rows != 1){
throw new UpdateException("设置默认地址时出现异常!");
}

}
}

BaseController

package com.ku.store.controller;

import com.ku.store.service.exception.*;
import com.ku.store.utils.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

/**
* 该类用于响应成功的状态码以及统一处理异常 ,简化原来的controller
*/
//控制类基类
public class BaseController {
public static final int OK = 200;

//统一处理方法抛出的异常,此注解表示专门处理这个类的异常
//注意:不要使用相同的注解参数,例如:Throwable e
@ExceptionHandler({ServiceException.class, FileUploadException.class})
public JsonResult<Void>handleException(Throwable e){
JsonResult<Void> result = new JsonResult<>();
if (e instanceof UsernameDuplicateException){
result.setState(400);
result.setMessage("用户名已存在!");
}else if(e instanceof UserNotFoundException){
result.setState(401);
result.setMessage("用户名未找到!");
}else if(e instanceof PasswordNotMatchException){
result.setState(402);
result.setMessage("密码不匹配!");
}else if(e instanceof AddressCountLimitException){
result.setState(403);
result.setMessage("地址数量超过上限!");
}else if (e instanceof AddressNotFoundException) {
result.setState(404);
result.setMessage("收货地址没找到!");
} else if (e instanceof AccessDeniedException) {
result.setState(405);
result.setMessage("非法异常!");
}else if(e instanceof InsertException){
result.setState(500);
result.setMessage("插入用户数据出现未知错误!");
}else if(e instanceof UpdateException){
result.setState(501);
result.setMessage("更改用户数据出现未知错误!");
}else if(e instanceof FileEmptyException){
result.setState(600);
result.setMessage("文件不能为空!");
}else if(e instanceof FileSizeException){
result.setState(601);
result.setMessage("文件超过限制!");
}else if(e instanceof FileTypeException){
result.setState(602);
result.setMessage("文件类型不允许!");
}else if(e instanceof FileStateException){
result.setState(603);
result.setMessage("文件状态不正常!");
}else if(e instanceof FileUploadIOException){
result.setState(604);
result.setMessage("文件上传流错误!");
}
return result;
}

/**
* 从HttpSession对象中获取uid
* @param session HttpSession对象
* @return 当前登录用户id
*/
protected final Integer getUidFromSession(HttpSession session){
return Integer.valueOf(session.getAttribute("uid").toString());
}

/**
* 从HttpSession对象中获取username
* @param session HttpSession对象
* @return 当前登录用户的username
*/
protected final String getUsernameFromSession(HttpSession session){
return session.getAttribute("username").toString();
}
}

 

AddressController

package com.ku.store.controller;


import com.ku.store.entity.Address;
import com.ku.store.service.impl.AddressServiceImpl;
import com.ku.store.utils.JsonResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addresses")
public class AddressController extends BaseController {

@Autowired
AddressServiceImpl addressService;

@RequestMapping("/add_new_address")
public JsonResult<Void> addNewAddress(Address address, HttpSession session){
//从session中获取uid和username
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
//调用业务对象方法执行业务
addressService.addNewAddress(uid, username, address);
return new JsonResult<Void>(OK);
}

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

@RequestMapping("{aid}/set_default")
public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid, HttpSession session){
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
addressService.setDefault(aid, uid, username);
return new JsonResult<Void>(OK);
}
}

 

2.反思:自己对基础的业务应该要有举一反三的能力,但是柑橘自己还是没办法根据mapper方法自己编写出SQL语句,这是自己写的少了,没有从原理上面进行

    这些小编写,一直都是照着别人的编写,这样是不行的,编写不是为了赶进度,而是逐步实现不按照别人的来,自己编写,随心所欲地编写简单的SQL语句

    ,再就是认识到了一些业务中的逻辑,一些判断都是业务中必不可少的;在设置默认地址中,就是先根据aid查询地址,再根据地址获取uid,再根据uid判断

    登录,合法就根据uid设置该用户所有地址为非默认地址,最后根据aid完成默认地址的设置

 

3.复盘:用户注册,登录,修改信息,修改图像(除咯注册是判断用户名是否已存在,其余都要判断用户信息是否存在、逻辑删除,在登陆和和修改密码还要判断密码是否与存在数据库密码是否一致),以及通过父级代号和编号查询省/市/区