SpringBoot整合第三方技术

发布时间 2023-03-23 16:38:30作者: qintee

整合JUnit

名称:@SpringBootTest
类型:测试类注解
位置:测试类定义上方
作用:设置JUnit加载的SpringBoot启动类
范例:

@SpringBootTest(classes = Springboot07JunitApplication.class)

class Springboot07JunitApplicationTests {}

注:

classes:设置SpringBoot启动类

如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定

 

 

 

整合Mybatis

 

 

 

 

 

 

yml设置数据源参数

 

 

 注意

SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区

jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC

或在MySQL数据库端配置时区解决此问题

 

Dao inferface定义数据层接口与映射配置

@Mapper的作用是什么?

运行中肯定需要对象运行interface中的方法才行,

那对象怎么来?

Spring的操作中需要ComponentScan来告诉这个类需要自动代理

而在SpringBoot中@Mapper就告诉SpringBoot这个类要做自动代理,简化了开发

@Mapper
public interface UserDao
{
  @Select("select * from user")
  public List<User> getAll();
}

 

测试类中注入dao接口,测试功能

@SpringBootTest
class Springboot08MybatisApplicationTests {
    @Autowired        
    private BookDao bookDao;

    @Test
    public void testGetById() {
        Book book = bookDao.getById(1);                
        System.out.println(book);
    }
}
        

 

 

代码来源:itheima