Spring整合其他框架

嗜血龍帥發表於2020-12-19

1.SpringBoot整合Junit

測試類需要寫在引導類所在的包及其子包。否則,需要加

@SpringBootTest(classes = SpringbootConfigApplication.class)
實現步驟:

1.搭建SpringBoot工程
在這裡插入圖片描述

在這裡插入圖片描述

2.在pom檔案引入starter-test起步依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

3.編寫測試類

@Service
public class UserService {

    public  void add(int i) {
        if(i == 0) {
            System.out.println("0....");
        }else{
            System.out.println("1....");
        }

        System.out.println("add...");
    }

}

4.編寫測試方法

  • @RunWith(SpringRunner.class)
  • @SpringBootTest(classes=啟動類.class)
/**
 * 如果測試類在引導類所在包及其子包,可不寫 classes = SpringbootTestApplication.class
 */
@SpringBootTest
class SpringbootConfigApplicationTests {

    @Autowired
    private UserService userService;
    @Test
    void contextLoads() {
        userService.add(0);
        userService.add(1);
    }

}

2.SpringBoot整合Mybatis

1.搭建SpringBoot工程
在這裡插入圖片描述

2.mybatis起步依賴,mysql驅動

 <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

3.編寫DataSource和MyBatis相關配置

spring.datasource.url=jdbc:mysql:///springboot?serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

mybatis.mapper-locations=classpath:mapper/*Mapper.xml

4.定義表和實體類
t_user

/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
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');

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

User.java

public class User {

    private int id;

    private String username;

    private String password;

    ... //get set toString
}

5.編寫dao和mapper檔案/純註解開發
UserMapper.java

/**
 * 純註解開發方式
 *
 * @author liqp
 * @version 1.0
 * @date 2020/12/19
 */
//@Mapper
public interface UserMapper {

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

UserMapperXml.java

/**
 * @author liqp
 * @version 1.0
 * @date 2020/12/19
 */
//@Mapper
public interface UserXmlMapper {

    List<User> findAll();
}

UserMapper.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.itheima.springbootmybatis.mapper.UserXmlMapper">

    <select id="findAll" resultType="com.itheima.springbootmybatis.domain.User">
        select * from t_user
    </select>
</mapper>

6.引導類新增mapper掃描

@SpringBootApplication
@MapperScan("com.itheima.springbootmybatis.mapper")//加上該註解後,可以不用在mapper介面上加@Mapper註解
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

7.測試
SpringbootMybatisApplicationTests.java

@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserXmlMapper userXmlMapper;
    @Test
    void contextLoads() {
        List<User> all = userMapper.findAll();
        System.out.println(all);
    }


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

3.SpringBoot整合Redis

1.搭建SpringBoot工程
在這裡插入圖片描述

2.引入redis起步依賴

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.配置redis相關屬性 (如果是本地可忽略)

spring.redis.host=localhost
spring.redis.port=6379

4.注入RedisTemplate模板
5.編寫測試方法,測試

package com.itheima.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.RedisTemplate;
@SpringBootTest
class SpringbootRedisApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void testSet() {
        redisTemplate.boundValueOps("name").set("wangwu");
    }
    @Test
    public void testGet() {
        Object name = redisTemplate.boundValueOps("name").get();
        System.out.println(name);
    }
}

相關文章