Java 杂谈我爱编程程序员

【SpringBoot2.0系列07】SpringBoot之re

2018-08-17  本文已影响73人  余空啊

【SpringBoot2.0系列01】初识SpringBoot
【SpringBoot2.0系列02】SpringBoot之使用Thymeleaf视图模板
【SpringBoot2.0系列03】SpringBoot之使用freemark视图模板
【SpringBoot2.0系列04】SpringBoot之使用JPA完成简单的rest api
【SpringBoot2.0系列05】SpringBoot之整合Mybatis
【SpringBoot2.0系列06】SpringBoot之多数据源动态切换数据源
【SpringBoot2.0系列07】SpringBoot之redis使用(Lettuce版本)

前言

前面三节我们讲解了springboot与关系型数据库交互,现在我们需要了解一下springboot,今天我们就需要学习了与nosql数据库交互,今天我们主要讲一下springboot如果操作redis。 目前java操作redis的客户端有jedisLettuce。在springboot1.x系列中,其中使用的是jedis,但是到了springboot2.x其中使用的是Lettuce。 因为我们的版本是springboot2.x系列,所以今天使用的是Lettuce
关于jedislettuce的区别:

实现

1、依赖

新建一个springboot工程,添加如下依赖。

<dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- redis依赖commons-pool 这个依赖一定要添加 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
    </dependencies>

然后在application.yml配置一下redis服务器的地址

server:
  port: 8989
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    # 密码 没有则可以不填
    password: 123456
    # 如果使用的jedis 则将lettuce改成jedis即可
    lettuce:
      pool:
        # 最大活跃链接数 默认8
        max-active: 8
        # 最大空闲连接数 默认8
        max-idle: 8
        # 最小空闲连接数 默认0
        min-idle: 0

2、 redis配置

接下来我们需要配置redis的key跟value的序列化方式,默认使用的JdkSerializationRedisSerializer 这样的会导致我们通过redis desktop manager显示的我们key跟value的时候显示不是正常字符。 所以我们需要手动配置一下序列化方式 新建一个config包,在其下新建一个RedisConfig.java 具体代码如下

/**
 * @Auther: yukong
 * @Date: 2018/8/17 14:58
 * @Description: redis配置
 */
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {

    /**
     * 配置自定义redisTemplate
     * @return
     */
    @Bean
    RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);

        template.setValueSerializer(serializer);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);
        template.afterPropertiesSet();
        return template;
    }

}

其中@Configuration 代表这个类是一个配置类,然后@AutoConfigureAfter(RedisAutoConfiguration.class) 是让我们这个配置类在内置的配置类之后在配置,这样就保证我们的配置类生效,并且不会被覆盖配置。其中需要注意的就是方法名一定要叫redisTemplate 因为@Bean注解是根据方法名配置这个bean的name的。

3、测试

我们需要测试在redis缓存对象的用例,所以我们需要新建一个实体类。
代码如下:

public class User implements Serializable {

    private static final long serialVersionUID = 1222221L;

    private Long id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 性别 1=男 2=女 其他=保密
     */
    private Integer sex;


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

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

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Long getId() {
        return id;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }
}
我们在`Chapter6Application.java`测试一下

代码如下

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

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void redisTest() {
        // redis存储数据
        String key = "name";
        redisTemplate.opsForValue().set(key, "yukong");
        // 获取数据
        String value = (String) redisTemplate.opsForValue().get(key);
        System.out.println("获取缓存中key为" + key + "的值为:" + value);

        User user = new User();
        user.setUsername("yukong");
        user.setSex(18);
        user.setId(1L);
        String userKey = "yukong";
        redisTemplate.opsForValue().set(userKey, user);
        User newUser = (User) redisTemplate.opsForValue().get(userKey);
        System.out.println("获取缓存中key为" + userKey + "的值为:" + newUser);

    }

}

首先我们引入springboot的测试环境,然后注入一个RedisTemplate对象,这里我解释一下为什么我前面让大家在配置redis的时候 放回RedisTemplate的方法的方法名一定要叫redisTemplate因为

如果我们没有把哪个bean命名为redisTemplate 而这里又这么注入,会导致我们使用的springboot内置的tempalte而不是我们配置的,导致我们的配置不生效,从而埋坑,所以最好的方法就是把方法命名为redisTemplate从而覆盖系统内置的。

接下来我们运行测试类。结果如下。


image.png

同时查看redis中缓存的结果。


image.png image.png

中文成功显示,并且对象在redis以json方式存储,代表我们配置成功。

下列的就是Redis其它类型所对应的操作方式

最后配套教程的代码全部在这里
github https://github.com/YuKongEr/SpringBoot-Study。麻烦点个star或者fork吧。

上一篇 下一篇

猜你喜欢

热点阅读