手记

架构师课学习笔记-第四周知识点

如何使用Hibernate做数据校验

  1. 依赖
  • hibernate-validator
  • 有spring-boot-starter-web就会有上边这个
  1. 常用注解
  • @NotBlank(message = “用户昵称不能为空”)
  • @Length(max = 12, message = “用户昵称不能超过12位”)
  • @Pattern(regexp = “^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$”, message = “手机号格式不正确”)
  • @Email
  • @Min(value = 0, message = “性别选择不正确”)
  • @Max(value = 2, message = “性别选择不正确”)
  1. 使用
public JSONResult update(
        @ApiParam(name = "userId", value = "用户id", required = true)
        @RequestParam String userId,
        // 关键
        @Valid
        @RequestBody CenterUserBO centerUserBO,
        HttpServletRequest request,
        HttpServletResponse response,
        // 关键
        BindingResult result
) {

    if (result.hasErrors()) {   // 关键
        List<FieldError> listError = result.getFieldErrors();
        Map<String, String> map = new HashMap<>();
        for (FieldError error : listError) {
            map.put(error.getField(), error.getDefaultMessage());
        }
        return JSONResult.errorMap(map);
    }
}
  1. 参考资料(来自慕课网)

BO数据验证

  • 验证Bean:
public class ValBean {
    
    
    /**
     * Bean Validation 中内置的 constraint       
     * @Null   被注释的元素必须为 null       
     * @NotNull    被注释的元素必须不为 null       
     * @AssertTrue     被注释的元素必须为 true       
     * @AssertFalse    被注释的元素必须为 false       
     * @Min(value)     被注释的元素必须是一个数字,其值必须大于等于指定的最小值       
     * @Max(value)     被注释的元素必须是一个数字,其值必须小于等于指定的最大值       
     * @DecimalMin(value)  被注释的元素必须是一个数字,其值必须大于等于指定的最小值       
     * @DecimalMax(value)  被注释的元素必须是一个数字,其值必须小于等于指定的最大值       
     * @Size(max=, min=)   被注释的元素的大小必须在指定的范围内       
     * @Digits (integer, fraction)     被注释的元素必须是一个数字,其值必须在可接受的范围内       
     * @Past   被注释的元素必须是一个过去的日期       
     * @Future     被注释的元素必须是一个将来的日期       
     * @Pattern(regex=,flag=)  被注释的元素必须符合指定的正则表达式       
     * Hibernate Validator 附加的 constraint       
     * @NotBlank(message =)   验证字符串非null,且长度必须大于0       
     * @Email  被注释的元素必须是电子邮箱地址       
     * @Length(min=,max=)  被注释的字符串的大小必须在指定的范围内       
     * @NotEmpty   被注释的字符串的必须非空       
     * @Range(min=,max=,message=)  被注释的元素必须在合适的范围内 
     */
    private Long id;

    @Max(value=20, message="{val.age.message}")   
    private Integer age;
    
    @NotBlank(message="{username.not.null}")
    @Length(max=6, min=3, message="{username.length}")
    private String username;

    @NotBlank(message="{pwd.not.null}")
    @Pattern(regexp="/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/", message="密码必须是6~10位数字和字母的组合")
    private String password;
    
    
    @Pattern(regexp="^((13[0-9])|(15[^4,\D])|(18[0,5-9]))\d{8}$", message="手机号格式不正确")
    private String phone;

    @Email(message="{email.format.error}")
    private String email;
}
  • 验证Controller:
@Controller
@RequestMapping(value = "/val")
public class ValidateController {

    @RequestMapping(value = "/val", method=RequestMethod.POST)
    @ResponseBody
    public LeeJSONResult val(@Valid @RequestBody ValBean bean, BindingResult result) throws Exception {
        
        if(result.hasErrors()){    
            //如果没有通过,跳转提示    
            Map<String, String> map = getErrors(result);
            return LeeJSONResult.error(map);
        }else{    
            //继续业务逻辑    
        } 
        
        return LeeJSONResult.ok();
    }
    
    private Map<String, String> getErrors(BindingResult result) {
        Map<String, String> map = new HashMap<String, String>();
        List<FieldError> list = result.getFieldErrors();
        for (FieldError error : list) {
            System.out.println("error.getField():" + error.getField());
            System.out.println("error.getDefaultMessage():" + error.getDefaultMessage());
            
            map.put(error.getField(), error.getDefaultMessage());
        }
        return map;
    }
}

如何做文件上传

  1. 使用MultipartFile file接收
  2. 根据文件路径创建输出流,从MultipartFile获得输入流,输出到输出流
outputStream = new FileOutputStream(outFile);
inputStream = file.getInputStream();
IOUtils.copy(inputStream, outputStream);
  1. 注意文件格式的限制
  2. 图片大小限制
spring
  servlet:
    multipart:
      max-file-size: 512000     # 上传文件大小限制
      max-request-size: 512000  # 请求大小限制
  1. 自定义捕获超出大小
package com.xiong.exception;

import com.xiong.common.utils.JSONResult;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@RestControllerAdvice
public class CustomExceptionHandler {
    public JSONResult handlerMaxUploadFile(MaxUploadSizeExceededException e) {
        return JSONResult.errorMsg("文件大小超出限制");
    }
}

如何使用属性资源文件

  1. 在resources创建资源文件

// file-upload-dev.properties

# 用户头像存放位置
file.userFaceLocation=/work/food/upload/userface
  1. 创建映射类
package com.xiong.resources;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:file-upload-dev.properties")
@ConfigurationProperties(prefix = "file")
public class FileUpload {
    private String userFaceLocation;

    public String getUserFaceLocation() {
        return userFaceLocation;
    }

    public void setUserFaceLocation(String userFaceLocation) {
        this.userFaceLocation = userFaceLocation;
    }
}

  1. 使用映射类

springboot如何做静态资源服务

package com.xiong.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    // 实现静态资源的映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("file:/work/food/upload/");
    }
}
  1. 解决swagger2文档看不了问题

pagehelper嵌套分页bug解决

1.方案: 使用多条sql解决
2. 示例

<?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.xiong.dao.OrdersMapperCustom">
    <resultMap id="myOrders" type="com.xiong.pojo.vo.MyOrdersVO">
        <id column="orderId" property="orderId"/>
        <result column="createdTime" property="createdTime"/>
        <result column="pay_method" property="pay_method"/>
        <result column="real_pay_amount" property="real_pay_amount"/>
        <result column="post_amount" property="post_amount"/>
        <result column="order_status" property="order_status"/>
        <!--关键-->
        <collection property="subOrderItemList" select="getMyOrdersSubItems" ofType="com.xiong.pojo.vo.MySubOrderItemVO"
                    column="orderId">
            <result column="itemId" property="itemId"/>
          
        </collection>
    </resultMap>

    <select id="getMyOrders" parameterType="Map" resultMap="myOrders">
        select
        od.id as orderId,
        od.created_time as createdTime,
        od.pay_method as payMethod,
        od.real_pay_amount as realPayAmount,
        od.post_amount as postAmount,
        os.order_status as orderStatus
        from orders as od
        left join order_status as os
        on od.id = os.order_id
        where od.user_id=#{params.userId}
        and od.is_delete=0
        <if test="params.orderStatus!=null and params.orderStatus!=''">
            and os.order_status=#{params.orderStatus}
        </if>
        order by od.updated_time desc
    </select>

    <select id="getMyOrdersSubItems" resultType="com.xiong.pojo.vo.MySubOrderItemVO" parameterType="String">
      select
        oi.item_id as itemId,
        oi.item_name as itemName,
        oi.item_img as itemImg,
        oi.item_spec_name as itemSpecName,
        oi.buy_counts as buyCounts,
        oi.price as price
      from order_items as oi where order_id=#{orderId};
  </select>
</mapper>
0人推荐
随时随地看视频
慕课网APP