欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot整合redis(lettuce)

程序员文章站 2022-04-30 19:43:23
...

SpringBoot整合redis(lettuce)

  • pom文件导入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  • application.yml配置
server:
  port: 8823
spring:
  #redis 要放在spring里,和datasourece同级
  redis:
    host: 127.0.0.1
    port: 6379
    password: 123456
    timeout: 3000
  datasource:
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/hellomybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8

#整合mybatis
mybatis:
  type-aliases-package: com.example.batis.entity
  mapper-locations:
    - classpath:mybatis/mapper/*.xml
  • Demo
package com.example.batis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.sql.SQLException;

@SpringBootTest
class BatisApplicationTests {

	@Autowired
	private RedisTemplate redisTemplate;

	@Test
	void contextLoads() throws SQLException {

        //redisTemplate  操作不同的数据类型,api和命令行的指令相同
        //opsForValue()  操作字符串 类似String
        //opsForList 	 操作List  类似List
        //opsForSet
        //opsForHash
        ·······
        //除了基本的操作,常用的方法都可以直接通过redisTemplate操作,如事务和基本的增删改查
		redisTemplate.opsForValue().set("key","value");
        
        //获取redis的连接对象(较少用)
        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
		connection.flushDb();
		connection.flushAll();

		redisTemplate.opsForValue().set("key","value");
		System.out.println(redisTemplate.opsForValue().get("key"));
        
	}

}
  • 关闭redis的时候要用redis-cli.exe shutdown,暴力关闭redis数据会丢失,这是因为redis保存数据到磁盘不是实时的,可以在redis.conf里配置(save ? ?)

序列化

  • RedisTemplate有默认的配置类,默认使用了jdk的序列化,会对存入的值直接进行转义,因此需要自定义一个RedisTemplate的配置类
package com.example.batis.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {

    //自定义RedisTemplate
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        //为了开发方便,一般直接使用<String,Object>
        RedisTemplate<String,Object>template = new RedisTemplate<String,Object>();
        template.setConnectionFactory(factory);

        //序列化配置
        //Json序列化配置
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        //String的序列化配置
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        //key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        //value采用jackson序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //hash的key也采用String序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        //hash的value采用jackson序列化方式
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }

}

相关标签: Java