整合Junit
-
导包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
-
无配置
-
编写测试类
//指定使用spring提供的运行类 @RunWith(SpringRunner.class) //指定spring启动类的字节码对象 @SpringBootTest(classes = SpringTestApplication.class) public class UserServiceTest { @Autowired private UserService userService; @Test public void testAdd() { userService.add(); } }
注意:如果测试类所在的包跟引导类所在包的结构一样,或者是引导类所在包的子包,那么使用@SpringBootTest注解时,则可以不指定引导类的字节码文件
整合mybatis
-
导包
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
-
编写配置文件
# 配置数据库连接信息 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/db1?serverTimezone=UTC username: root password: root # 配置mybatis mybatis: config-location: #指定mybatis核心配置文件路径 #...还有许多
-
编写dao
@Mapper @Repository public interface StudentMapper { @Select("select * from student") public List<Student> findAll(); }