ssm 整合 redis(进阶教程)
最后我建议大家使用
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(进阶教程)的更多相关文章
- ssm 整合 redis(简单教程)
最后我建议大家使用 Spring StringRedisTemplate 配置,参阅: http://blog.csdn.net/hanjun0612/article/details/78131333 ...
- SSM整合Redis
前言 服务端缓存的意义大多数在于减轻数据库压力,提供响应速度,而缺点也是显而易见的,会带来缓存与数据库一致性问题.当然,Redis还可以作为分布式锁. Redis 想在项目中使用Redis需要做的事情 ...
- SSM之整合Redis
Redis安装与使用 第一步当然是安装Redis,这里以Windows上的安装为例. 首先下载Redis,可以选择msi或zip包安装方式 zip方式需打开cmd窗口,在解压后的目录下运行redis- ...
- Spring+SpringMVC+Mybatis整合redis
SSM整合redis redis是一种非关系型数据库,与mongoDB不同的是redis是内存数据库,所以访问速度很快.常用作缓存和发布-订阅式的消息队列. 这里用的是ssm框架+maven构建的项目 ...
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...
- SpringBoot进阶教程(二十七)整合Redis之分布式锁
在之前的一篇文章(<Java分布式锁,搞懂分布式锁实现看这篇文章就对了>),已经介绍过几种java分布式锁,今天来个Redis分布式锁的demo.redis 现在已经成为系统缓存的必备组件 ...
- SpringBoot进阶教程(二十八)整合Redis事物
Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用.这样做会强制将当前重新连接绑定到触发m ...
- SpringBoot进阶教程(二十六)整合Redis之共享Session
集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...
- SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用
在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...
随机推荐
- Python学习总结 05 pandas
pandas官方网址 : http://pandas.pydata.org/ . pandas的安装比较复杂,如果想开箱即用,可以考虑下载WinPython.WinPython的官方地址是: htt ...
- Luogu4156 WC2016 论战捆竹竿 KMP、同余类最短路、背包、单调队列
传送门 豪华升级版同余类最短路-- 官方题解 主要写几个小trick: \(1.O(nm)\)实现同余类最短路: 设某一条边长度为\(x\),那么我们选择一个点,在同余类上不断跳\(x\),可以形成一 ...
- BZOJ3252 攻略 贪心、长链剖分
传送门 给树竟直接给父子关系!!!真良心 首先一个贪心策略:每一次选择的链一定是所有链中权值最大的.这应该比较显然 那么我们接下来考虑如何维护这个贪心.我们可以使用长链剖分进行维护,对权值进行长链剖分 ...
- $\mathfrak {reputation}$
\(\mathfrak {reputation}\) 举世盛名 身败名裂
- 案例学python——案例二:连接数据库MySql
调侃的话:案例一跑完之后,欣赏把玩了一番.人就有点飘飘然,昨天除了做饭吃饭,就是玩三国杀,江郎才尽,今天周一,不飘了,敲点代码,看看Python操作数据库有啥不一样的. 前期准备: 1.数据库 电脑上 ...
- element-ui + vue + node.js 与 服务器 Python 应用的跨域问题
跨越问题解决的两种办法: 1. 在 config => index.js 中配置 proxyTable 代理: proxyTable: { '/charts': { target: 'http: ...
- E. Train Hard, Win Easy
链接 [http://codeforces.com/contest/1043/problem/E] 题意 有n个人,每个人都有做出a,b题的分数,xi,yi,但是有些人是不能组队的,问你每个人和其他能 ...
- 20135337——linux第四次实践:字符集总结与分析
ASCII & GB2312 & UTF-8 ASCII 主要用于显示现代英语和其他西欧语言.它是现今最通用的单字节编码系统,并等同于国际标准ISO 646: 7位(bits)表示一个 ...
- 201303014001 张敏 计科高职13-1 github使用心得
Github:https://github.com/zhangmin131/text 个人心得体会: Git是一种良好的.支持分支管理的代码管理方式,能很好地解决团队之间协作的问题.每个工程师在自己本 ...
- android 活动的生命周期
掌握活动的生命周期非常重要,因为一个正常的android应用,会有很多的活动,如何在这些活动之间进行切换.数据的交互等,就经常会用到活动的生命周期这一块的知识.可以说,只要掌握了活动的生命周期,才能更 ...