1.数组的创建要注意,在 - 后要有个空格
2.在yml文件的对象" : "后面要加上空格
3.取值方式
@Value("${name}")
@Value("${address[0]}")
4.值包括在''中则不转义字符,包裹在""中则转义字符
5.在pom.xml中添加如下依赖
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-configuration-processorartifactId>
- <optional>trueoptional>
- dependency>
这样在书写application.yml的时候可以按照属性提示,比如person.address等等
6.springboot项目打包成jar包


将springboot项目打包后,在打包的文件夹下启动如下命令
如果要切换到测试环境则:即在上一句的后面添加spring.properties.active=prod,同时观察到图中端口号已经切换为8087

7.整合junit
- package com.example.junitstudy;
-
- import com.example.service.UserService;
- import org.junit.jupiter.api.Test;
- import org.junit.runner.RunWith;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- @RunWith(SpringRunner.class)//junit框架整合过来
- @SpringBootTest(classes = JunitStudyApplication.class)//将要测试的类导入
- class JunitStudyApplicationTests {
- UserService userService = new UserService();
- @Test
- void contextLoads() {
- userService.add();
- }
- }
8.springboot整合mybatis
在pom中添加如下依赖:
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.4version>
- <relativePath/>
- parent>
- <groupId>com.ydlgroupId>
- <artifactId>springboot-mybatisartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>springboot-mybatisname>
- <description>springboot-mybatisdescription>
- <properties>
- <java.version>17java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starterartifactId>
- <version>1.3.1version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
- <excludes>
- <exclude>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- exclude>
- excludes>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
然后建立实体类
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class User {
- private int id;
- private String user_name;
- private String password;
- }
建立mapper类(查询语句)
-
- @Mapper
- public interface UserMapper {
- @Select("select * from user")
- public List
findAll(); - }
在测试类里配置一下
- package com.ydl.springbootmybatis;
-
- import com.ydl.springbootmybatis.mapper.UserMapper;
- import com.ydl.springbootmybatis.user.User;
- import org.junit.jupiter.api.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- import java.util.List;
-
- @SpringBootTest(classes = SpringbootMybatisApplication.class)
- @RunWith(SpringRunner.class)
- class SpringbootMybatisApplicationTests {
- @Autowired
- UserMapper userMapper;
-
- @Test
- void contextLoads() {
- List
all = userMapper.findAll(); - System.out.println(all);
- }
-
- }