栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

  • 整合
    • 整合Junit
      • 1.Test类:
      • 2.UserService类:
      • 3.控制台显示:
    • 整合redis
      • 1. 新建项目
      • 2. redis启动
      • 3. Test类:
      • 4. 启动redis测试类
    • 整合MyBatis
      • 1.新建项目
      • 2. User类:
      • 3. 配置
      • 4. 数据库表
      • 使用注解Select的方式
        • 1.Mapper类:
        • 2. Test类
        • 3. 控制台结果:
      • 使用xml配置的方式
        • 1.xml配置文件
        • 2.application.yml配置
        • 3.Test类
        • 4.Mapper类
        • 5.控制台结果打印
      • 5. 期间遇到的错误
        • 解决方法:

整合 整合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 findAll();
}
2. Test类
@SpringBootTest
class DemoMybatisApplicationTests {
	@Autowired
	private UserMapper userMapper;
	@Test
	void testFindAll() {
		List list=userMapper.findAll();
		System.out.println(list);
	}
}
3. 控制台结果:

使用xml配置的方式 1.xml配置文件



	
		select * from t_user;
	

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 list=userxmlMapper.findAll();
		System.out.println(list);
	}
}
4.Mapper类
@Mapper
@Repository
public interface UserXMLMapper {
	public List 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)}

解决方法:


扫描的时候只能扫描主程序入口同级包或者是其子包。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/292380.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号