Skip to content

整合其他框架

SpringBoot提供了起步依赖,方便导入坐标,因此能够非常方便的去整合其他的第三方框架

整合Junit

SpringBoot整合Junit来提供单元测试,基本步骤:

  1. 搭建SpringBoot工程

  2. 引入starter-test起步依赖(创建SpringBoot工程会自动的写入该起步依赖)

  3. 编写测试类

    src/main/java文件夹中编写相关的测试类:

    java
    package com.jlc.springboottest;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService {
        public void add() {
            System.out.println("add...");
        }
    }
  4. 编写测试方法

  5. 添加测试相关的注解

    对于测试用例,我们在src/test/java文件中进行编写:

    java
    package com.jlc.test;
    
    import com.jlc.springboottest.SpringbootTestApplication;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import com.jlc.springboottest.UserService;
    
    // UserService的测试类
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = SpringbootTestApplication.class)
    public class UserServiceTest {
        @Autowired
        private UserService userService;
        
        @Test
        public void testAdd() {
            userService.add();
        }
    }

    如果当前测试用例所在的包,是其引导类src/main/java/com.jlc.springboottest.SpringbootTestApplication所在的包com.jlc.springboottest的子包(或者包名相同),那么测试用例的代码可以修改为:

    java
    package com.jlc.springboottest;
    
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import com.jlc.springboottest.UserService;
    
    // UserService的测试类
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class UserServiceTest {
     @Autowired
     private UserService userService;
    
     @Test
     public void testAdd() {
         userService.add();
     }
    }

整合Redis

SpringBoot整合Redis的基本步骤:

  1. 搭建SpringBoot工程

    要勾选NoSQL中的Spring Data Redis (Access+Driver)

  2. 引入redis起步依赖(勾选NoSQL中的Spring Data Redis (Access+Driver)后会自动引入)

    具体的引入内容为:

    xml
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
  3. 配置Redis相关属性(只有本机的Redis才可以不进行配置(Redis的连接信息默认的就是本机的Ip))

    在实际的开发中,一定时需要外置的Redis,因此,我们需要进行相应的配置

    在配置文件application.yml文件中进行如下的配置:

    yaml
    spring:
      redis:
        host: 127.0.0.1   # redis的主机Ip,后续修改成对应的Ip即可
        port: 6379   # redis的端口号,后续修改成对应的即可
  4. 注入RedisTemplate模板

  5. 编写测试方法,测试

    src/test/java中创建一个测试类:

    java
    package com.jlc.springbootredis;
    
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringbootRedisApplicationTests {
        @Autowired
        private RedisTemplate redisTemplate;   // 注入RedisTemplate模板
        
        @Test
        public void testSet() {
            // 存入数据
            redisTemplate.boundValueOps("name").set("jlc");
        }
        
        @Test
        public void getSet() {
            // 获取数据
            Object name = redisTemplate.boundValueOps("name").get();
            System.out.println(name);
        }
    }

整合MyBatis

SpringBoot整合MyBatis的基本步骤:

  1. 搭建SpringBoot工程

    勾选对应的依赖:SQL中的MyBatis FrameworkMySQL Driver

  2. 引入mybatis起步依赖,添加mysql驱动(搭建SpringBoot工程勾选对应的依赖后就会自动创建)

    mybatis起步依赖的内容为:

    xml
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.0</version>
    </dependency>

    mysql的驱动也会自动的添加,其内容为:

    xml
    <dependency>
    	<groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>   <!--表示驱动在运行时生效,可以注释掉,使编译时也生效-->
    </dependency>
  3. 定义表和实体类

    定义一张User数据表

    编写实体类:

    java
    package com.jlc.springbootmybatis.domain;
    
    public class User {
        private int id;
        private String username;
        private String password;
        
        public int getId() { return id; }
        public void setId(int id) { this.id = id; }
        
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }
  4. 编写DataSourceMyBatis相关配置(使用注解开发就不需要配置MyBatis的相关配置)

    在配置文件application.yml文件中进行如下的配置:

    yaml
    # DataSource的配置
    spring:
      datasource:
        # ///表示省略了用户名,Ip和端口,因为是连接的本地的,myMysql表示数据库的名称,后面需要配置时区信息
        url: jdbc:mysql:///myMysql?serverTimezone=UTC    
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
  5. 编写dao/mapper文件/纯注解开发

    • 使用xml方式进行开发

      编写mapper层内容(在src/main/java文件夹下)

      java
      package com.jlc.springbootmybatis.mapper;
      
      import com.jlc.springbootmybatis.domain.User;
      import org.apache.ibatis.annotations.Mapper;
      import org.springframework.sterotype.Repository;
      import java.util.List;
      
      @Mapper
      @Repository
      public interface UserXmlMapper {
          
          public List<User> findAll();
      }

      resources中创建mapper文件夹,用于存放我们的映射文件,创建:UserMapper.xml

      xml
      <?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.jlc.springbootmybatis.mapper.UserXmlMapper">   
          <select id="findAll" resultType="user">
          	select * from user
          </select>
      </mapper>

      使用xml方式开发,需要在配置文件中加入MyBatis的相关配置

      yaml
      # MyBatis的配置
      mybatis:
        mapper-location: classpath: classpath:mapper/*Mapper.xml  # 指定映射文件路径
        type-aliases-package: com.jlc.springbootmybatis.domain.User  # 通过包扫描配置别名
        # config-Location:    # 指定mybatis的核心配置文件
    • 使用注解开发

      编写mapper层内容(在src/main/java文件夹下)

      java
      package com.jlc.springbootmybatis.mapper;
      
      import com.jlc.springbootmybatis.domain.User;
      import org.apache.ibatis.annotations.Mapper;
      import org.apache.ibatis.annotations.Select;
      import org.springframework.sterotype.Repository;
      import java.util.List;
      
      @Mapper
      @Repository
      public interface UserMapper {
          @Select("select * from user")
          public List<User> findAll();
      }
  6. 测试

    注入对应的测试方法:

    java
    package com.jlc.springbootmybatis;
    
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import com.jlc.springbootmybatis.mapper.UserMapper;
    import com.jlc.springbootmybatis.domain.User;
    import java.util.List;
    
    // UserMapper的测试类
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringbootMybatisApplicationTests {
        @Autowired
        private UserMapper userMapper;
        
        @Autowired
        private UserXmlMapper userXmlMapper;
        
        @Test
        public void testXmlFindAll() {
            List<User> list = userXmlMapper.findAll();
            System.out.println(list);
        }
        
        @Test
        public void testFindAll() {
            List<User> list = userMapper.findAll();
            System.out.println(list);
        }
    }

Released under the MIT License.