ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

HM-SpringBoot1.5【SpringBoot整合Redis】

2021-07-31 11:02:07  阅读:173  来源: 互联网

标签:SpringBoot boot redis springframework SpringBoot1.5 org Redis redisTemplate name


 

 

 

 

 1

springboot默认连接本地redis,默认6379端口号,不需要配置,注入redisTemplate后直接使用即可

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.5.3</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.haifei</groupId>
12     <artifactId>springboot6-redis</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>springboot6-redis</name>
15     <description>Demo project for Spring Boot</description>
16     <properties>
17         <java.version>1.8</java.version>
18     </properties>
19     <dependencies>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-data-redis</artifactId>
23         </dependency>
24 
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-test</artifactId>
28             <scope>test</scope>
29         </dependency>
30     </dependencies>
31 
32     <build>
33         <plugins>
34             <plugin>
35                 <groupId>org.springframework.boot</groupId>
36                 <artifactId>spring-boot-maven-plugin</artifactId>
37             </plugin>
38         </plugins>
39     </build>
40 
41 </project>
 1 package com.haifei.springboot6redis;
 2 
 3 import org.junit.jupiter.api.Test;
 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 
 8 /**
 9  * 运行测试方法前,将本地redis服务器端打开
10  *  D:\Program Files\redis-2.8.9\redis-server.exe
11  */
12 @SpringBootTest
13 class Springboot6RedisApplicationTests {
14 
15     @Autowired
16     private RedisTemplate redisTemplate;
17 
18     @Test
19     void setTest() {
20         //存入数据
21         redisTemplate.boundValueOps("name").set("zhangsan");
22         System.out.println("数据存入完成");
23     }
24 
25     @Test
26     void getTest() {
27         //获取数据
28         Object name = redisTemplate.boundValueOps("name").get();
29         System.out.println(name);
30     }
31 
32 }

 

 

 

 

 2

实际项目中,多连接远程redis,可在resources下的配置文件中进行配置

 

 

 

 

 

标签:SpringBoot,boot,redis,springframework,SpringBoot1.5,org,Redis,redisTemplate,name
来源: https://www.cnblogs.com/yppah/p/15083605.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有