目录
1、添加整合junit起步依赖(可以直接勾选)
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
2、编写测试类
- @SpringBootTest
- class Springboot07JunitApplicationTests {
- @Autowired
- private BookService bookService;
-
- @Test
- public void testSave() {
- bookService.save();
- }
- }
SpringBoot整合Spring(不存在)SpringBoot整合SpringMVC(不存在)SpringBoot整合MyBatis(可以)
(1)创建新模块,选择Spring初始化,并配置模块相关基础信息(之前文章有教程)
(2)选择当前模块需要使用的技术集(MyBatis、MySQL)
(3)设置数据源参数
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
- username: root
- password: root
- type: com.alibaba.druid.pool.DruidDataSource
SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区,或在MySQL数据库端配置时区解决此问题
jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
(4) 定义数据层接口与映射配置
- @Mapper
- public interface UserDao {
- @Select("select * from tbl_book where id=#{id}")
- Book getById(Integer id);
- }
(5)测试类中注入dao接口,测试功能
- @SpringBootTest
- class Springboot08MybatisApplicationTests {
- @Autowired
- private BookDao bookDao;
-
- @Test
- public void testGetById() {
- Book book = bookDao.getById(1);
- System.out.println(book);
- }
- }