SpringBoot集成Redis - Demo

项目文件截图

这里写图片描述

pom.xml

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.zhihua</groupId>
	<artifactId>SpringBoot_Redis</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBoot_Redis Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.13.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

application.properties 主要是简单配置一个redis的地址

spring.redis.host=localhost

User.java 模拟bean

package com.zhihua.redis.bean;

import java.io.Serializable;

/**
 *  要支持序列化,方便redis存储
 * @author caizh
 *
 */
public class User implements Serializable {
	
    private Integer id;
    private String name;
    private Integer age;

    public User() {
    }

    public User(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

MyRedisConfig.java 自定义配置redisTemplate的序列化方式

package com.zhihua.redis.config;

import java.net.UnknownHostException;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.GenericJackson2JsonRedisSerializer;

@Configuration
public class MyRedisConfig {

	/**
	 * 自定义redisTemplate配置
	 * @return
	 */
	@Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
	public RedisTemplate<Object, Object> redisTemplate(
			RedisConnectionFactory redisConnectionFactory
			)throws UnknownHostException{
		RedisTemplate<Object, Object> template = new RedisTemplate<Object,Object>();
		template.setConnectionFactory(redisConnectionFactory);
		// 自定义序列化方式
		template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
		return template;
	}
}

SpringbootRedisApplication.java 启动类

package com.zhihua;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 1、配置RedisProperties相关属性
 * 2、可以直接使用
 * 		StringRedisTemplate:常用的String操作
 * 		RedisTemplate<Object, Object>:
 * 	xxxxTemplate:
 * 			JdbcTemplate,RestTemplate
 *
 */
@SpringBootApplication
public class SpringbootRedisApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootRedisApplication.class, args);
	}
}

SpringbootRedisApplicationTests.java 测试类

package com.zhihua.redis;

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.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import com.zhihua.redis.bean.User;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {

	@Autowired
	StringRedisTemplate stringRedisTemplate; // k-v都是字符串
	@Autowired
	RedisTemplate<Object, Object> redisTemplate;//
	
	/**
	 *  字符串(strings),stringRedisTemplate.opsForValue()
	 *  散列(hashes),stringRedisTemplate.opsForHash()
	 *  列表(lists),stringRedisTemplate.opsForList()
	 *  集合(sets),stringRedisTemplate.opsForSet()
	 *  有序集合(sorted sets)stringRedisTemplate.opsForZSet()
	 */
	@Test
	public void testStringRedisTemplate() {
		
		//stringRedisTemplate.opsForValue().set("msg", "这是redis");
		String msg = stringRedisTemplate.opsForValue().get("msg");
		System.out.println(msg);
	}
	
	@Test
	public void testRedisTemplate() {
		
		redisTemplate.opsForValue().set("user",new User(1,"zhangsan",18));
		User user = (User) redisTemplate.opsForValue().get("user");
		System.out.println(user);
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值