本地的redis得先启动
1.pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
</parent>
<groupId>cn.itcast</groupId>
<artifactId>Spingboot_day1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.9</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--java配置配置数据库要的-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--Spring Boot的属性注入需要的-->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<!--不传递依赖-->
<optional>true</optional>
</dependency>
<!--lombok需要的-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--事务和连接池需要的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--mybatis需要的-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--mybatis通用mapper需要的-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!--Junit需要的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--redis需要的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
2.配置 application.yml 文件;
空格上面的要注意
#
spring:
profiles:
active: abc,def
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot_test
username: root
password: root
redis:
port: 6379
host: localhost
#tomcatΪ81
server:
port: 81
#日志记录级别
logging:
level:
com.itheima: debug
org.springframework: info
# mybatis配置
mybatis:
type-aliases-package: com.itheima.pojo
# 映射文件路径 (一般在启动类加注解就行)
# mapper-locations: classpath:mappers/*.xml(一般在启动类加注解就行)
configuration:
# 控制台输出执行sql log-impl:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6379是本地默认redis端口,可改
3.在test包创建RedisTest.java 测试代码:
测试类
测试类都得加@RunWith(SpringRunner.class) @SpringBootTest
package com.itheima.redis;
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.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Set;
/**
* @author QLBF
* @version 1.0
* @date 2021/2/22 14:57
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
//加入java官方提供的redis测试对象
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test1(){
//1.string字符串
// redisTemplate.opsForValue().set("str", "heima"); //一般用下面的方法
redisTemplate.boundValueOps("str").set("喜洋洋");
System.out.println("str="+redisTemplate.opsForValue().get("str"));
//2.hash 散列
redisTemplate.boundHashOps("h_key").put("name","xiaoli");
redisTemplate.boundHashOps("h_key").put("age",18);
//获取所有域
Set h_key = redisTemplate.boundHashOps("h_key").keys();
System.out.println(" hash散列的所有域:" + h_key);
//获取所有值
List list = redisTemplate.boundHashOps("h_key").values();
System.out.println(" hash散列的所有域的值:" + list);
//3.list 列表
redisTemplate.boundListOps("l_key").leftPush("c");
redisTemplate.boundListOps("l_key").leftPush("b");
redisTemplate.boundListOps("l_key").leftPush("a");
//获取全部元素
List l_list = redisTemplate.boundListOps("l_key").range(0, -1);
System.out.println("list列表的所有元素"+l_list);
//4. set 集合
redisTemplate.boundSetOps("s_key").add("a","b","c");
Set set = redisTemplate.boundSetOps("s_key").members();
System.out.println(" set集合中的所有元素:" + set);
//5.sorted set 有序集合
redisTemplate.boundZSetOps("z_key").add("a",30);
redisTemplate.boundZSetOps("z_key").add("b",20);
redisTemplate.boundZSetOps("z_key").add("c",25);
Set z_set = redisTemplate.boundZSetOps("z_key").range(0, -1);
System.out.println(" zset有序集合中的所有元素:" + z_set);
}
}
运行这个测试类
测试成功!