依赖

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

application.yml

spring:
redis:
# 指的是默认操作redis数据库中的db1
database: 0
host: 127.0.0.1
port: 6379
password: 123456
timeout: 0
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0

测试代码

package com.vast;

import com.vast.dao.AccountRepository;
import com.vast.dao.IAccountMybatisDao;
import com.vast.entity.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner; import java.util.concurrent.TimeUnit; @RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationVastStart.class)
public class TestAccountService { @Autowired
private IAccountMybatisDao accountMybatisDao;
@Autowired
private AccountRepository accountRepository; @Autowired
private StringRedisTemplate stringRedisTemplate; @Test
public void TestSaveAccount(){
Account account = new Account();
account.setName("222");
account.setMoney(23.9);
//// accountMybatisDao.saveAccount(account);
// accountRepository.findByName("");
// System.out.println(accountRepository.findAll());
// System.out.println(accountRepository.insert(account)); // 测试Redis
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
stringStringValueOperations.set("name","张三", 1, TimeUnit.MINUTES);//1分钟过期
        //JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) stringRedisTemplate.getConnectionFactory();
// 切换数据库为db1
//jedisConnectionFactory.setDatabase(1);
//stringRedisTemplate.setConnectionFactory(jedisConnectionFactory);

System.out.println(stringStringValueOperations.get("name"));
}
}

扩展

Windows中,把redis-server.exe注册成服务命令

redis-server.exe --service-install redis.windows.conf

另一种redis连接方式

public class RedisUtil {

    //服务器IP地址
private static String ADDR = "192.168.41.65";
//端口
private static int PORT = 6379;
//密码
private static String AUTH = "123456";
//连接实例的最大连接数
private static int MAX_ACTIVE = 1024;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
private static int MAX_WAIT = 10000;
//连接超时的时间  
private static int TIMEOUT = 10000;
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true; private static JedisPool jedisPool = null;
//数据库模式是16个数据库 0~15
public static final int DEFAULT_DATABASE = 0;
/**
* 初始化Redis连接池
*/ static { try { JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT,AUTH,DEFAULT_DATABASE); } catch (Exception e) { e.printStackTrace();
} } /**
* 获取Jedis实例
*/ public synchronized static Jedis getJedis() { try { if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
System.out.println("redis--服务正在运行: "+resource.ping());
return resource;
} else {
return null;
} } catch (Exception e) {
e.printStackTrace();
return null;
} } /***
*
* 释放资源
*/ public static void returnResource(final Jedis jedis) {
if(jedis != null) {
jedisPool.returnResource(jedis);
} }
}

SpringBoot30 整合Mybatis-Plus、整合Redis、利用Ehcache实现二级缓存、利用SpringCache和Redis作为缓存

SpringBoot入门-Redis(六)的更多相关文章

  1. SpringBoot入门教程(六)SpringBoot2.0统一处理404,500等http错误跳转页

    在做web项目的时候,大家对404.500等http状态码肯定并不陌生.然而无论是哪种"非正常"状态码,都不是我们想遇到的.尤其像一些500这种服务器内部错误,不愿意展示给用户的, ...

  2. SpringBoot入门 (七) Redis访问操作

    本文记录学习在SpringBoot中使用Redis. 一 什么是Redis Redis 是一个速度非常快的非关系数据库(Non-Relational Database),它可以存储键(Key)与 多种 ...

  3. (入门SpringBoot)SpringBoot结合redis(四)

    SpringBoot整合redis: 1.引入jar <!--  引入redis依赖 --><dependency>    <groupId>org.springf ...

  4. Springboot整合Redis入门完整篇,零基础入门教学教程

    记录一次简易集成Redis缓存 自定义Redisconfig配置 自定义序列化操作 加深印像 整合前提工具环境准备: 1.redis官网 https://redis.io/download 下载安装r ...

  5. SpringBoot入门系列(七)Spring Boot整合Redis缓存

    前面介绍了Spring Boot 中的整合Mybatis并实现增删改查,.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/ ...

  6. SpringBoot入门基础

    目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...

  7. SpringBoot入门及深入

    一:SpringBoot简介 当前互联网后端开发中,JavaEE占据了主导地位.对JavaEE开发,首选框架是Spring框架.在传统的Spring开发中,需要使用大量的与业务无关的XML配置才能使S ...

  8. Docker 入门 第六部分:部署app

    目录 Docker 入门 第六部分:部署app 先决条件 介绍 选择一个选项 Docker CE(Cloud provider) Enterprise(Cloud provider)这里不做介绍 En ...

  9. springboot整合redis——redisTemplate的使用

    一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...

随机推荐

  1. gitlab安装配置

    一.安装依赖服务 yum install curl policycoreutils-python openssh-server postfix -y systemctl start postfix s ...

  2. 缺jar包异常:java.lang.NoClassDefFoundError: org/springframework/core/convert/support/PropertyTypeDescriptor

    严重: StandardWrapper.Throwable java.lang.NoClassDefFoundError: org/springframework/core/convert/suppo ...

  3. mysql-xtrabackup备份恢复

    1.xtrabackup的安装 8.0版本-支持mysql8 wget https://www.percona.com/downloads/Percona-XtraBackup-LATEST/Perc ...

  4. python小案例-计算输入两个数的最大公约数与最小公倍数

    # 计算最大公约数 def gcd(x,y): """ 计算最大公约数 :param x:一个正整数 :param y:一个正整数 :return:x,y的最大公约数 & ...

  5. 【Postgres】根据字段数据创建空间字段

    --添加空间字段 , ); --根据其他字段更新空间字段数据 update "GIS" b ) from "GIS" a where b."ID&qu ...

  6. 测试mybatis延迟加载错误与解决方法

    什么是延迟加载? 延迟加载又叫懒加载,也叫按需加载,也就是说先加载主信息,需要的时候,再去加载从信息. 需求: 查询订单信息,需要时再去查询用户信息 实现方式: 编写两个statement,其中一个s ...

  7. Async programming

    Asynchrony, in computer programming, refers to the occurrence of events independent of the mainprogr ...

  8. Linux中的查找与替换

    grep只能用于查找文件中的内容sed可以查找,然后替换或者插入想要的内容 a :新增,a的后面可以接字串,而这些字串会在新的一行出现(目前的下一行):d :删除,因为是删除啊,所以d后面通常不接任何 ...

  9. A* 第k短路

    #include <cstdio> #include <algorithm> #include <queue> #include <cstring> # ...

  10. LeetCode 1100. Find K-Length Substrings With No Repeated Characters

    原题链接在这里:https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/ 题目: Give ...