當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring模板对象之RedisTemplate(Spring整合jedis)
生活随笔
收集整理的這篇文章主要介紹了
Spring模板对象之RedisTemplate(Spring整合jedis)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Spring模板對象之RedisTemplate
構(gòu)建maven項(xiàng)目
pom
<?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><groupId>com.fs</groupId><artifactId>day04_spring_redisTemplate</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.9.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.9.RELEASE</version></dependency><!-- redis--><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>2.0.6.RELEASE</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies></project>redis.properties
# redis服務(wù)器主機(jī)地址 redis.host=192.168.93.132 #redis服務(wù)器主機(jī)端口 redis.port=6379#redis服務(wù)器登錄密碼 #redis.password=root#最大活動連接 redis.maxActive=20 #最大空閑連接 redis.maxIdle=10 #最小空閑連接 redis.minIdle=0 #最大等待時間 redis.maxWait=-1配置類
SpringConfig
package com.fs.config;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import;@Configuration @ComponentScan("com.fs") @Import({RedisConfig.class}) public class SpringConfig { }RedisConfig
package com.fs.config;import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;@PropertySource("redis.properties") public class RedisConfig {@Value("${redis.host}")private String hostName;@Value("${redis.port}")private Integer port;//由于我redis沒有密碼,我就不配置了 // @Value("${redis.password}") // private String password;@Value("${redis.maxActive}")private Integer maxActive;@Value("${redis.minIdle}")private Integer minIdle;@Value("${redis.maxIdle}")private Integer maxIdle;@Value("${redis.maxWait}")private Integer maxWait;/**** @param redisConnectionFactory 連接工廠* @return RedisTemplate*/@Bean//配置RedisTemplatepublic RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory){//1.創(chuàng)建對象RedisTemplate redisTemplate = new RedisTemplate();//2.設(shè)置連接工廠redisTemplate.setConnectionFactory(redisConnectionFactory);//3.設(shè)置redis生成的key的序列化器,對key編碼進(jìn)行處理RedisSerializer stringSerializer = new StringRedisSerializer();redisTemplate.setKeySerializer(stringSerializer);redisTemplate.setHashKeySerializer(stringSerializer);//4.返回return redisTemplate;}/**** @param redisStandaloneConfiguration 連接信息* @param genericObjectPoolConfig 連接池信息* @return 連接工廠*/@Bean//配置Redis連接工廠,redis單點(diǎn)配置,就是一臺redis的服務(wù)器地址public RedisConnectionFactory createRedisConnectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration, GenericObjectPoolConfig genericObjectPoolConfig){//1.創(chuàng)建配置構(gòu)建器,它是基于池的思想管理Jedis連接的JedisClientConfiguration.JedisPoolingClientConfigurationBuilder builder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();//2.設(shè)置池的配置信息對象builder.poolConfig(genericObjectPoolConfig);//3.創(chuàng)建Jedis連接工廠JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration,builder.build());//4.返回return jedisConnectionFactory;}@Bean//配置spring提供的Redis連接池信息public GenericObjectPoolConfig createGenericObjectPoolConfig(){//1.創(chuàng)建Jedis連接池的配置對象GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();//2.設(shè)置連接池信息genericObjectPoolConfig.setMaxTotal(maxActive);genericObjectPoolConfig.setMinIdle(minIdle);genericObjectPoolConfig.setMaxIdle(maxIdle);genericObjectPoolConfig.setMaxWaitMillis(maxWait);//3.返回return genericObjectPoolConfig;}@Bean//配置Redis標(biāo)準(zhǔn)連接配置對象public RedisStandaloneConfiguration createRedisStandaloneConfiguration(){//1.創(chuàng)建Redis服務(wù)器配置信息對象RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();//2.設(shè)置Redis服務(wù)器地址,端口和密碼(如果有密碼的話)redisStandaloneConfiguration.setHostName(hostName);redisStandaloneConfiguration.setPort(port); // redisStandaloneConfiguration.setPassword(RedisPassword.of(password));//3.返回return redisStandaloneConfiguration;}}實(shí)體類
Account
package com.fs.pojo;import lombok.Data;import java.io.Serializable;@Data public class Account implements Serializable {private Integer id;private String name;private Double money; }service
AccountService
package com.fs.service;import com.fs.pojo.Account;import java.util.List;public interface AccountService {void save(Account account); }AccountServiceImpl
package com.fs.service.impl;import com.fs.pojo.Account; import com.fs.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service;@Service("accountService") public class AccountServiceImpl implements AccountService {@Autowiredprivate RedisTemplate redisTemplate;public void save(Account account) {redisTemplate.opsForValue().set("account",account);}} //redisTemplate中的方法// redisTemplate.type() // redisTemplate.persist() // redisTemplate.move() // redisTemplate.hasKey() // redisTemplate.getExpire() // redisTemplate.expire() // redisTemplate.delete() // redisTemplate.rename(); // // redisTemplate.opsForValue().; // redisTemplate.opsForHash().; // redisTemplate.opsForList().; // redisTemplate.opsForSet().; // redisTemplate.opsForZSet(); // // // redisTemplate.boundValueOps().; // // redisTemplate.slaveOf(); // redisTemplate.slaveOfNoOne(); // // redisTemplate.opsForCluster()測試類
package com.fs.service.impl;import com.fs.config.SpringConfig; import com.fs.pojo.Account; import com.fs.service.AccountService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfig.class) public class AccountServiceImplTest {@Autowiredprivate AccountService accountService;@Autowiredprivate RedisTemplate redisTemplate;//測試查詢所有方法@Testpublic void testRedisTemplate() {Account account = new Account();account.setId(1);account.setName("小花");account.setMoney(888D);accountService.save(account);Account account1 = (Account) redisTemplate.opsForValue().get("account");System.out.println(account1.toString());}}測試結(jié)果
總結(jié)
以上是生活随笔為你收集整理的Spring模板对象之RedisTemplate(Spring整合jedis)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring控制事务:声明式事务(注解)
- 下一篇: SpringMVC基础配置与简单的Spr