SpringBoot程序启动时,会从以下位置加载配置文件:
- file:./config/:当前项目下的config目录下file:./ :当前项目的根目录classpath:/config/: classpath的/config目录classpath/: classpath的根目录
例子:这里有4个文件,名字都为application.properties
它们的位置分别为:
A.src/main/resources/config
B.src/resources
C.项目根目录/
D.项目根目录/config
在4个application.properties中,相同属性,最高优先级(D)的配置才会生效
优先级的大小排序为D>C>A>B
1.Junit
首先在pom.xml中导入依赖
junit junit 4.13.2 test org.springframework.boot spring-boot-starter-test
写业务类 UserService.java
package com.Mafu.springbootprofiles;
import org.springframework.stereotype.Service;
@Service
public class UserService
{
public void add(){
System.out.println("添加了...");
}
}
在test/java下创建包com.Mafu.Test
创建类UserServiceTest作为UserService的测试类
package com.Mafu.test;
import com.Mafu.springbootprofiles.SpringbootProfilesApplication;
import com.Mafu.springbootprofiles.UserService;
import org.junit.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;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootProfilesApplication.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testAdd()
{
userService.add();
}
}
在测试类中运行程序
后台输出 添加了…
注:若测试类的包名与入口类的包名相同,那么注解SpringBootTest后可以不加括号
2.Mybatis
首先,在项目中引入mybatis起步依赖,添加对应版本的mysql驱动(可以通过创建新模块,idea可以直接创建spring项目,在创建时选择对应的依赖可以自动导入)
在本地数据库创建表t_user
添加2个元组:(1,zhangsan,123),(2,lisi,234)
这样数据库就创建好了
然后在resources文件夹下创建application.yml,导入数据源
spring:
datasource:
url: jdbc:mysql:///mybatis?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
实体类User
package com.mafu.springbootmybatis.domain;
public class User
{
private int id;
private String username;
private String password;
@Override
public String toString()
{
return "User{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
'}';
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
首先用注解的方式查询
UserMapper接口
package com.mafu.springbootmybatis.mapper;
import com.mafu.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper
{
@Select("select * from t_user")
public List findAll();
}
最后是测试类
package com.mafu.springbootmybatis;
import com.mafu.springbootmybatis.domain.User;
import com.mafu.springbootmybatis.mapper.UserMapper;
import com.mafu.springbootmybatis.mapper.UserXmlMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatisApplicationTests
{
@Autowired
private UserMapper userMapper;
@Test
public void testFindAll()
{
List list= userMapper.findAll();
System.out.println(list);
}
}
点击testFindAll旁边的绿色小三角运行程序
成功查询到了数据
第二种方法是通过xml mapper的方式进行查询
在resources文件夹下创建mapper目录,在其中创建userMapper.xml
select * from t_user
UserXmlMapper.java
package com.mafu.springbootmybatis.mapper;
import com.mafu.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserXmlMapper
{
public List findAll();
}
然后在resources下的application.yml中添加映射信息
mybatis: mapper-locations: classpath:mapper/*Mapper.xml #mapper映射文件路径 type-aliases-package: com.mafu.springbootmybatis.domain
其中*Mapper是为了匹配所有Mapper结尾的映射
最后在测试类中新增加一个UserXmlMapper的对象,和方法
@Autowired
private UserXmlMapper userXmlMapper;
@Test
public void testFindAll2()
{
List list=userXmlMapper.findAll();
System.out.println(list);
}
最后运行testFindAll2方法,成功查询到数据。
注意:mysql8.0版本以下的,驱动应该去掉cj。



