数据网站建设工具模板,第六感聊城网站建设,wordpress主题不兼容,建设专业网站运营团队Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外#xff0c;对于很多NoSQL数据库一样提供了自动化配置的支持#xff0c;包括#xff1a;Redis, MongoDB, Elasticsearch, Solr和Cassandra。
使用Redis
Redis是一个开源的使用ANSI C语言编写、支持网络、…Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外对于很多NoSQL数据库一样提供了自动化配置的支持包括Redis, MongoDB, Elasticsearch, Solr和Cassandra。
使用Redis
Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库。
Redis官网Redis中文社区
引入依赖
Spring Boot提供的数据访问框架Spring Data Redis基于Jedis。可以通过引入spring-boot-starter-redis来配置依赖关系。
dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-redis/artifactId/dependency注意不同版本的spring boot下redis的starter依赖名略有不同如果上面的不行可以尝试spring-boot-starter-data-redis 参数配置
按照惯例在application.properties中加入Redis服务端的相关配置具体说明如下
# REDIS (RedisProperties)# Redis数据库索引默认为0spring.redis.database0# Redis服务器地址spring.redis.hostlocalhost# Redis服务器连接端口spring.redis.port6379# Redis服务器连接密码默认为空spring.redis.password# 连接池最大连接数使用负值表示没有限制spring.redis.pool.max-active8# 连接池最大阻塞等待时间使用负值表示没有限制spring.redis.pool.max-wait-1# 连接池中的最大空闲连接spring.redis.pool.max-idle8# 连接池中的最小空闲连接spring.redis.pool.min-idle0# 连接超时时间毫秒spring.redis.timeout0其中spring.redis.database的配置通常使用0即可Redis在配置的时候可以设置数据库数量默认为16可以理解为数据库的schema
测试访问
通过编写测试用例举例说明如何访问Redis。
RunWith(SpringJUnit4ClassRunner.class)SpringApplicationConfiguration(Application.class)public class ApplicationTests { Autowired private StringRedisTemplate stringRedisTemplate; Test public void test() throws Exception { // 保存字符串 stringRedisTemplate.opsForValue().set(aaa, 111); Assert.assertEquals(111, stringRedisTemplate.opsForValue().get(aaa)); }}通过上面这段极为简单的测试案例演示了如何通过自动配置的StringRedisTemplate对象进行Redis的读写操作该对象从命名中就可注意到支持的是String类型。如果有使用过spring-data-redis的开发者一定熟悉RedisTemplateK, V接口StringRedisTemplate就相当于RedisTemplateString, String的实现。
除了String类型实战中我们还经常会在Redis中存储对象这时候我们就会想是否可以使用类似RedisTemplateString, User来初始化并进行操作。但是Spring Boot并不支持直接使用需要我们自己实现RedisSerializerT接口来对传入对象进行序列化和反序列化下面我们通过一个实例来完成对象的读写操作。
创建要存储的对象User
public class User implements Serializable { private static final long serialVersionUID -1L; private String username; private Integer age; public User(String username, Integer age) { this.username username; this.age age; } // 省略getter和setter}实现对象的序列化接口
public class RedisObjectSerializer implements RedisSerializerObject { private ConverterObject, byte[] serializer new SerializingConverter(); private Converterbyte[], Object deserializer new DeserializingConverter(); static final byte[] EMPTY_ARRAY new byte[0]; public Object deserialize(byte[] bytes) { if (isEmpty(bytes)) { return null; } try { return deserializer.convert(bytes); } catch (Exception ex) { throw new SerializationException(Cannot deserialize, ex); } } public byte[] serialize(Object object) { if (object null) { return EMPTY_ARRAY; } try { return serializer.convert(object); } catch (Exception ex) { return EMPTY_ARRAY; } } private boolean isEmpty(byte[] data) { return (data null || data.length 0); }}配置针对User的RedisTemplate实例
Configurationpublic class RedisConfig { Bean JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(); } Bean public RedisTemplateString, User redisTemplate(RedisConnectionFactory factory) { RedisTemplateString, User template new RedisTemplateString, User(); template.setConnectionFactory(jedisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new RedisObjectSerializer()); return template; }}完成了配置工作后编写测试用例实验效果
RunWith(SpringJUnit4ClassRunner.class)SpringApplicationConfiguration(Application.class)public class ApplicationTests { Autowired private RedisTemplateString, User redisTemplate; Test public void test() throws Exception { // 保存对象 User user new User(超人, 20); redisTemplate.opsForValue().set(user.getUsername(), user); user new User(蝙蝠侠, 30); redisTemplate.opsForValue().set(user.getUsername(), user); user new User(蜘蛛侠, 40); redisTemplate.opsForValue().set(user.getUsername(), user); Assert.assertEquals(20, redisTemplate.opsForValue().get(超人).getAge().longValue()); Assert.assertEquals(30, redisTemplate.opsForValue().get(蝙蝠侠).getAge().longValue()); Assert.assertEquals(40, redisTemplate.opsForValue().get(蜘蛛侠).getAge().longValue()); }}当然spring-data-redis中提供的数据操作远不止这些本文仅作为在Spring Boot中使用redis时的配置参考更多对于redis的操作使用请参考Spring-data-redis Reference。
代码示例
本文的相关例子可以查看下面仓库中的chapter3-2-5目录
Githubhttps://github.com/dyc87112/SpringBoot-LearningGiteehttps://gitee.com/didispace/SpringBoot-Learning
如果您觉得本文不错欢迎Star支持您的关注是我坚持的动力