Spring Boot使用redis

pom.xml

在pom.xml中配置启动器依赖

  • spring-boot-starter-data-redis,版本问题可以去maven仓库里找
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.checkCode</groupId>
    <artifactId>checkCode</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>checkCode</name>
    <description>redis project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

  • redis默认情况下是没有密码的,有需要再配就可以
  • redis默认情况下redis.conf文件中的bind是127.0.0.1,如果默认不变的话除了本机就没有人访问了,为了方便操作改成bind 0.0.0.0 这样就没有限制了.
  • redis启动的时候要指定配置文件,不指定的话你修改的默认配置是无效的.一般启动是redis/src目录下 redis-server ../redis.conf
#服务端口
server.port=25880

#redis的ip地址
spring.redis.host=192.168.47.129

#redis的端口号
spring.redis.port=6379


#选择几号库操作
spring.redis.database=0

spring boot下使用redis

在spring boot下使用redis非常简单,spring boot为我们提供了两个操作redis的模版,分别是:

  • StringRedisTemplate
  • RedisTemplate

这两个区别不大,一个用String一个用Object,看需要自己定.一般用String的比较多.

/**
 * String-focused extension of RedisTemplate. Since most operations against Redis are String based, this class provides
 * a dedicated class that minimizes configuration of its more generic {@link RedisTemplate template} especially in terms
 * of serializers.
 * <p/>
 * Note that this template exposes the {@link RedisConnection} used by the {@link RedisCallback} as a
 * {@link StringRedisConnection}.
 *
 * @author Costin Leau
 * @author Mark Paluch
 */
public class StringRedisTemplate extends RedisTemplate<String, String> {

	/**
	 * Constructs a new <code>StringRedisTemplate</code> instance. {@link #setConnectionFactory(RedisConnectionFactory)}
	 * and {@link #afterPropertiesSet()} still need to be called.
	 */
	public StringRedisTemplate() {
		setKeySerializer(RedisSerializer.string());
		setValueSerializer(RedisSerializer.string());
		setHashKeySerializer(RedisSerializer.string());
		setHashValueSerializer(RedisSerializer.string());
	}

	/**
	 * Constructs a new <code>StringRedisTemplate</code> instance ready to be used.
	 *
	 * @param connectionFactory connection factory for creating new connections
	 */
	public StringRedisTemplate(RedisConnectionFactory connectionFactory) {
		this();
		setConnectionFactory(connectionFactory);
		afterPropertiesSet();
	}

	protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
		return new DefaultStringRedisConnection(connection);
	}
}

具体操作

具体操作非常简单,和redis的命令基本上一致,下面举个例子,别的操作也都相差不大.

@SpringBootTest
class RedisApplicationTests {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Test
    void add(){
        stringRedisTemplate.opsForValue().set("k1","v1");
    }

    @Test
    void get(){
        stringRedisTemplate.opsForValue().get("k1");
    }
  
}


posted @ 2020-03-12 18:39  continued258  阅读(169)  评论(0)    收藏  举报