當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
028_SpringBoot整合Redis
生活随笔
收集整理的這篇文章主要介紹了
028_SpringBoot整合Redis
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 使用maven構(gòu)建SpringBoot的名叫spring-boot-redis項(xiàng)目
2. Spring Data Redis的啟動(dòng)器
3. pom.xml?
<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><groupId>com.bjbs</groupId><artifactId>spring-boot-redis</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version></properties><dependencies><!-- springBoot的啟動(dòng)器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Data Redis的啟動(dòng)器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- Test的啟動(dòng)器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies> </project>4. 在src/main/resources下, 新建application.properties
spring.redis.pool.max-idle=10 spring.redis.pool.min-idle=5 spring.redis.pool.max-total=20spring.redis.hostName=192.168.25.138 spring.redis.port=6379 spring.redis.password=lyw1234565. 新建RedisConfig.java
package com.bjbs.config;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig;/*** 完成對(duì)Redis的整合的一些配置*/ @Configuration // @Configuration注解SpringBoot啟動(dòng)的時(shí)候會(huì)初始化該類 public class RedisConfig {/*** 1. 創(chuàng)建JedisPoolConfig對(duì)象, 在該對(duì)象中完成一些鏈接池配置。* @ConfigurationProperties: 會(huì)將配置文件中, 前綴(spring.redis.pool)相同的內(nèi)容創(chuàng)建一個(gè)實(shí)體, 然后將剩余部分(max-idle)作為屬性添加到實(shí)體中。*/@Bean@ConfigurationProperties(prefix = "spring.redis.pool")public JedisPoolConfig jedisPoolConfig() {JedisPoolConfig config = new JedisPoolConfig();/** //最大空閑數(shù) config.setMaxIdle(10); * //最小空閑數(shù) config.setMinIdle(5); * //最大鏈接數(shù) config.setMaxTotal(20);*/System.out.println("默認(rèn)值: " + config.getMaxIdle());System.out.println("默認(rèn)值: " + config.getMinIdle());System.out.println("默認(rèn)值: " + config.getMaxTotal());// 返回對(duì)象后, 才能完成屬性注入return config;}/*** 2.創(chuàng)建JedisConnectionFactory: 配置redis鏈接信息*/@Bean@ConfigurationProperties(prefix = "spring.redis")public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {System.out.println("配置完畢: " + config.getMaxIdle());System.out.println("配置完畢: " + config.getMinIdle());System.out.println("配置完畢: " + config.getMaxTotal());JedisConnectionFactory factory = new JedisConnectionFactory();// 關(guān)聯(lián)鏈接池的配置對(duì)象factory.setPoolConfig(config);/* //配置鏈接Redis的信息* //主機(jī)地址factory.setHostName("192.168.70.128"); * //端口 factory.setPort(6379);* //密碼factory.setPassword("lyw123456");*/return factory;}/*** 3.創(chuàng)建RedisTemplate: 用于執(zhí)行Redis操作的方法*/@Beanpublic RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();// 關(guān)聯(lián)template.setConnectionFactory(factory);// 為key設(shè)置序列化器template.setKeySerializer(new StringRedisSerializer());// 為value設(shè)置序列化器template.setValueSerializer(new StringRedisSerializer());return template;} }6. 新建User.java
package com.bjbs.pojo;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String name;private Integer age;public User() {}public User(Integer id, String name, Integer age) {super();this.id = id;this.name = name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", age=" + age + "]";} }7. 新建App.java
package com.bjbs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }8. 新建RedisTest.java
package com.bjbs.test;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.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.bjbs.App; import com.bjbs.pojo.User;/*** SpringBoot整合Redis測(cè)試類*/ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = App.class) public class RedisTest {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;/*** 添加一個(gè)字符串*/@Testpublic void setStr() {this.redisTemplate.opsForValue().set("spring-boot-redis", "SpringBoot整合Redis存儲(chǔ)了一個(gè)字符串");}/*** 獲取一個(gè)字符串*/@Testpublic void getStr() {String value = (String) this.redisTemplate.opsForValue().get("spring-boot-redis");System.out.println(value);}/*** 刪除一個(gè)值*/@Testpublic void delValue(){redisTemplate.delete("spring-boot-redis");}/*** 添加User對(duì)象, 使用JDK的序列化器*/@Testpublic void setUesr() {User user = new User();user.setAge(20);user.setName("張三豐");user.setId(1);// 重新設(shè)置序列化器this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());this.redisTemplate.opsForValue().set("user", user);}/*** 取User對(duì)象, 使用JDK的序列化器*/@Testpublic void getUser() {// 重新設(shè)置序列化器this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());User users = (User) this.redisTemplate.opsForValue().get("user");System.out.println(users);}/*** 基于JSON格式存User對(duì)象*/@Testpublic void setUseJSON() {User user = new User();user.setAge(20);user.setName("李四豐");user.setId(1);this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));this.redisTemplate.opsForValue().set("user_json", user);}/*** 基于JSON格式取User對(duì)象*/@Testpublic void getUseJSON() {this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));User user = (User) this.redisTemplate.opsForValue().get("user_json");System.out.println(user);} }9. 啟動(dòng)服務(wù)器上的Redis, 選中setStr方法, 右鍵——>Run As——>JUnit Test
10. 選中g(shù)etStr方法, 右鍵——>Run As——>JUnit Test?
11. 選中setUser方法, 右鍵——>Run As——>JUnit Test?
12. 選中g(shù)etUser方法, 右鍵——>Run As——>JUnit Test?
13. 選中setUserJSON方法, 右鍵——>Run As——>JUnit Test?
14. 選中g(shù)etUserJSON方法, 右鍵——>Run As——>JUnit Test?
總結(jié)
以上是生活随笔為你收集整理的028_SpringBoot整合Redis的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 018_SpringBoot异常处理方式
- 下一篇: 029_SpringBoot整合JPa