引言
在现代应用开发中,Spring Boot 提供了极大的便利性,使得集成各种框架变得更加高效和灵活。本文将详细介绍如何在 Spring Boot 项目中整合 JUnit、Redis 和 MyBatis 等重要框架,以提升项目的测试能力、缓存性能和数据持久化效果。
整合其他框架
junit
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies>
redis
需要在创建的时候加上 redis
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
mybatis
依赖
SpringBoot 配置 的 版本不一样 所用的依赖也不一样
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency>
yaml
server: port: 8081 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8 username: root password: 253215
创建 userMapper
package com.example.springbootdemo01.mapper; import com.example.springbootdemo01.pojo.User; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserMapper { // 也可一用上面写注解的方法 // @Select("select * from sys_user") List<User> findAll(); }
在 resource 里面 编写 mapper 创建 userMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace 绑定 对应 的 dao/mapper 接口--> <mapper namespace="com.example.springbootdemo01.mapper.UserMapper"> <!-- id 绑定 的为 接口 的 方法--> <select id="findAll" resultType="User"> select * from sys_user </select> </mapper>
使用
package com.example.springbootdemo01.controller; import com.example.springbootdemo01.mapper.UserMapper; import com.example.springbootdemo01.pojo.User; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @MapperScan("com.example.springbootdemo01.mapper") @RestController public class helloController { @Autowired private UserMapper userMapper; @GetMapping("/hello") public void hello(){ List<User> userList = userMapper.findAll(); System.out.println(userList); } }
condition
简单使用
选择性创建bean
测试
导入 依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootDemo01Application.class, args); // 判断 是 否 有 导入这个 包 Object redisTemplate = context.getBean("redisTemplate"); System.out.println(redisTemplate); }
需求
当存在 jedis 时 返回 bean 不存在 则 不返回
package com.example.springbootdemo01.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class ClassCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { // 需求 导入 jedis 坐标之后 才 返回 true // 判断 文件是否 存在 boolean flag=true; try { Class<?> cls = Class.forName("redis.clients.jedis.Jedis"); } catch (ClassNotFoundException e) { flag=false; } return flag; } }
依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
userconfig
package com.example.springbootdemo01.config; import com.example.springbootdemo01.condition.ClassCondition; import com.example.springbootdemo01.pojo.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; // 配置类 @Configuration public class UserConfig { @Bean // 条件 @Conditional(ClassCondition.class) public User user(){ return new User(); } }
切换web配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 在这个依赖里面 排除 这个--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 再 从新 导入 这个依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
<!--引入启动器依赖 里面就有默认的tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--排除tomcat--> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <!--引入Jetty--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>