• springboot之二:整合junit进行单元测试+整合redis(本机、远程)+整合mybatis


    资源地址:

    整合junit的代码:https://download.csdn.net/download/zhiaidaidai/88291527

    整合redis的代码:https://download.csdn.net/download/zhiaidaidai/88291536

    整合mybatis的代码:https://download.csdn.net/download/zhiaidaidai/88307290

    首先先在IDEA里创建一个空项目。

    整合junit

    整体流程

    1. 搭建SpringBoot工程

    2. 引入starter-test起步依赖

    3. 编写测试类

    4. 添加测试相关注解     

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

    1. 编写测试方法

    创建模块:

    整体项目结构和对应的代码

    整体项目:

    UserService.java:

    1. package com.itheima.springboottest;
    2. import org.springframework.stereotype.Service;
    3. @Service
    4. public class UserService {
    5. public void add(){
    6. System.out.println("add..");
    7. }
    8. }

    UserServiceTest.java:

    1. package com.itheima.springboottest;
    2. import org.junit.jupiter.api.Test;
    3. import org.junit.runner.RunWith;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. import org.springframework.test.context.junit4.SpringRunner;
    7. @RunWith(SpringRunner.class)
    8. @SpringBootTest(classes = SpringbootTestApplication.class)
    9. //使用IDE自动创建的项目没有加引导类。是因为如果测试文件属于service文件对应的同一包结构或者在其子包之下,可以不用加。
    10. // 如果不在同一包结构或者子包之下,则必须加引导类class
    11. public class UserServiceTest {
    12. @Autowired
    13. private UserService userService;
    14. @Test
    15. public void testAdd(){
    16. userService.add();
    17. }
    18. }

     导入RunWith和SpringRunner爆红:

    选中RunWith,alt+enter后选择选项‘将JUnit4”添加到类路径中’。

    随后分别选中RunWith和SpringRunner,按alt+enter,选择选项“导入类”。

    整合redis

    整体流程

    1. 搭建SpringBoot工程

    2. 引入redis起步依赖

    3. 配置redis相关属性

    4. 注入RedisTemplate模板

    5. 编写测试方法,测试

    创建模块:

    整合本机redis:

    只有本机的redis不需要进行配置。我们先启动本机redis服务。redis的安装与启动可以见我的另一篇博客:http://t.csdn.cn/AlSjX

    整体项目结构和对应的代码

     将SpringbootRedisApplicationTests.java中改写为以下代码:

    1. package com.itheima.springbootredis;
    2. import org.junit.jupiter.api.Test;
    3. import org.junit.runner.RunWith;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. import org.springframework.data.redis.core.RedisTemplate;
    7. import org.springframework.test.context.junit4.SpringRunner;
    8. @RunWith(SpringRunner.class)
    9. @SpringBootTest
    10. class SpringbootRedisApplicationTests {
    11. @Autowired
    12. private RedisTemplate redisTemplate;
    13. @Test
    14. public void testSet() {
    15. //存入数据
    16. redisTemplate.boundValueOps("name").set("zhangsan");
    17. }
    18. @Test
    19. public void testGet() {
    20. //存入数据
    21. Object name = redisTemplate.boundValueOps("name").get();
    22. System.out.println(name);
    23. }
    24. }

    整合远程redis:

    在resources包下面新建application.yml(原本就有个application.properties也没有关系)

    整体项目结构和对应的代码:

    application.yml(redis配置了密码的话就写password字段,否则不用):

    1. spring:
    2. redis:
    3. host: 127.0.0.1
    4. port: 6379
    5. password: 123456

    整合mybatis:

    整体流程:

    1. 搭建SpringBoot.工程
    2. 引入mybatis起步依赖,添动加mysq驱动
    3. 编写DataSource和MyBatis相关配置
    4. 定义表和实体类
    5. 编写dao和mapper文件/纯注解开发
    6. 测试

    创建模块、准备数据库环境:

     

     注意:勾选了两个依赖项。

    准备数据库环境可以通过以下任意一种方法:

    1、新建一个文件user.txt,将以下代码复制进去后名字保存为user.sql,然后在navicat里执行这个sql文件。

    2、cmd命令行打开mysql服务,随后直接复制以下代码回车后运行。

    1. /*!40101 SET NAMES utf8 */;
    2. /*!40101 SET SQL_MODE=''*/;
    3. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    4. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    5. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    6. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    7. CREATE DATABASE /*!32312 IF NOT EXISTS*/`springboot` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
    8. USE `springboot`;
    9. /*Table structure for table `t_user` */
    10. DROP TABLE IF EXISTS `t_user`;
    11. CREATE TABLE `t_user` (
    12. `id` int(11) NOT NULL AUTO_INCREMENT,
    13. `username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    14. `password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
    15. PRIMARY KEY (`id`)
    16. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    17. /*Data for the table `t_user` */
    18. insert into `t_user`(`id`,`username`,`password`) values (1,'zhangsan','123'),(2,'lisi','234');
    19. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    20. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    21. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    22. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

    整体项目结构和对应的代码:

    新建domain包,User类:

    以下的代码可以先定义好类和三个字段,然后使用alt+insert快速生成getter和setter和tostring方法

    1. package com.itheima.springbootmybatis.domain;
    2. public class User {
    3. private int id;
    4. private String username;
    5. private String password;
    6. public int getId() {
    7. return id;
    8. }
    9. public void setId(int id) {
    10. this.id = id;
    11. }
    12. public String getUsername() {
    13. return username;
    14. }
    15. public void setUsername(String username) {
    16. this.username = username;
    17. }
    18. public String getPassword() {
    19. return password;
    20. }
    21. public void setPassword(String password) {
    22. this.password = password;
    23. }
    24. @Override
    25. public String toString() {
    26. return "User{" +
    27. "id=" + id +
    28. ", username='" + username + '\'' +
    29. ", password='" + password + '\'' +
    30. '}';
    31. }
    32. }

    新建mapper包,UserMapper类:

    1. package com.itheima.springbootmybatis.mapper;
    2. import com.itheima.springbootmybatis.domain.User;
    3. import org.apache.ibatis.annotations.Mapper;
    4. import org.apache.ibatis.annotations.Select;
    5. import java.util.List;
    6. @Mapper
    7. public interface UserMapper {
    8. @Select("select * from t_user")
    9. public List findaAll();
    10. }

    SpringbootMybatisApplicationTests.java中:

    1. package com.itheima.springbootmybatis;
    2. import com.itheima.springbootmybatis.domain.User;
    3. import com.itheima.springbootmybatis.mapper.UserMapper;
    4. import org.junit.jupiter.api.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.boot.test.context.SpringBootTest;
    8. import org.springframework.test.context.junit4.SpringRunner;
    9. import java.util.List;
    10. @RunWith(SpringRunner.class)
    11. @SpringBootTest
    12. class SpringbootMybatisApplicationTests {
    13. @Autowired
    14. private UserMapper userMapper;
    15. @Test
    16. public void testFindAll(){
    17. List list = userMapper.findaAll();
    18. System.out.println(list);
    19. }
    20. }

    application.yml:

    1. spring:
    2. datasource:
    3. url: jdbc:mysql:///springboot?serverTimezone=UTC
    4. username: root
    5. password: 123456
    6. driver-class-name: com.mysql.jdbc.Driver

    出现的问题:

    alt+insert快速生成方法失效/只有版权的问题:

    首先要确保在类里定义了相应的字段,其次需要确保光标在类的大括号里。这两个条件都确认了后可能是电脑的快捷键冲突的原因,直接鼠标右键找到生成(generate)也是一样的。

    yml文件里driver-class-name的mysql.jdbc爆红:

    可以去pom.xml文件里找到mysql这个依赖项,将其runtime注释掉。

    SpringbootMybatisApplicationTests中userMapper类爆红:

    其实并不影响程序运行,但是如果觉得有影响的话,可以在UserMapper类里面额外加个

    @Repository注解。
    

  • 相关阅读:
    find_first_of()函数和find_last_of()函数
    Linux环境更换阿里源(CentOS+Ubuntu)
    上传镜像到 docker hub 中
    Acwing:自然数拆分(完全背包求方案数)
    J9数字论:DAO组织的组成形式是怎样的?
    中国储运杂志中国储运杂志社中国储运编辑部2022年第7期目录
    Spring Boot集成Redis实现数据缓存
    备份解决方案介绍
    记一次HEAP CORRUPTION DETECTED问题及解决
    《Linux运维总结:内网服务器通过代理访问外网服务器(方法一)》
  • 原文地址:https://blog.csdn.net/zhiaidaidai/article/details/132641221