87.【SpringBoot-01】(四)

简介: 87.【SpringBoot-01】

5.松散绑定 (松散语法)

就是我们yaml中的last-name等同于实体类中驼峰命名的lastName

实体类的数据

package com.example.springboot01hello.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
/*@ConfigurationProperties作用:
*   将配置文件中配置的每一个属性的值,映射到这个组件中;
*   告诉SpringBoot将本类中的所有属性和配置文件相关的配置进行绑定
*  参数: prefix = "person"将配置文件中的person下面的所有书信一一对应
*
* 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
 */
@ConfigurationProperties(prefix = "person")  //这里面的命名不能使用驼峰,全部小写
@PropertySource(value = "classpath:jsxs.properties")
public class people {
    private String lastName;
    private Integer age;
    private Boolean happy;
    private Date birth;   //这里的日期格式,一定要是xxxx/xx/xx否则报错
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
    public people(String lastName, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
        this.lastName = lastName;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }
    public people() {
    }
    public String getlastName() {
        return lastName;
    }
    public void setlastName(String lastName) {
        this.lastName = lastName;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Boolean getHappy() {
        return happy;
    }
    public void setHappy(Boolean happy) {
        this.happy = happy;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public Map<String, Object> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }
    public List<Object> getLists() {
        return lists;
    }
    public void setLists(List<Object> lists) {
        this.lists = lists;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "people{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

yaml的配置文件

# 外对象
person:
# 基本属性
  last-name: jsxs${random.uuid}
  age: ${random.int}
  happy: false
  birth: 2001/12/01
  hello: sdsdsd
#键值对
  maps: {k1: v1,k2: v2}
#数组
  lists:
    - code
    - music
    - girls
# 内嵌对象
  dog:
    # 假如说person这个yaml类中不存在hello,那么就会输出hello00_旺财
    # 假如说存在: 就会输出存在的hello的值
    name: ${person.hello:hello00}_旺财
    age: 15

6.JSR303校验

实质上就是: 利用注解对数据类型进行初步校验。

需要导入依赖以及添加注解 @Validated //数据校验

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <version>2.7.7</version>
</dependency>
(1).JSR303的总结

message这个属性就是如果出现错误,我们在控制台打印什么消息

@NotNull(message="名字不能为空")
private String userName;
@Max(value=120,message="年龄最大不能查过120")
private int age;
@Email(message="邮箱格式错误")
private String email;
空检查
@Null       验证对象是否为null
@NotNull    验证对象是否不为null, 无法查检长度为0的字符串
@NotBlank   检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格.
@NotEmpty   检查约束元素是否为NULL或者是EMPTY.
Booelan检查
@AssertTrue     验证 Boolean 对象是否为 true  
@AssertFalse    验证 Boolean 对象是否为 false  
长度检查
@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内  
@Length(min=, max=) string is between min and max included.
日期检查
@Past       验证 Date 和 Calendar 对象是否在当前时间之前  
@Future     验证 Date 和 Calendar 对象是否在当前时间之后  
@Pattern    验证 String 对象是否符合正则表达式的规则
.......等等
除此以外,我们还可以自定义一些数据校验规则
(2).JSR303数据核验的测试

实体类 人

  • 添加注解 @Validated //数据校验
  • 进行测试邮箱 @Email(message = “邮箱格式错误!”)
package com.example.springboot01hello.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
/*@ConfigurationProperties作用:
*   将配置文件中配置的每一个属性的值,映射到这个组件中;
*   告诉SpringBoot将本类中的所有属性和配置文件相关的配置进行绑定
*  参数: prefix = "person"将配置文件中的person下面的所有书信一一对应
*
* 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
 */
@ConfigurationProperties(prefix = "person")  //这里面的命名不能使用驼峰,全部小写
@PropertySource(value = "classpath:jsxs.properties")
@Validated  //数据校验
public class people {
    @Email(message = "邮箱格式错误!")
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;   //这里的日期格式,一定要是xxxx/xx/xx否则报错
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
    public people(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }
    public people() {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Boolean getHappy() {
        return happy;
    }
    public void setHappy(Boolean happy) {
        this.happy = happy;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public Map<String, Object> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }
    public List<Object> getLists() {
        return lists;
    }
    public void setLists(List<Object> lists) {
        this.lists = lists;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "people{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

相关文章
|
SQL Java 数据库连接
|
5月前
|
XML Java 数据格式
SpringBoot详解
SpringBoot详解
46 0
|
Java 数据库 数据安全/隐私保护
89.【SpringBoot-02】(八)
89.【SpringBoot-02】
43 0
|
XML Java 数据格式
87.【SpringBoot-01】(二)
87.【SpringBoot-01】
60 0
|
Java 测试技术 容器
87.【SpringBoot-01】(六)
87.【SpringBoot-01】
69 0
|
Java 数据库连接 数据库
SpringBoot总结必知必会
SpringBoot总结必知必会
91 0
|
SQL 监控 druid
SpringBoot详解(中)
SpringBoot详解(中)
3055 12
SpringBoot详解(中)
|
Java 应用服务中间件
SpringBoot(二)
SpringBoot(二)
138 0
SpringBoot(二)
|
XML Java 应用服务中间件
SpringBoot(三)
SpringBoot(三)
151 0
SpringBoot(三)
|
存储 开发框架 Java
《SpringBoot篇》19.SpringBoot整合Quart
《SpringBoot篇》19.SpringBoot整合Quart
381 0
《SpringBoot篇》19.SpringBoot整合Quart
下一篇
DataWorks