国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

SpringBoot整合SpringDataRedis的示例代碼

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:波波烤鴨 欄目: 編程語(yǔ)言 歡迎投稿:712375056

  本文介紹下SpringBoot如何整合SpringDataRedis框架的,SpringDataRedis具體的內容在前面已經(jīng)介紹過(guò)了,可自行參考。

1.創(chuàng )建項目添加依賴(lài)

  創(chuàng )建SpringBoot項目,并添加如下依賴(lài):

<dependencies>
    <!-- springBoot 的啟動(dòng)器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Data Redis 的啟動(dòng)器 -->
    <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>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
</dependencies>

2.設置application.properties文件

spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=5
spring.redis.pool.max-total=20
spring.redis.hostName=192.168.88.120
spring.redis.port=6379

3.添加Redis的配置類(lèi)

  添加Redis的java配置類(lèi),設置相關(guān)的信息。

/**
 * @program: springboot-redis-demo
 * @description: Redis的配置類(lèi)
 * @author: 波波烤鴨
 * @create: 2019-05-20 23:40
 */
@Configuration
public class RedisConfig {

    /**
     * 1.創(chuàng  )建JedisPoolConfig對象。在該對象中完成一些鏈接池配置
     * @ConfigurationProperties:會(huì )將前綴相同的內容創(chuàng  )建一個(gè)實(shí)體。
     */
    @Bean
    @ConfigurationProperties(prefix="spring.redis.pool")
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
		/*//最大空閑數
		config.setMaxIdle(10);
		//最小空閑數
		config.setMinIdle(5);
		//最大鏈接數
		config.setMaxTotal(20);*/
        System.out.println("默認值:"+config.getMaxIdle());
        System.out.println("默認值:"+config.getMinIdle());
        System.out.println("默認值:"+config.getMaxTotal());
        return config;
    }

    /**
     * 2.創(chuàng  )建JedisConnectionFactory:配置redis鏈接信息
     */
    @Bean
    @ConfigurationProperties(prefix="spring.redis")
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
        System.out.println("配置完畢:"+config.getMaxIdle());
        System.out.println("配置完畢:"+config.getMinIdle());
        System.out.println("配置完畢:"+config.getMaxTotal());

        JedisConnectionFactory factory = new JedisConnectionFactory();
        //關(guān)聯(lián)鏈接池的配置對象
        factory.setPoolConfig(config);
        //配置鏈接Redis的信息
        //主機地址
		/*factory.setHostName("192.168.70.128");
		//端口
		factory.setPort(6379);*/
        return factory;
    }

    /**
     * 3.創(chuàng  )建RedisTemplate:用于執行Redis操作的方法
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        //關(guān)聯(lián)
        template.setConnectionFactory(factory);

        //為key設置序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //為value設置序列化器
        template.setValueSerializer(new StringRedisSerializer());

        return template;
    }
}

4.添加pojo

/**
 * @program: springboot-redis-demo
 * @description: Users
 * @author: 波波烤鴨
 * @create: 2019-05-20 23:47
 */
public class Users implements Serializable {

    private Integer id;
    private String name;
    private Integer 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 "Users [id=" + id + ", name=" + name + ", age=" + age + "]";
    }

}

5.單元測試

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootRedisDemoApplication.class)
public class SpringbootRedisDemoApplicationTests {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 添加一個(gè)字符串
     */
    @Test
    public void testSet(){
        this.redisTemplate.opsForValue().set("key", "bobokaoya...");
    }

    /**
     * 獲取一個(gè)字符串
     */
    @Test
    public void testGet(){
        String value = (String)this.redisTemplate.opsForValue().get("key");
        System.out.println(value);
    }

    /**
     * 添加Users對象
     */
    @Test
    public void testSetUesrs(){
        Users users = new Users();
        users.setAge(20);
        users.setName("張三豐");
        users.setId(1);
        //重新設置序列化器
        this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        this.redisTemplate.opsForValue().set("users", users);
    }

    /**
     * 取Users對象
     */
    @Test
    public void testGetUsers(){
        //重新設置序列化器
        this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        Users users = (Users)this.redisTemplate.opsForValue().get("users");
        System.out.println(users);
    }

    /**
     * 基于JSON格式存Users對象
     */
    @Test
    public void testSetUsersUseJSON(){
        Users users = new Users();
        users.setAge(20);
        users.setName("李四豐");
        users.setId(1);
        this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
        this.redisTemplate.opsForValue().set("users_json", users);
    }

    /**
     * 基于JSON格式取Users對象
     */
    @Test
    public void testGetUseJSON(){
        this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
        Users users = (Users)this.redisTemplate.opsForValue().get("users_json");
        System.out.println(users);
    }

}

到此這篇關(guān)于SpringBoot整合SpringDataRedis的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot整合SpringDataRedis內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

日韩一区二区在线观看视频| 亚洲精品无码不卡在线播HE| 男人把女人桶爽30分钟| 国产aⅴ无码专区亚洲av| 亚洲最大激情中文字幕| 成人综合婷婷国产精品久久蜜臀|