Spring + SpringMVC + Mybatis项目中redis的配置及使用
maven文件
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis.version}</version>
</dependency>
1.redis配置文件redis.properties
#redis的服务器地址
redis.host=127.0.0.1
#redis的服务端口
redis.port=6379
#密码
redis.password=
#链接数据库
redis.default.db=0
#客户端超时时间单位是毫秒
redis.timeout=100000
#最大连接数
redis.maxActive=300
#最大空闲数
redis.maxIdle=100
#最大建立连接等待时间
redis.maxWaitMillis=1000
2.spring-redis.xml读取参数
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:property-placeholder location="classpath:redis.properties"/>
<!-- Redis 配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxActive}"/>
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="true"/>
</bean> <!-- redis单节点数据库连接配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.password}"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean> <!-- redisTemplate配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
</bean>
</beans>
3.web.xml中读取spring配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-*.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
这样redis在项目中就算配好了
4.redis工具类
package com.mz.usps.common.component; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; import javax.annotation.Resource;
import java.util.concurrent.TimeUnit; /**
* Redis模板
*
* @author sjl
* @date 2018-03-12 15:18
**/
@Component
public class RedisCache {
@Resource
private RedisTemplate redisTemplate; public void setValue(Object key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public void setValue(Object key, Object value, long timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, value, timeout, unit);
} public Object getValue(Object key) {
Object value = redisTemplate.opsForValue().get(key);
return value;
}
}
5.Redis使用实战
在哪个类中使用
@Autowired
RedisCache redisCache;
加到哪个类中
6.方法中存值
redisCache.setValue(token, "1", 1, TimeUnit.DAYS);
token为key值 "1"为value 1时间 TimeUnit.DAYS为时间单位
将key value 存入redis中一天
Spring + SpringMVC + Mybatis项目中redis的配置及使用的更多相关文章
- IDEA中maven搭建Spring+SpringMVC+mybatis项目
一.介绍 使用IDEA搭建maven web项目,整合框架Spring+SpringMVC+mybatis 项目结构图:
- 用 eclipse 创建一个简单的 meaven spring springMvc mybatis 项目
下面是整体步骤: 1: 先创建一个Maven 项目: 选择跳过骨架: 因为要搭建的是 web 项目 所以这个地方选择 war 包; 点击完成 这样就完成 Maven项目的搭建: 接下俩 先把 Mav ...
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释(转)
原文:https://blog.csdn.net/yijiemamin/article/details/51156189# 这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文 ...
- 0927-转载:SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释
这篇文章暂时只对框架中所要用到的配置文件进行解释说明,而且是针对注解形式的,框架运转的具体流程过两天再进行总结. spring+springmvc+mybatis框架中用到了三个XML配置文件:web ...
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释
这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿 ...
- SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例
要搭建的项目的项目结构如下(使用的框架为:Spring.SpingMVC.MyBatis): 2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包): 其中 ...
- Spring+SpringMVC+MyBatis+Maven 服务端XML配置
项目目录结构 spring-mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- spring +springmvc+mybatis组合applicationContext.xml文件配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- spring +springmvc+mybatis组合mybatis-config.xml文件配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC &q ...
随机推荐
- codeforces 1151 B
codeforces 1151 B codeforces 1151 B 1600fen 题意:n*m的矩阵,问能否从n行中每行选一个数 异或 大于0 解析:刚开始看没思路,想用dfs跑一遍,看到 ...
- zimbra6同域名与同hostname与同系统异机恢复
系统:redhat5.4_64 安装DNS:[root@test6 ~]# yum install bind -y[root@test6 ~]# yum install bind-chroot -y[ ...
- 自动备份远程mongodb数据库并拉取到本地
自动备份远程mongodb数据库并拉取到本地 目标: 远程服务器 .1中的mongodb数据拉回公司测试服务器中 .远程服务器中编写自动备份mongodb脚本 ①编写脚本 # vim /opt/bac ...
- [Linux]返回被阻塞的信号集
一.概述 在另一篇实例说到,进程可以屏蔽它不想接收的信号集. 事实上这些被屏蔽的信号只是阻塞在内核的进程表中,因为他们不能递送给进程,所以状态是未决的(pending). 利用sigpending函数 ...
- 处理 oracle 数据库导入报错“IMP-00058: 遇到 ORACLE 错误 942”
在导入数据文件的时候出现了下图错误: 经过多次百度搜索问题.得知问题错误方向: 仔细的查询了被导入数据的数据库的版本: 而 被导入的数据包 dmp 文件是从 oracle11g r2的版本导出的. 所 ...
- loadrunner使用过程遇到的问题(一)
1.如果log日志出现乱码,如何设置?(对于乱码设置只是对单一脚本有效,并不是全部脚本有效,所以多个脚本出现乱码,需要逐个设置) loadrunner12版本设置方法,在preference里面,设置 ...
- 用Python写一个zip文件的密码破解程序
最近在读<python绝技:运用python成为顶级黑客>一书,文中有如何运用Python中zipfile自带的方法破解zip文件.短短的十几行代码就将一个程序实现了.下面给出书中所用的代 ...
- Android Q 变更和新特性
安全和隐私变更 隐私保护是Android Q重要的主题之一,Android Q带来了一系列增强用户隐私保护的变更. 1 应用文件存储空间限制 应用访问限制是Android Q影响最大变更之一.在And ...
- Spring boot实现原生websocket
网上的大部分教程是基于sockjs,这篇文章内容则是基于原生协议. 后台Spring boot 配置 @Configuration @EnableWebSocket public class WebS ...
- JS 实现blob与base64互转
/** * base64 to blob二进制 */ function dataURItoBlob(dataURI) { var mimeString = dataURI.split(',')[0]. ...