SpringBoot整合其他框架【Junit,Redis,MyBatis】

  • 一、SpringBoot整合Junit
  • ①搭建SpringBoot工程
  • ②引入starter-test起步依赖
  • ③编写测试类
  • (1)在启动类傍边其他类
  • ④添加测试相关注解
  • ⑤编写测试方法
  • 二、SpringBoot整合Redis
  • 1、搭建SpringBoot工程
  • 2、引入redis起步依赖
  • 3、在测试方法当中默认连接本地的redis数据库
  • 4、配置redis相关属性
  • 三、SpringBoot整合MyBatis
  • 1、搭建SpringBoot工程
  • 2、引入mybatis起步依赖,添加mysq|驱动
  • 3、定义表和实体类
  • 4、编写DataSource和MyBatis相关配置
  • 5、编写dao和mapper文件/纯注解开发
  • 6、测试
  • 7、编写dao和mapper文件/XML开发
  • (1)创建UserXmlMapper接口
  • (2) 编写配置文件
  • (3) 配置配置文件
  • (4)完善测试类


一、SpringBoot整合Junit

①搭建SpringBoot工程

springboot集成bpmnjs spring boot集成_intellij-idea


springboot集成bpmnjs spring boot集成_spring_02


springboot集成bpmnjs spring boot集成_java_03


springboot集成bpmnjs spring boot集成_spring_04

springboot集成bpmnjs spring boot集成_spring boot_05

springboot集成bpmnjs spring boot集成_java_06

②引入starter-test起步依赖

  • 快速构建的spring项目自动以及引入了spring-test的依赖

③编写测试类

  • 并且也自动写了test类,但是为了方便演示,现在将下面对应的内容删除
  • 删除成功
(1)在启动类傍边其他类
  • UserService
package cn.itbluebox.springboottest;
import org.springframework.stereotype.Service;
@Service
public class UserService {
    public void add(){
        System.out.println("add...");
    }
}
  • 创建测试类

④添加测试相关注解

  • 引入junit的依赖
<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

@RunWith(SpringRunner.class)@SpringBootTest(clasSes =启动类.class)

springboot集成bpmnjs spring boot集成_intellij-idea_07

package cn.itbluebox.test;
/*
userService的测试类
 */
import cn.itbluebox.springboottest.SpringbootTestApplication;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootTestApplication.class)
public class UserServiceTest {
}

⑤编写测试方法

springboot集成bpmnjs spring boot集成_java_08

package cn.itbluebox.test;
/*
userService的测试类
 */
import cn.itbluebox.springboottest.SpringbootTestApplication;
import cn.itbluebox.springboottest.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootTestApplication.class)
public class UserServiceTest {
    @Autowired
    private UserService userService;
    @Test
    public void test(){
        userService.add();
    }
}
  • 运行测试
  • 运行成功
  • 如果主包和测试包的目录结构是相同的则@SpringBootTest(classes = SpringbootTestApplication.class)不需要写当中的classes
  • 将UserServiceTest类移动到springboottest包当中
package cn.itbluebox.springboottest;

/*
userService的测试类
 */
import cn.itbluebox.springboottest.SpringbootTestApplication;
import cn.itbluebox.springboottest.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void test(){
        userService.add();
    }

}
  • 运行测试

二、SpringBoot整合Redis

1、搭建SpringBoot工程

springboot集成bpmnjs spring boot集成_spring_09


springboot集成bpmnjs spring boot集成_java_10


springboot集成bpmnjs spring boot集成_java_11


springboot集成bpmnjs spring boot集成_intellij-idea_12


springboot集成bpmnjs spring boot集成_spring_13

2、引入redis起步依赖

  • 通过上述的自动创建的工程redis的依赖已经自动添加好

3、在测试方法当中默认连接本地的redis数据库

不需要配置如何信息

springboot集成bpmnjs spring boot集成_spring boot_14

package cn.itbluebox.springbootredis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class SpringbootRedisApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    void testSet() {
        //存入数据
        redisTemplate.boundValueOps("name").set("张三");
    }
    @Test
    void testGet() {
        //获取数据
        Object name = redisTemplate.boundValueOps("name").get();
        System.out.println(name);
    }
}
  • 启动redis
  • springboot集成bpmnjs spring boot集成_spring_15


  • springboot集成bpmnjs spring boot集成_intellij-idea_16

  • 运行测试类(测试方法testSet()存入数据)
  • springboot集成bpmnjs spring boot集成_intellij-idea_17


  • springboot集成bpmnjs spring boot集成_spring boot_18

  • 运行测试类(测试方法testGet()获取数据)
  • springboot集成bpmnjs spring boot集成_intellij-idea_19


  • springboot集成bpmnjs spring boot集成_spring boot_20

4、配置redis相关属性

springboot集成bpmnjs spring boot集成_spring boot_21


springboot集成bpmnjs spring boot集成_spring boot_22


springboot集成bpmnjs spring boot集成_spring boot_23

spring:
  redis:
    host: 127.0.0.1 #redis的主机地址
    port: 6379
  • 运行测试

三、SpringBoot整合MyBatis

1、搭建SpringBoot工程

springboot集成bpmnjs spring boot集成_java_24


springboot集成bpmnjs spring boot集成_java_25


springboot集成bpmnjs spring boot集成_intellij-idea_26


springboot集成bpmnjs spring boot集成_intellij-idea_27


springboot集成bpmnjs spring boot集成_spring_28

2、引入mybatis起步依赖,添加mysq|驱动

通过上述的工程搭建自动添加好了依赖

springboot集成bpmnjs spring boot集成_java_29

3、定义表和实体类

  • 创建数据库
CREATE DATABASE /*!32312 IF NOT EXISTS*/`springboot` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;

USE `springboot`;

/*Table structure for table `t_user` */

DROP TABLE IF EXISTS `t_user`;

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

/*Data for the table `t_user` */

insert  into `t_user`(`id`,`username`,`password`) values (1,'zhangsan','123'),(2,'lisi','234');

springboot集成bpmnjs spring boot集成_springboot集成bpmnjs_30

  • 创建实体类
package cn.tbluebox.springbootmybatis.domain;

public class User {

    private int id;
    private String username;
    private String password;

    public User() {
    }

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = 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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

4、编写DataSource和MyBatis相关配置

springboot集成bpmnjs spring boot集成_springboot集成bpmnjs_31


springboot集成bpmnjs spring boot集成_spring_32


springboot集成bpmnjs spring boot集成_intellij-idea_33

#datasource
spring:
  datasource:
    url: jdbc:mysql:///springboot
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

5、编写dao和mapper文件/纯注解开发

springboot集成bpmnjs spring boot集成_spring boot_34


springboot集成bpmnjs spring boot集成_springboot集成bpmnjs_35

package cn.tbluebox.springbootmybatis.mapper;

import cn.tbluebox.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {

    @Select("select * from t_user")
    public List<User> findAll();

}

6、测试

springboot集成bpmnjs spring boot集成_spring_36

package cn.tbluebox.springbootmybatis;

import cn.tbluebox.springbootmybatis.domain.User;
import cn.tbluebox.springbootmybatis.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    void findAllUser() {
        List<User> all = userMapper.findAll();
        System.out.println(all);
    }

}

运行测试类

springboot集成bpmnjs spring boot集成_spring_37


springboot集成bpmnjs spring boot集成_spring boot_38

7、编写dao和mapper文件/XML开发

(1)创建UserXmlMapper接口

springboot集成bpmnjs spring boot集成_springboot集成bpmnjs_39


springboot集成bpmnjs spring boot集成_intellij-idea_40

package cn.tbluebox.springbootmybatis.mapper;

import cn.tbluebox.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserXmlMapper {

    public List<User> findAll();

}
(2) 编写配置文件

springboot集成bpmnjs spring boot集成_spring_41


springboot集成bpmnjs spring boot集成_springboot集成bpmnjs_42


springboot集成bpmnjs spring boot集成_springboot集成bpmnjs_43

<?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="cn.tbluebox.springbootmybatis.mapper.UserXmlMapper">
    <select id="findAll" resultType="user">
        select * from t_user
    </select>
</mapper>
(3) 配置配置文件

springboot集成bpmnjs spring boot集成_spring_44

#datasource
spring:
  datasource:
    url: jdbc:mysql:///springboot
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

# mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml  #mapper映射文件的路径
  type-aliases-package: cn.tbluebox.springbootmybatis.domain
  # config-location: #指定mybatis核心配置文件
(4)完善测试类

springboot集成bpmnjs spring boot集成_intellij-idea_45

package cn.tbluebox.springbootmybatis;

import cn.tbluebox.springbootmybatis.domain.User;
import cn.tbluebox.springbootmybatis.mapper.UserMapper;
import cn.tbluebox.springbootmybatis.mapper.UserXmlMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserXmlMapper userXmlMapper;

    @Test
    void findAllUser() {
        List<User> all = userMapper.findAll();
        System.out.println(all);
    }

    @Test
    void findAllUser2() {
        List<User> all = userXmlMapper.findAll();
        System.out.println(all);
    }

}
  • 运行测试