提交 a99ddfad 编写于 作者: 武汉红喜's avatar 武汉红喜

spring-data-redis demo

上级 d89338d1
package org.hongxi.whatsmars.boot.sample.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.hongxi.whatsmars.boot.sample.redis.domain.User;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
import java.io.Serializable;
/**
* Created by javahongxi on 2017/12/5.
*/
@SpringBootApplication
public class SampleRedisApplication implements CommandLineRunner {
@Autowired
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource // 不要用Autowired, 否则会报错
private RedisTemplate<String, Serializable> redisTemplate;
public static void main(String[] args) {
SpringApplication.run(SampleRedisApplication.class, args);
......@@ -23,5 +29,10 @@ public class SampleRedisApplication implements CommandLineRunner {
public void run(String... args) throws Exception {
stringRedisTemplate.opsForValue().set("count", "1");
System.out.println(stringRedisTemplate.opsForValue().get("count"));
User user = User.builder().username("javahongxi").nickname("hongxi").build();
redisTemplate.opsForValue().set("user3", user);
User user1 = (User) redisTemplate.opsForValue().get("user3");
System.out.println(user1.getUsername());
}
}
package org.hongxi.whatsmars.boot.sample.redis.domain;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Tolerate;
import java.io.Serializable;
import java.util.Date;
/**
* Created by shenhongxi on 2017/6/26.
*/
@Data
@Builder
public class User implements Serializable {
private static final long serialVersionUID = 4064009692985107575L;
private Long id;
private String username;
private String nickname;
private Integer gender;
private Integer age;
private Date createDate;
private Date updateDate;
@Tolerate
public User() {}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册