SpringBoot入门-Redis(六)
依赖
<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(六)的更多相关文章
- SpringBoot入门教程(六)SpringBoot2.0统一处理404,500等http错误跳转页
在做web项目的时候,大家对404.500等http状态码肯定并不陌生.然而无论是哪种"非正常"状态码,都不是我们想遇到的.尤其像一些500这种服务器内部错误,不愿意展示给用户的, ...
- SpringBoot入门 (七) Redis访问操作
本文记录学习在SpringBoot中使用Redis. 一 什么是Redis Redis 是一个速度非常快的非关系数据库(Non-Relational Database),它可以存储键(Key)与 多种 ...
- (入门SpringBoot)SpringBoot结合redis(四)
SpringBoot整合redis: 1.引入jar <!-- 引入redis依赖 --><dependency> <groupId>org.springf ...
- Springboot整合Redis入门完整篇,零基础入门教学教程
记录一次简易集成Redis缓存 自定义Redisconfig配置 自定义序列化操作 加深印像 整合前提工具环境准备: 1.redis官网 https://redis.io/download 下载安装r ...
- SpringBoot入门系列(七)Spring Boot整合Redis缓存
前面介绍了Spring Boot 中的整合Mybatis并实现增删改查,.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/ ...
- SpringBoot入门基础
目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...
- SpringBoot入门及深入
一:SpringBoot简介 当前互联网后端开发中,JavaEE占据了主导地位.对JavaEE开发,首选框架是Spring框架.在传统的Spring开发中,需要使用大量的与业务无关的XML配置才能使S ...
- Docker 入门 第六部分:部署app
目录 Docker 入门 第六部分:部署app 先决条件 介绍 选择一个选项 Docker CE(Cloud provider) Enterprise(Cloud provider)这里不做介绍 En ...
- springboot整合redis——redisTemplate的使用
一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...
随机推荐
- zabbix--监控MySQL主从状态
zabbix监控MySQL主从状态 搭建MySQL主从后,很多时候不知道从的状态是否ok,有时候出现异常不能及时知道,这里通过shell脚本结合zabbix实现监控并告警 一般情况下,在MySQL的从 ...
- 初识Java(Java数字处理类-大数字运算)
一.大数字运算 在 Java 中提供了大数字的操作类,即 java.math.BigInteger 类与 java.math.BigDecimal 类.这两个类用于高精度计算,体重 BigInteg ...
- Linux下如何退出vim的一些常用命令总结
1.保存并退出 linux下安装好了vim以及gcc后,我们开始新建一个c文件,例如: vim test.c 之后进入vim的编辑框中,点击i进入插入模式,开始编辑程序,当你编写好自己的程序之后,按E ...
- firefox修改user-agent
让firefox对web服务器伪装成任意浏览器,找一个iphone的useragent,瞬间firefox变身iPhone有木有,一般人我不告诉他嘿嘿 1.firefox地址栏中输入about:con ...
- Excel——读取——导出目录
/** * 导出Excel文件到具体的目录 * <一句话功能简述> * <功能详细描述> * @param fileName 导出的文件名 * @param sheetName ...
- ajax jsonp函数调用
jsonp数据 jsonpHandler({name:"liujinyu",age:"24"}) ajax调用 $.ajax({ type:'GET', ...
- 关于vue+axios上传文件的踩坑分析
上传文件是每个前端开发者都会遇到的问题,在之前实习期做了一个上传文件的功能,当时没有彻底搞明白问题所在,现在重新复盘下. 1.使用formData来上传文件,没有使用axios上传文件,之前在学校有做 ...
- The Difference between Gamification and Game-Based Learning
http://inservice.ascd.org/the-difference-between-gamification-and-game-based-learning/ Have you trie ...
- 在IAR平台建立STC8ASK64S4A12单片机工程
转载:http://www.51hei.com/bbs/forum.php?mod=viewthread&tid=168481&page=1#pid737250 一般我们使用STC单 ...
- [Schematics] 2. EJS
Schematices using EJS as template language. template: <%# This will not appear in the generated o ...