springboot学习笔记

1.数组的创建要注意,在 - 后要有个空格

2.在yml文件的对象" : "后面要加上空格

3.取值方式

@Value("${name}")
@Value("${address[0]}")

4.值包括在''中则不转义字符,包裹在""中则转义字符

5.在pom.xml中添加如下依赖


    org.springframework.boot
    spring-boot-configuration-processor
    true

这样在书写application.yml的时候可以按照属性提示,比如person.address等等

6.springboot项目打包成jar包

springboot学习笔记_第1张图片

 springboot学习笔记_第2张图片

将springboot项目打包后,在打包的文件夹下启动如下命令

springboot学习笔记_第3张图片 

 如果要切换到测试环境则:即在上一句的后面添加spring.properties.active=prod,同时观察到图中端口号已经切换为8087

springboot学习笔记_第4张图片

 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中添加如下依赖:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.7.4
		 
	
	com.ydl
	springboot-mybatis
	0.0.1-SNAPSHOT
	springboot-mybatis
	springboot-mybatis
	
		17
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.projectlombok
			lombok
			true
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.1
		
        
            mysql
            mysql-connector-java
        
		
			org.springframework.boot
			spring-boot-test
			test
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			junit
			junit
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					
						
							org.projectlombok
							lombok
						
					
				
			
		
	


 然后建立实体类

@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);
    }

}

你可能感兴趣的:(springboot,学习,java,开发语言)