Springboot Notes 02 December 17, 2020

There are several ways to configure files.
1. The most primitive is application.properties. This is a key=value.
2. Application.yml must be the name of the applicatin, otherwise Springboot will not recognize it. The configuration is stricter. Key: The format of the space attribute is as shown below

#实体类的
Username:
  id: ${
    
    random.int(10)}
  name: 娇娇
  pwd: ${
    
    random.long}

#thymeleaf 模板配置
spring:
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    cache: false
#    邮箱的配置
  mail:
    host: smtp.163.com
    username: *******2020@163.com
    password: ***************
    protocol: smtp
    default-encoding: UTF-8
    #数据库的配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/op?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
    username: root
    password: 123456
#    Spring mvc 的配置
  mvc:

    view:
      prefix: /
      suffix: .jsp
#mybatis的配置
mybatis:
  configuration:
    #   sql日志显示,这里使用标准显示
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #  数据库中如果有类似 如  user_name 等命名,会将 _后的字母大写,这里是为了和实体类对应
#    map-underscore-to-camel-case: true
  #  配置mapper文件的路径
  mapper-locations: classpath*:com/jj/**/dao/*.xml
  #pageHelper配置(官网推荐配置)
# 分页配置
## pagehelper 分页插件配制
pagehelper:
  reasonable: false # 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据
  support-methods-arguments: true
  params: count=countSql
  row-bounds-with-count: true
  helper-dialect: mysql
  auto-dialect: com.github.pagehelper.dialect.helper.MySqlDialect





Why use yml instead
Insert picture description here
of several small demo
entity classes assigned with yml for properties? If you import @ConfigurationProperties to report red, then add the following dependencies

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

@Component
@ConfigurationProperties(prefix = "username")
public class Username {
    
    

    private int id;

    private String name;

    private String pwd;

    public Username() {
    
    
    }

yml configuration

#实体类的
Username:
  id: ${
    
    random.int(10)}
  name: 娇娇
  pwd: ${
    
    random.long}

Test class

package com.jj.demo;

import com.jj.demo.pojo.Username;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {
    
    
@Autowired
private Username username;
    @Test
    void contextLoads() {
    
    
        System.out.println("username = " + username);
    }

}

Loose binding of yml

If there is a name similar to user_name in the database, the letter after _ will be capitalized, here is to correspond to the entity class

JSR303 data verification
small demo join us and add on the name
Insert picture description here

But what is written in our yml is
Insert picture description herethat an error will be reported at this moment
Insert picture description here

Empty check

  1. @Null verifies whether the object is null
  2. @NotNull verifies whether the object is not null, and cannot check a string with a length of 0
  3. @NotBlank checks whether the constraint string is Null and whether the length of the Trim is greater than 0, only for the string, and the leading and trailing spaces will be removed.
  4. @NotEmpty checks whether the constraint element is NULL or EMPTY.

Booelan check
5. @AssertTrue verifies whether the Boolean object is true
6. @AssertFalse verifies whether the Boolean object is false

Length check
7. @Size(min=, max=) verifies whether the length of the object (Array, Collection, Map, String) is within the given range
8. @Length(min=, max=) string is between min and max included.

Date check
9. @Past verifies whether Date and Calendar objects are before the current time
10. @Future verifies whether Date and Calendar objects are after the current time
11. @Pattern verifies that String objects comply with the rules of regular expressions
20.@Max(value =120,message="The oldest age cannot be checked 120") Verify the maximum value of the field, and give the default value and error message
21. @Email(message="Email format error") Determine whether the field is in the mail format.
The location of the configuration file and the order of the location
1, the config folder configuration file
under the project path 2, the configuration file under the project path
3, the configuration file under the resource path
4, the configuration file under the resource path
** If you need to configure multiple ports Number can be used in the following way**

---
server:
  port: 8081
spring:
  profiles: dev
---
server:
  port: 8082
spring:
  profiles: dev
---
server:
  port: 8083
spring:
  profiles: test

The relationship between xxx.properties and the configuration file
1. Spring boot will load a large number of automatic configuration classes.
2. If the function we need is not in the Springboot default configuration class
3. You can see if our component is in the automatic configuration class or not. If
4, automatically configure the class in the container, you will get it from the properties.
xxxx AutoConfigurartion automatic configuration class, add component
xxx.properties: encapsulate some comments on the relevant properties in the configuration file

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46937429/article/details/111330960