最后我建议大家使用

Spring StringRedisTemplate 配置,参阅:

这次,我们配置一个进阶版本的教程。

功能:1  带有密码
           2  分库

先看配置文件以及目录:

spring 文件夹:

applicationContext.xml  总配置文件

spring-business.xml  数据库配置文件(redis也在这里!)

springmvc-servlet.xml  spring配置文件

redis.properties  你懂得,redis属性配置

一  redis.properties

 
# Redis settings
redis.host=192.168.1.88
redis.port=6379
redis.timeOut=10000
# 密码留空很有必要
redis.pass=
redis.maxIdle=300
redis.maxTotal=1024
redis.maxWaitMillis=10000
redis.testOnBorrow=true
# 设置使用的数据库
redis.database=1

redis.pass 密码可以为空,但是必须要写。(这是由于启用分库database的功能,redis的构造函数就需要密码了)

database 选择的数据库(分库)

二  spring-business.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 通常来说,只需要修改initialSize、minIdle、maxActive。 如果用Oracle,则把poolPreparedStatements配置为true,
mysql可以配置为false。分库分表较多的数据库,建议配置为false。 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.jdbcUrl}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:mybatis/mybatisConfig.xml</value>
</property>
<property name="mapperLocations">
<list>
<value>classpath:mybatis/mapper/*.xml</value>
</list>
</property> </bean> <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.sanju.sanjuSCM.dao"/>
</bean> <!-- 启用注解按需添加事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/> <!--redis配置 -->
<bean name="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean> <bean name="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
<constructor-arg name="host" value="${redis.host}"/>
<constructor-arg name="port" value="${redis.port}"/>
<constructor-arg name="timeout" value="${redis.timeOut}"/>
<constructor-arg name="password" value="#{'${redis.pass}'!=''?'${redis.pass}':null}"/>
<constructor-arg name="database" value="${redis.database}"/>
</bean> <bean name="redisHelper" class="com.sanju.sanjuSCM.utils.redisHelper.RedisHelper">
<constructor-arg index="0" ref="jedisPool"/>
</bean>
</beans>

1

这里需要注意的就是:由于我们密码为空,所以这么写

<constructor-arg name="password" value="#{'${redis.pass}'!=''?'${redis.pass}':null}"/>

密码不为空:

<constructor-arg name="password" value="${redis.pass}"/>

2

jedisPoolConfig 和  jedisPool 的配置没什么好说的了,

看一下 redisHelper,这个的class就是RedisHelper的全名+类名。

package com.sanju.sanjuSCM.utils.redisHelper;

import com.sanju.sanjuSCM.model.Token.apiToken;
import com.sanju.sanjuSCM.utils.MD5;
import com.sanju.sanjuSCM.utils.jsonUtil.JsonConvert;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import java.util.Date;
import java.util.Random; /**
* Created by Tyler on 2017/7/4.
*/ public class RedisHelper { private JedisPool jedisPool;
private Jedis jedis; public RedisHelper(){ } public RedisHelper(JedisPool jedisPool){
this.jedisPool=jedisPool;
this.jedis=this.jedisPool.getResource();
} }

其中的方法我都删除了,大家可自行添加方法。

三  web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://Java.sun.com/xml/ns/j2ee " xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param> <servlet>
<servlet-name>sanjuSCM</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc-servlet.xml</param-value>
</init-param> <!-- 取消其自动注册的异常解析器 -->
<init-param>
<param-name>detectAllHandlerExceptionResolvers</param-name>
<param-value>false</param-value>
</init-param> <load-on-startup>1</load-on-startup>
</servlet> </web-app>

最后看一下调用:

package com.sanju.sanjuSCM.app.controller;

import com.sanju.sanjuSCM.commons.ApiResult;
import com.sanju.sanjuSCM.commons.BaseResult;
import com.sanju.sanjuSCM.model.SysUser;
import com.sanju.sanjuSCM.utils.redisHelper.RedisHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @RestController
@RequestMapping("api/test")
public class TestAPIController extends BaseClassAPIController { @Autowired
RedisHelper redisHelper; @RequestMapping(value = "test", method = RequestMethod.POST)
public BaseResult test(HttpServletRequest request, HttpServletResponse response, @RequestBody SysUser user) throws Exception { String p=redisHelper.GetToken("1"); ApiResult rm = new ApiResult();
return rm;
} }

GetToken是我redisHelper的方法。

PS:我参考了  http://www.cnblogs.com/woshimrf/p/5211253.html

我想说,看了网上很多方案,都是拷贝来拷贝去。这一篇是值得一看的。赞一下

ssm 整合 redis(进阶教程)的更多相关文章

  1. ssm 整合 redis(简单教程)

    最后我建议大家使用 Spring StringRedisTemplate 配置,参阅: http://blog.csdn.net/hanjun0612/article/details/78131333 ...

  2. SSM整合Redis

    前言 服务端缓存的意义大多数在于减轻数据库压力,提供响应速度,而缺点也是显而易见的,会带来缓存与数据库一致性问题.当然,Redis还可以作为分布式锁. Redis 想在项目中使用Redis需要做的事情 ...

  3. SSM之整合Redis

    Redis安装与使用 第一步当然是安装Redis,这里以Windows上的安装为例. 首先下载Redis,可以选择msi或zip包安装方式 zip方式需打开cmd窗口,在解压后的目录下运行redis- ...

  4. Spring+SpringMVC+Mybatis整合redis

    SSM整合redis redis是一种非关系型数据库,与mongoDB不同的是redis是内存数据库,所以访问速度很快.常用作缓存和发布-订阅式的消息队列. 这里用的是ssm框架+maven构建的项目 ...

  5. SpringBoot进阶教程(二十九)整合Redis 发布订阅

    SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...

  6. SpringBoot进阶教程(二十七)整合Redis之分布式锁

    在之前的一篇文章(<Java分布式锁,搞懂分布式锁实现看这篇文章就对了>),已经介绍过几种java分布式锁,今天来个Redis分布式锁的demo.redis 现在已经成为系统缓存的必备组件 ...

  7. SpringBoot进阶教程(二十八)整合Redis事物

    Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用.这样做会强制将当前重新连接绑定到触发m ...

  8. SpringBoot进阶教程(二十六)整合Redis之共享Session

    集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...

  9. SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用

    在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...

随机推荐

  1. async源码学习 - 控制流程waterfall函数

    waterfall函数会连续执行数组中的函数,每次通过数组下一个函数的结果.然而,数组任务中的任意一个函数结果传递失败,那么该函数的下一个函数将不会执行,并且主回调函数立马把错误作为参数执行. 以上是 ...

  2. Omi框架学习之旅 - 组件通讯(data通讯) 及原理说明

    接着上一篇的data-*通讯,这篇写data通讯. data通讯主要为了复杂的数据通讯. 老规矩:先上demo代码, 然后提出问题, 之后解答问题, 最后源码说明. class Hello exten ...

  3. CYJian的水题大赛

    实在没忍住就去打比赛了然后一耗就是一天 最后Rank19还是挺好的(要不是乐多赛不然炸飞),这是唯一一套在Luogu上号称水题大赛的而实际上真的是水题大赛的比赛 好了我们开始看题 T1 八百标兵奔北坡 ...

  4. [Spark][Python]DataFrame select 操作例子

    [Spark][Python]DataFrame中取出有限个记录的例子 的 继续 In [4]: peopleDF.select("age")Out[4]: DataFrame[a ...

  5. linux的convert图片处理工具

    得到一个图片的尺寸, identify test.png 结果为: test.png PNG 178x15 178x15+0+0 16-bit PseudoClass 65536c 2.28kb 使用 ...

  6. Ubuntu 打包后安装提示:子进程 已安装 pre-removal 脚本 返回了错误号 1

    子进程 已安装 pre-removal 脚本 返回了错误号 1或2 与 子进程 已安装 post-installation 脚本 返回了错误号 1或2   一.子进程 已安装 pre-removal  ...

  7. Jlink使用技巧之烧写SPI Flash存储芯片

    前言 大多数玩单片机的人都知道Jlink可以烧写Hex文件,作为ARM仿真调试器,但是知道能烧写SPI Flash的人应该不多,本篇文章将介绍如何使用JLink来烧写或者读取SPI Flash存储器, ...

  8. 过渡与动画 - 缓动效果&基于贝塞尔曲线的调速函数

    难题 给过渡和动画加上缓动效果是一种常见的手法(比如具有回弹效果的过渡过程)是一种流行的表现手法,可以让界面显得更加生动和真实:在现实世界中,物体A点到B点往往也是不完全匀速的 以纯技术的角度来看,回 ...

  9. BugkuCTF 域名解析

    前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...

  10. handlebars.js 自定义helper(过滤)

    将对象数据渲染到页面上: id 插入公共样式: handlebars.js 自定义helper(过滤)demo <script id="tbody-content-template&q ...