2. Introduction to spring boot
2.1 configuration file
2.1.1 yml and properties
- spring boot supports 2 configuration files: *. yaml/*.yml, *. properties
- Default name of configuration file: application
- YML format: application.yml
- properties: application.properties
1) properties configuration
-
Location:% maven%/src/main/resources/
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-bBtZKn4E-1634646593323)(assets/image-20211019082248820.png)]
-
Configuration content: key=value
- The key content has any value. Generally, the package naming method is adopted. For example: jdbc.driver
#Port number server.port=9090 #Data source configuration spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc://localhost:3306/ssm_db1 spring.datasource.username=root spring.datasource.password=1234
2) yml configuration
-
Profile location:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-daprrvkp-16346593326) (assets / image-20211019083112015. PNG)]
-
Configuration content: key: value
#example A: B: C: key: value
#Port number server: port: 9091 #data source spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc://localhost:3306/ssm_db1 username: root password: 1234
3) yml advanced usage
-
Data types supported by yml
# Custom data user: username: tom age: 12 birthday: 1997/10/10 vip: true valueList: - MyBatis - SpringMVC - SpringBoot ageArray: - 18 - 20 - 22 userList: # Complex writing list < Map > - > [{Name: Tom, age: 20}, {Name: Jack, age: 22}] - name: tom age: 20 - name: Jack age: 22
2.1.2 configuration content acquisition
- Two methods are usually used: @ Value, @ ConfigurationProperties
- @Value: get value one by one. Get value through key. Key is the writing method of the key in the properties file
- @ConfigurationProperties, determine the prefix and load a group of content at one time.
1) @ Value get a Value
package com.czxy.boot.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author Uncle Tong * @email liangtong@itcast.cn */ @Configuration public class OneConfig { /** * Get the content in the configuration file ${key: default} */ @Value("${server.port:8000}") private Integer port; /** * Print obtained content * @return */ @Bean public String oneDemo() { System.out.println("port Port number:" + port); return ""; } }
2) @ ConfigurationProperties gets a set of values
package com.czxy.boot.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; /** * @author Uncle Tong * @email liangtong@itcast.cn */ @Configuration @ConfigurationProperties(prefix = "user") public class UserConfig { private String username; private Integer age; private Date birthday; private boolean vip; private List<String> valueList; private String[] ageArray; private List<Map<String,String>> userList; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public boolean isVip() { return vip; } public void setVip(boolean vip) { this.vip = vip; } public List<String> getValueList() { return valueList; } public void setValueList(List<String> valueList) { this.valueList = valueList; } public String[] getAgeArray() { return ageArray; } public void setAgeArray(String[] ageArray) { this.ageArray = ageArray; } public List<Map<String, String>> getUserList() { return userList; } public void setUserList(List<Map<String, String>> userList) { this.userList = userList; } @Override public String toString() { return "UserConfig{" + "username='" + username + '\'' + ", age=" + age + ", birthday='" + birthday + '\'' + ", vip=" + vip + ", valueList=" + valueList + ", ageArray=" + Arrays.toString(ageArray) + ", userList=" + userList + '}'; } @Bean public String userDemo() { System.out.println(this); return ""; } }
2.1.3 profile priority
1) yml and properties priority
- Properties have priority over yml (properties override the contents of yml)
2) Location priority
-
Configuration files are stored in different locations with different priorities.
-
file:./config /: the config directory of the directory where the project jar package is located
-
file:. /: the same level directory where the project jar package is located
-
classpath:/config: the config directory under the classpath (resource) directory
-
Classpath: /: under the classpath (resource) directory
-
-
Priority in idea environment
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-p5www1ay-16346593328) (assets / image-20211019092843936. PNG)]
-
Comparison of complete configuration priorities (priority: 9001 > 9002 > 9003 > 9004)
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG yeguhazr-1634659329) (assets / image-20211019093802940. PNG)]
2.1.4 multi environment configuration
-
You need to write a configuration file application-{profile}.yml for different environments
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG rfkaonzu-1634659331) (assets / image-20211019094732238. PNG)]··
-
Start different configurations
-
Method 1: application.yml file configuration
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG fbpqrqtk-1634659332) (assets / image-20211019095038531. PNG)]
spring: profiles: active: 10000
-
Method 2: in idea, configure the parameters of the startup class
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-bwopwszm-1634659332) (assets / image-20211019095301241. PNG)]
-
Method 3: in cmd, configure the parameters of the jar package
java -jar -Dspring.profiles.active=10000 xxx.jar
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-8qib5ar-163464659333) (assets / image-20211019102303488. PNG)]
-
2.2 deployment
2.2.1 project deployment
- Package the spring boot project into a runnable jar.
- Just add a plug-in in the pom.xml file and configure the startup class.
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- Configure startup class --> <mainClass>com.czxy.boot.HelloApplication</mainClass> </configuration> </plugin> </plugins> </build>
2.2.2 hot deployment
-
Status quo: after the controller is written, it needs to be restarted to take effect.
-
Hot deployment: after modifying some content, it is automatically deployed and can be accessed without restarting.
-
Steps:
- Enable auto make automatic compilation (one-time)
- Set auto compile project (follow project)
- Add dependency
-
Steps:
-
Enable auto make automatic compilation (one-time)
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zqq5c4df-163464659334) (assets / image-20211019102918856. PNG)]
-
Set auto compile project (follow project)
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-avxjgpdh-163446593334) (assets / image-20211019103034566. PNG)]
-
Add dependency
<!-- Hot deployment module --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <!-- This needs to be true --> <optional>true</optional> </dependency>
-
2.3 integration test
-
spring boot integration Junit
-
Steps:
- Add dependency
- Write test cases
-
realization:
-
Add dependency
<!--test starter--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
-
Write test cases
package com.czxy.boot.service; import com.czxy.boot.HelloApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; /** * @author Uncle Tong * @email liangtong@itcast.cn */ @RunWith(SpringRunner.class) // spring integrates Junit @SpringBootTest(classes = HelloApplication.class) // spring boot integration Junit public class TestUserService { @Resource private UserService userService; @Test public void testSave() { userService.save(); } }
-
3. Unified environment: VS
- Front end development tool: Visual Studio Code
4. ES 6 (ECMSScript 6)
4.1 what is ES6
- ECMAScript 6 is a JavaScript specification.
4.2 basic grammar
4.2.1 declaring variables
//1 declare the common variable var var a = 1; //2 declare the local variable let let b = 2; //3 declare constant const const c = 3;
4.2.2 template string
-
Status quo: in js, splicing strings is very troublesome.
-
Template string: solve the problem of splicing strings
-
Syntax:
var t = `content`;