一、关于SSM整合Redis需要知道的一些小知识
在学习Redis的时候,大家应该知道,JAVA操作redis通常使用的是Jedis,通过java代码来操作redis的数据存储读取等操作,用过的人应该知道,Jedis客户端已经足够简单和轻量级了,但是呢,在此同时,Spring也为Redis提供了支持,就是在Spring-data模块中的Spring-Data-Redis(SDR),它一部分是基于Jedis客户端的API封装,另一部分是对Spring容器的整合。
大家应该都知道,Spring容器可以作为一个大工厂,为各种对象创建实例,关于SSM与Redis的整合,准确点,其实也就是Spring与Redis的整合。
又说会来了,Spring要和Redis整合需要用到的,就是Spring-Data模块中的Spring-data-Redis。
二、SDR资料(Spring-Data-Redis)
资料:http://projects.spring.io/spring-data-redis/
文档:https://docs.spring.io/spring-data/redis/docs/1.8.6.RELEASE/reference/html/
个人觉得没有什么比官方文档说的更为详细的了
这里对其功能做一个简单的说明:
1、连接池自动管理,并且提供了RedisTemplate类(很重要)
2、将jedis的API按照类型操作,进行封装
其中Key Bound Operations ,是对key绑定便携化的接口,什么意思呢,就是,原先我们在存放数据的时候,需要指定一个key,然后根据这个key修改,删除啊之类操作,但是无论是修改还是删除都要重新填写使用这个key,而这个的接口呢,其实也是需要的,只是前期你指定了一个key,在后续的操作中就无需显式的频繁的使用key来尽享操作。
做一个简单的关于String的演示,代码如下:
String boundKey = "key1";
BoundValueOperations<Stirng,String> ops = stringRedisTemplate.boundValueOps(boundKey);//绑定key
ops.set("hellos");//添加数据,相当于redis命令:set key1 hellos,只是在这里不需要显式使用key了
System.out.println(ops.get());获取并且输出数据
3、对事务进行封装最后将由Spring容器进行控制
4、提供了多种序列化和反序列化的策略
(1)StringRedisSerializer:应用与字符串场景,可以是key可以是value
(2)JdkSerializationRedisSerializer:使用JDK本身序列化方式,序列化POJO对象,通常是存储value为对象的时候
(3)JacksonJsonRedisSerializer:这一种方式理解起来很容易,将POJO对象和JSON对象进行互相转换,然后存储这个JSON字符串
(4)OxmSerializer:和上面个有些类似,但是适合XML进行转换,不推荐使用
三、SSM整合Redis
1、导入jar包,或者使用maven,方式自己选,我这提供jar包:
http://pan.baidu.com/s/1miBlAMc
2、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--spring 监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--mybatis和redis配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-redis.xml,classpath:spring-mybaties.xml</param-value>
</context-param>
<!--log4j日志-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!--spring mvc-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3、spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400000"></property>
</bean>
</beans>
4、spring-mybaties.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/springutil.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 设置自动扫描包 -->
<context:component-scan base-package="com"></context:component-scan>
<!-- 后处理器,只能写一个,所以写在redis配置文件中 -->
<!--<bean id="mapping" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:*.properties" />
</bean>-->
<!--连接池-->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="${driverClassName}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="connectionTimeout" value="${connectionTimeout}"/>
<property name="idleTimeout" value="${idleTimeout}"/>
<property name="maxLifetime" value="${maxLifetime}"/>
<property name="maximumPoolSize" value="${maximumPoolSize}"/>
<property name="minimumIdle" value="${minimumIdle}"/>
</bean>
</constructor-arg>
</bean>
<!-- 依赖于mybatis-spring.jar包 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件,这一句很重要 -->
<property name="mapperLocations" value="classpath:com/zxm/mapping/xml/*.xml"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zxm.mapping.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- AOP定义切面添加事务管理 -->
<aop:config>
<!-- 定义切面 -->
<aop:pointcut expression="execution(public * com.zxm.*.*(..))" id="myPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
</aop:config>
</beans>
mybatis如果还需要使用Alias的话还需要一个配置文件
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!-- <typeAlias type="com.zxm.mybatis.bean.User" alias="User"/> -->
<package name="com.zxm"/>
</typeAliases>
</configuration>
5、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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:*.properties"/>
<!--设置数据池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="minIdle" value="${redis.minIdle}"></property>
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean>
<!--链接redis-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.password}"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="connectionFactory" >
<!--以下针对各种数据进行序列化方式的选择-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<!--<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>-->
</bean>
</beans>
5、JDBC所需properties文件:
jdbc.properties
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
jdbcUrl=jdbc\:mysql\://localhost\:3306/redis
connectionTimeout=30000
idleTimeout=600000
maxLifetime=1800000
maximumPoolSize=400
minimumIdle=100
6、Redis所需properties文件:
redis.properties
redis.maxIdle=300
redis.minIdle=100
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.maxTotal=500
redis.host=127.0.0.1
redis.port=6379
redis.password=zxm
7、日志配置文件
log.properties
# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
四、合作
1、设置一个Reids的基本操作接口
RedisBaiseTakes.java
package com.zxm.util.redistakes;
import java.util.List;
/**
* Created by zxm on 2017/8/11.
*/
public interface RedisBaiseTakes<H,K,V> {
//增
public void add(K key,String value);
public void addObj(H objectKey,K key,V object);
//删
public void delete(K key);
public void delete(List<K> listKeys);
public void deletObj(H objecyKey,K key);
//改
public void update(K key,String value);
public void updateObj(H objectKey,K key,V object);
//查
public String get(K key);
public V getObj(H objectKey,K key);
}
设定一个访问客户的redis操作类:
SeeUserRedisTakes.java
package com.zxm.util.redistakes;
import com.zxm.bean.SeeUser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.logging.Logger;
/**
* Created by zxm on 2017/8/12.
*
*/
@Component("seeUserRedisTakes")
public class SeeUserRedisTakes implements RedisBaiseTakes<String,String,SeeUser>{
@Resource(name="redisTemplate")
private RedisTemplate redisTemplate;
private Logger logger = Logger.getLogger(String.valueOf(SeeUserRedisTakes.class));
@Override
public void add(String key, String value) {
if(redisTemplate==null){
logger.warning("redisTemplate 实例化失败");
return;
}else{
redisTemplate.opsForValue().set(key,value);
}
}
@Override
public void addObj(String objectKey, String key, SeeUser object) {
if(redisTemplate==null){
logger.warning("redisTemplate 实例化失败");
return;
}else{
redisTemplate.opsForHash().put(objectKey,key,object);
}
}
@Override
public void delete(String key) {
}
@Override
public void delete(List<String> listKeys) {
}
@Override
public void deletObj(String objecyKey, String key) {
}
@Override
public void update(String key, String value) {
}
@Override
public void updateObj(String objectKey, String key, SeeUser object) {
}
@Override
public String get(String key) {
String value = (String) redisTemplate.opsForValue().get(key);
return value;
}
@Override
public SeeUser getObj(String objectKey, String key) {
SeeUser seeUser = (SeeUser) redisTemplate.opsForHash().get(objectKey,key);
return seeUser;
}
}
创建一个用户访问的POJO对象
SeeUser.java
package com.zxm.bean;
import java.io.Serializable;
/**
* Created by zxm on 2017/8/11.
*/
public class SeeUser implements Serializable{
private String id;
private String ip;//用户的id
private String seeTime;//用户访问的时间
private int seeCount;//用户访问的次数
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getSeeTime() {
return seeTime;
}
public void setSeeTime(String seeTime) {
this.seeTime = seeTime;
}
public int getSeeCount() {
return seeCount;
}
public void setSeeCount(int seeCount) {
this.seeCount = seeCount;
}
}
创建一个Controller对象:
package com.zxm.controller;
import com.zxm.bean.SeeUser;
import com.zxm.util.redistakes.RedisBaiseTakes;
import com.zxm.util.tools.CommTools;
import com.zxm.util.tools.GetUserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* Created by zxm on 2017/8/7.
*
*/
@Controller
@RequestMapping("test")
public class TestController {
@Resource(name="seeUserRedisTakes")
private RedisBaiseTakes seeUserRedisTakes;
@RequestMapping("/hello.do")
public ModelAndView hello(){
ModelAndView mv = new ModelAndView();
System.out.println("hello see");
seeUserRedisTakes.add("hello1","zxm");
mv.setViewName("hello");
return mv;
}
@RequestMapping("/hello2.do")
public ModelAndView hello2(HttpServletRequest request){
ModelAndView mv = new ModelAndView();
SeeUser seeUser = new SeeUser();
seeUser.setId("1");
seeUser.setIp(GetUserInfo.getIpAddr(request));
seeUser.setSeeTime(CommTools.getNoewTime("yyyy-MM-dd HH:mm:ss"));
seeUser.setSeeCount(1);
seeUserRedisTakes.addObj("seeUser",seeUser.getId(),seeUser);
mv.setViewName("hello");
return mv;
}
@RequestMapping("/hello3.do")
public ModelAndView hello3(HttpServletRequest request){
ModelAndView mv = new ModelAndView();
SeeUser seeUser = (SeeUser) seeUserRedisTakes.getObj("seeUser","1");
if(seeUser!=null){
System.out.println(seeUser.getId()+"======="+seeUser.getIp()+"======"+seeUser.getSeeTime()+"======="+seeUser.getSeeCount());
}
mv.setViewName("hello");
return mv;
}
}
访问结果:
(1)访问链接:
http://localhost:8080/test/hello.do
访问结果:
(2)访问链接:
http://localhost:8080/test/hello2.do
访问结果:
(3)访问链接:
http://localhost:8080/test/hello3.do
访问结果:
输出:
上述代码中没有将mybatis用起来,但是redis和SSM的整合就是redis和Spring的整合,这里演示的是SpringMVC 控制器(Controller)对redis的简单操作和使用,当然也可以其它如server层,原理都一样