spring-boot整合junit
@RunWith(SpringRunner.class) //这是一个 JUnit 注解,指示用于运行测试的运行器。
@SpringBootTest(classes = DemoApplication.class) //这个注解用于指定测试为Spring Boot测试。它会加载完整的Spring应用程序上下文,
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void methodTest(){
userService.method();
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
spring-boot整合readis
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class SpringbootReadisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testSet(){
redisTemplate.boundValueOps("name").set("jack");
}
@Test
public void testGet(){
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring-boot整合mybatis
spring:
datasource:
url: jdbc:mysql:///bookdb?serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.example.demo.bean
#mapper-locations: classpath:映射文件路径,如果mapper.xml和Mapper接口在同一目录下则不用配置
#config-location: 指定mybatis的配置核心文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="queryAll" resultType="UserInfo">
select * from userinfo
</select>
</mapper>
@Component
@Mapper
public interface UserMapper {
List<UserInfo> queryAll();
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class UserTest {
@Autowired
private UserMapper userMapper;
@Test
public void queryALlTest(){
List<UserInfo> userInfos = userMapper.queryAll();
System.out.println(userInfos);
}
}
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>