Springboot整合其他框架

Springboot整合junit

  • (1)搭建Springboot工程,引入starter-test依赖
  • (2)编写测试类
  • (3)编写测试方法
    测试类:
    例:UserServiceTest
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJunitApplication.class)
public class UserServiceTest {
    @Autowired
    private UserService userService;
    @Test
    public void testAdd()
    {
        userService.add();
    }

}

Springboot整合redis

  • (1)搭建Springboot工程,引入redis起步依赖
  • (2)配置redis相关属性
  • (3)注入RedisTemplate模版
  • (4)编写测试方法

application.yml配置

spring:
  data:
    redis:
      host: 127.0.0.1
      port: 6379

测试类:

@SpringBootTest
class SpringbootRedisApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSet() {
        //存入数据
        redisTemplate.boundValueOps("name").set("zhangsan");

    }
    @Test
    public void testGet() {
        //获取数据
        Object name = redisTemplate.boundValueOps("name").get();
        System.out.println(name);
    }

}

Springboot整合mybatis

  • (1)搭建Springboot工程,引入相关依赖
  • (2)编写mybatis配置
  • (3)定义表和实体类
  • (4)mapper接口,纯注解开发
  • (5)测试
# datasource
spring:
  datasource:
    url: jdbc:mysql:///db1
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
@Data
public class User {
    private String username;
    private String password;
}
@Mapper
public interface UserMapper {
    @Select("select * from tb_user")
    public List<User> findAll();
}

posted @ 2024-07-23 15:33  jhhhred  阅读(29)  评论(0)    收藏  举报