spring data redis 使用之 spring boot 2.x
准备
本人写的spring data是通过maven子父工程管理
parent项目的 : pom.xml
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">
<parent>
<artifactId>spring-boot-data</artifactId>
<groupId>com.ronnie</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-redis</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- fix start java.lang.ClassNotFoundException: io.lettuce.core.AbstractRedisClient -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.0.3.RELEASE</version>
<optional>true</optional>
</dependency>
<!-- fix end java.lang.ClassNotFoundException: io.lettuce.core.AbstractRedisClient -->
</dependencies>
</project>
使用的是lettuce redis连接客户端
配置
package com.ronnie.data.config;
import com.ronnie.data.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
/**
* @Description:
* @Author: rongyu
* @CreateDate: 2018/9/8$ 16:08$
* @Remark:
*/
@Configuration
public class ApplicationConfig {
// TODO remove config of redis to application.yml
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration configuration =
new RedisStandaloneConfiguration("192.168.1.12", 6379);
RedisPassword redisPassword = RedisPassword.of("lcex123");
configuration.setDatabase(5);
configuration.setPassword(redisPassword);
return new LettuceConnectionFactory(configuration);
}
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
public RedisTemplate<String, User> redisUserTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, User> stringUserRedisTemplate = new RedisTemplate<>();
stringUserRedisTemplate.setKeySerializer(new StringRedisSerializer());
stringUserRedisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
stringUserRedisTemplate.setConnectionFactory(redisConnectionFactory);
return stringUserRedisTemplate;
}
}
测试
package com.ronnie.data;
import com.ronnie.data.entity.User;
import lombok.extern.slf4j.Slf4j;
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.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.Serializable;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate<String, Serializable> redisTemplate;
@Autowired
private RedisTemplate<String, User> userRedisTemplate;
@Test
public void saveString() {
// 保存字符串
redisTemplate.opsForValue().set("a:b:c", "111");
Serializable serializable = redisTemplate.opsForValue().get("a:b:c");
log.info("s={}", serializable);
}
@Test
public void saveUser() {
// 保存字符串
User user = new User();
user.setUsername("root");
user.setPassword("root");
ValueOperations<String, User> opsForValue = userRedisTemplate.opsForValue();
opsForValue.set("1", user);
User user1 = opsForValue.get("1");
log.info("user={}", user1);
}
}

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
C#中的NameValueCollection简介
NameValueCollection继承自NameObjectCollectionBase,并且和一般的键值对不同的是,它支持集合中出现相同的Key。 引用:using System.Collections.Specialized; 直接上示例代码: NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" }, { "quartz.serializer.type", "binary" } }; Console.WriteLine(props[0]); Console.Read(); 输出结果如下: 可以看到,在读取相同的key时,输出结果对Value进行了合并,这就是NameValueCollection与一般键值对的主要区别所在。
-
下一篇
SQLServer之DEFAULT约束
原文: SQLServer之DEFAULT约束 DEFAULT约束添加规则 1、若在表中定义了默认值约束,用户在插入新的数据行时,如果该行没有指定数据,那么系统将默认值赋给该列,如果我们不设置默认值,系统默认为NULL。 2、如果“默认值”字段中的项替换绑定的默认值(以不带圆括号的形式显示),则将提示你解除对默认值的绑定,并将其替换为新的默认值。 3、若要输入文本字符串,请用单引号 (') 将值括起来;不要使用双引号 ("),因为双引号已保留用于带引号的标识符。 4、若要输入数值默认值,请输入数值并且不要用引号将值括起来。 5、若要输入对象/函数,请输入对象/函数的名称并且不要用引号将名称括起来。 使用SSMS数据库管理工具添加DEFAULT约束 1、连接数据库,选择数据表-》右键点击-》选择设计。 2、在表设计窗口中-》选择数据列-》在列属性窗口中找到默认值或绑定-》输入默认值(注意默认值的数据类型和输入格式)。 3、点击保存按钮(或者ctrl+s)-》刷新表-》再次打开表查看结果。 使用T-SQL脚本添加DEFAULT约束 当表结构已存在时 首先判断表中是否存在默认约束,如果存在则...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS7,8上快速安装Gitea,搭建Git服务器