ssm 整合 redis(进阶教程)

时间:2023-01-14 07:07:49

最后我建议大家使用

Spring StringRedisTemplate 配置,参阅:

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

功能:1  带有密码
           2  分库

先看配置文件以及目录:

ssm 整合 redis(进阶教程)

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

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