第一步:在porm.xml文件中引入下面的jar
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
第二步:写配置文件(local-redis.properties)
#master IP
redis.master.host=192.168.4.92
#master Port
redis.master.port=6379
#slave IP
redis.slaver.host=192.168.4.92
#slave Port
redis.slaver.port=6379
#最大分配的对象数
redis.pool.maxTotal=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWaitMillis=60000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#多长时间检查一次连接池中空闲的连接
redis.pool.timeBetweenEvictionRunsMillis=30000
#空闲连接多长时间后会被收回
redis.pool.minEvictableIdleTimeMillis=30000
redis.pool.softMinEvictableIdleTimeMillis=10000
redis.pool.numTestsPerEvictionRun=1024
#缓存过期时间 秒 1000*60*60*24*7 七天
#redis.pool.expire=604800000
#是否开启Redis服务应用
redis.pool.unlock=false
redis.pool.testWhileIdle=true
redis.pool.testOnReturn=true
redis.pool.jmxEnabled=true
redis.pool.blockWhenExhausted=false
第三步:写配置文件(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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 加载公用的jdbc和db配置文件 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:redis.properties</value>
</list>
</property>
</bean> -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}" />
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" />
<property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}" />
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
<property name="jmxEnabled" value="${redis.pool.jmxEnabled}" />
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}" />
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean name="slaver" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.slaver.host}" />
<constructor-arg index="1" value="${redis.slaver.port}"
type="int" />
</bean>
<bean name="master" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.master.host}" />
<constructor-arg index="1" value="${redis.master.port}"
type="int" />
</bean>
</list>
</constructor-arg>
</bean>
</beans>
第四步:依赖注入(在spring.xml中注入redis的对象)
//注入
<import resource="redis.xml"/>
//扫描
<context:component-scan base-package="com.hiersun.ecommerce.redis">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
第五步:编写redis的servicejava文件(RedisBaseService.java,实现类RedisBaseServiceImpl.java)
测试示例:(将session对象插入到redis中(就是存到内存中))
//SET Session
HttpSession session = request.getSession();
session.setAttribute(WebCnts.SESSION_KEY_LOGINUSER, user);
redisBaseService.setObj(String.valueOf(user.getId()), user);
SessionUser sUser = (SessionUser)redisBaseService.getObj(String.valueOf(user.getId()));
logger.debug("Login>>>>>>>sUserName = "+sUser.getUname());
分享到:
相关推荐
该项目主要技术架构包括:spring+springMVC+Mybatis+MYSQL+Redis+Maven,适合初学者,简单易懂。
【项目原型】spring+spring mvc+mybatis+shiro+maven+bootstrap+ajax+json+分页+逆向工程 包含现在最流行的技术框架,快速部署各种应用,加入shiro权限框架,安全,美观,你值得拥有
框架是Spring+SpringMvc+Mybatis+maven+redis。前端是妹子UI。 里面的功能包括: 1、权限管理,可动态加载3级菜单,利用ztree 加载权限,可进行修改。 2、文件管理,客户端上传文件到服务器,服务器将文件上传到ftp...
基于maven+springmvc+redis+mybatis整合案例框架,主要实现redis的读取案例
项目描述 说明: spring security 全注解式的权限管理 动态配置权限,角色和资源,权限控制到...Springboot+Mybatis+ SpringMvc+springsecrity+Redis+bootstrap+jquery 数据库文件 压缩包内 jar包文件 maven搭建
使用maven简单搭建Spring mvc + redis缓存 个人实验成功
springboot、Dubbo、MySQL,源码web系统,框架,代码均经过严格测试,可直接运行,有需要可自取
1、基于Spring + Spring MVC + Mybatis的销售管理系统源码+项目说明.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为...
简讯基础框架Spring + Spring MVC + Mybatis分布式框架dubbo消息系统缓存redis构建工具
springMVC+spring+mybatis+redis整合的maven demo,redis作为二级缓存
vue + spring boot + spring cloud + maven + nacos + getway + redis + rabbitMQ + MyBatis-Plus 基于Spring cloud + vue 的前后端分离社团管理系统,因学校没有成熟的社团管理系统,一为满足学校需求, 其次开源...
SSM(Spring + Spring MVC + MyBatis)框架作为Java开发中的黄金组合,为开发者提供了强大的技术支持和丰富的功能。本系列资料将带您从零基础开始,逐步掌握SSM的核心技术和最佳实践,助您在Java Web开发领域更上一...
springmvc 的平台架构框架。开发环境 eclipse + maven ,插件lombok, 嵌入了redis
SSM(Spring + Spring MVC + MyBatis)框架作为Java开发中的黄金组合,为开发者提供了强大的技术支持和丰富的功能。本系列资料将带您从零基础开始,逐步掌握SSM的核心技术和最佳实践,助您在Java Web开发领域更上一...
java后台权限管理系统源码(spring boot+mvc+mybatis)。 项目是maven,相应的jar包也在压缩包里。项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器)...
这是一个混合的实例 ,springmvc的maven项目。包含redis实例,dubbo实例,rabbitmq实例。
Spinrg+SpirngMvc+Mybatis+Maven整合项目,打开即用,使用者只需要更改其中的配置即可,
本项目是一个基于Spring MVC + Spring + Mybatis + Redis + Maven的入门级(拥有完整的登录/注册逻辑)。Demo,是在SSM-Demo项目的基础上修改,调整而来,主要涉及以下几项技术及知识点: 使用SpringMVC作MVC控制器...
博客源码主要涉及技术包括的包括 Maven、Spring、SpringMVC、MyBatis、Redis、JSP等。后台地址:/admin 或者 /login 技术栈 SpringMVC + Spring + MyBatis 运行环境 IDEA + Tomcat8以上 + MySQL5.5以上 注意事项 ...