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将连接池hikari替换为druid,体验最强大的数据库连接池
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS关闭SELinux安全模块
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作