1.SpringBoot测试
(1)添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
(2)按Shift+CTRL+T生成Junit5测试类
注意:Junit5类上只有@SpringBootTest,像Junit4就要多个@RunWith(SpringRunner.class)
package com.xxxx.controller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class HelloControllerTest {
@Autowired
private HelloController helloController;
@Test
public void hello() {
String hello = helloController.hello();
System.out.println(hello);
}
}
2.Spring整合Mybatis
添加mysql驱动依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
添加SpringBoot的Mybatis启动器
注意:这里是需要定义版本,主要是Mybatis官方开发【根据Spring的接口】
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
application.yml配置
注意:mapper映射注释掉同时需要mapper在一种形式结构目录中
server:
port: 9999
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3307/user?characterEncoding=utf-8
username: root
password: 123456
mybatis:
type-aliases-package: com.cqgcxy.entity
# mapper-locations: classpath:com/cqgcxy/mapper/*Mapper.xml
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--配置文件的根元素-->
<configuration>
<settings>
<!-- 具体的参数值参数名-->
<setting name="mapUnderScoreToCamelCase" value="true" />
</settings>
</configuration>
创建类
这里的@Data是由lombok提供的
创建mapper接口
编写mapper
在App启动类扫mapper包
最后编写测试类
测试成功