SpringBoot学习笔记:整合Junit、整合redis和整合mybatis

本文详细记录了SpringBoot项目中如何整合Junit进行单元测试,如何连接并测试Redis,以及使用两种方式(注解和XML配置)整合MyBatis的过程。在整合过程中遇到了Bean创建异常和查询结果为空的问题,并提供了解决方案。
摘要由CSDN通过智能技术生成

整合

整合Junit

1.Test类:

在这里插入图片描述

2.UserService类:

import org.springframework.stereotype.Service;

@Service
public class UserService {

	public void add() {
		System.out.println("add---");
	}
}

3.控制台显示:

在这里插入图片描述

在这里插入图片描述

整合redis

1. 新建项目

首先要在新建时勾选NoSQL里的Spring Data Redis
在这里插入图片描述

2. redis启动

[root@localhost ~]#cd /usr/local/redis/bin
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# ps aux|grep redis
root     101199  0.0  0.2 140992  2032 ?        Ssl  04:13   0:00 ./redis-server *:6379
root     101203  0.0  0.0 112824   984 pts/3    S+   04:13   0:00 grep --color=auto redis
[root@localhost bin]# systemctl stop firewalld

我的redis是之前做项目时在Linux环境下安装配置的,这里就不细说安装过程啦~

3. Test类:

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 DemoRedisApplicationTests {

	@Autowired
	private RedisTemplate redisTemplate;
	@Test
	void testSet() {
		redisTemplate.boundValueOps("name").set("HR");
	}

	@Test
	void testGet() {
		Object name=redisTemplate.boundValueOps("name").get();
		System.out.print(name);
	}
}

4. 启动redis测试类

先后运行set、get会在控制台成功打印名字的值。
在这里插入图片描述

整合MyBatis

1.新建项目

在这里插入图片描述

2. User类:

public class User {

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

3. 配置

这次用的是application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springbootstyle?characterEncoding=utf-8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

4. 数据库表

在这里插入图片描述

使用注解Select的方式

1.Mapper类:
@Mapper
@Repository
public interface UserMapper {
	@Select("select * from t_user")
	public List<User> findAll();
}
2. Test类
@SpringBootTest
class DemoMybatisApplicationTests {
	@Autowired
	private UserMapper userMapper;
	@Test
	void testFindAll() {
		List<User> list=userMapper.findAll();
		System.out.println(list);
	}
}
3. 控制台结果:

在这里插入图片描述

使用xml配置的方式

1.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.example.demo.mapper.UserXMLMapper">
	<select id="findAll" resultType="user">
		select * from t_user;
	</select>
</mapper>
2.application.yml配置

加上扫描

mybatis:
  mapper-locations: classpath:Mapper/*Mapper.xml  #mapper映射文件路径
  type-aliases-package: com.example.demo      #别名包扫描
3.Test类
@SpringBootTest
class DemoMybatisApplicationTests {
	@Autowired
	private UserXMLMapper userxmlMapper;
	@Test
	void testFindAll2() {
		List<User> list=userxmlMapper.findAll();
		System.out.println(list);
	}
}
4.Mapper类
@Mapper
@Repository
public interface UserXMLMapper {
	public List<User> findAll();
}
5.控制台结果打印

同上(没错我懒了)

5. 期间遇到的错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘com.example.demo.DemoMybatisApplicationTests’: Unsatisfied dependency expressed through field ‘userMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.example.mapper.UserMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

解决方法:

在这里插入图片描述
扫描的时候只能扫描主程序入口同级包或者是其子包。
2.测试通过,但是打印出来的结果为null
这位大佬解决了~

https://blog.csdn.net/SiuMu_/article/details/105130739

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值