记着:Spring与Redis整合其实是缓存的整合,不在spring部分,而在mybatis部分!!!
在网上找了很多博客,最后找到这篇博文才算整合完成:https://blog.csdn.net/zhanglf02/article/details/74999231#commentBox 十分感谢博主!
项目结构:
1、添加pom依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gm</groupId>
<artifactId>SSM-Redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 版本控制 -->
<properties>
<!-- 整个项目统一字符集编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.9</junit.version>
<spring.version>4.3.1.RELEASE</spring.version>
<mybatis.version>3.4.4</mybatis.version>
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<commons-dbcp.version>1.4</commons-dbcp.version>
<jstl.version>1.2</jstl.version>
<log4j.version>1.2.17</log4j.version>
<fastjson.version>1.2.35</fastjson.version>
<slf4j.version>1.7.25</slf4j.version>
<jackson.version>1.9.13</jackson.version>
<commons-fileupload.version>1.3.3</commons-fileupload.version>
<commons-io.version>2.5</commons-io.version>
<commons-codec.version>1.10</commons-codec.version>
<aspectjweaver.version>1.8.11</aspectjweaver.version>
</properties>
<dependencies>
<!-- spring包 core、web、oxm、tx、jdbc、webmvc、aop、context、test -->
<!-- Spring的核心工具包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring对于object/xml映射的支持,可以让JAVA与XML之间来回切换 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 对JDBC 的简单封装 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 包含SpringMVC框架相关的所有类。包含国际化、标签、Theme、视图展现的FreeMarker、JasperReports、
Tiles、Velocity、XSLT相关类。当然,如果你的应用使用了独立的MVC框架,则无需这个JAR文件里的任何类。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring的面向切面编程,提供AOP(面向切面编程)的实现 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring context的扩展支持,用于MVC方面 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 对JUNIT等测试框架的简单封装 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- 日志文件管理包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- StringUtils工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<!-- 用于摘要运算、编码解码的包 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- spring的切入点表达式需要用的包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
<!-- 对javax servlet 的引用 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b07</version>
<scope>provided</scope>
</dependency>
<!-- Junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<!--引入redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-redis</artifactId>
<version>1.0.0-beta2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 指定项目源码的jdk版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<finalName>redis</finalName>
</build>
</project>
2、配置资源文件:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置指定要扫描spring注解类的所在包 (剔除 Controller) ******注意修改**** -->
<context:component-scan base-package="com.gm.*"
use-default-filters="true">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 开启注解 -->
<context:annotation-config />
<!--配置属性加载解析属性文件 (数据库链接) -->
<bean
class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<!-- 链接数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置MyBatis SqlSessionFactory 注入数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定MyBatis数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 指定MyBatis mapper文件的位置 -->
<!-- <property name="mapperLocations" value="classpath:com/southwind/dao/*.xml"/> -->
<!-- mapper映射文件 对应Dao层 mapper映射文件 ********注意修改 -->
<property name="mapperLocations" value="classpath:com/gm/dao/*Dao.xml"></property>
<!-- 指定MyBatis全局配置文件的位置 -->
<!-- <property name="configLocation" value="classpath:mybatis-config.xml"></property> -->
<!-- 指定别名 实体类的别名 -->
<property name="typeAliasesPackage" value="com.gm.entity"></property>
<!--开启redis缓存 -->
<property name="configurationProperties">
<props>
<!--全局映射器启用缓存 -->
<prop key="cacheEnabled">true</prop>
<!--查询时,关闭关连对象即时加载以提高性能 -->
<prop key="lazyLoadingEnabled">false</prop>
<!-- 设置关联对象加载的形态,此处为按需要加载字段(加载字段由sql决定),不会加载关联表的所有字段,以提高性能 -->
<prop key="aggressiveLazyLoading">true</prop>
</props>
</property>
</bean>
<!-- 扫描MyBatis的mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有DAO接口的实现,加入到IOC容器中 -->
<property name="basePackage" value="com.gm.dao" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务增强,事务如何切入 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="regist*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove**" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 事物增强 第一个*表示返回值类型! 空格后跟的是包路径! 括号..表任何类型! **********注意修改 -->
<aop:config>
<!-- 切入点表达式 -->
<aop:pointcut expression="execution(* com.gm.service.impl.*.*(..))"
id="txPoint" />
<!-- 配置事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
</aop:config>
</beans>
db.properties
driver = com.mysql.jdbc.Driver
#在xml配置文件中配置数据库utl时,要使用&的转义字符也就是&
#useUnicode=true&characterEncoding=utf8
#1. 存数据时:
#数据库在存放项目数据的时候会先用UTF-8格式将数据解码成字节码,然后再将解码后的字节码重新使用GBK编码存放到数据库中。
#2.取数据时:
#在从数据库中取数据的时候,数据库会先将数据库中的数据按GBK格式解码成字节码,然后再将解码后的字节码重新按UTF-8格式编码数据,最后再将数据返回给客户端。
#serverTimezone=UTC
#覆盖时区的检测/映射。当服务器的时区为映射到Java时区时使用
#设置时区UTC代表的是全球标准时间 ,但是我们使用的时间是北京时区也就是东八区,领先UTC八个小时
#如果出现在java代码和数据插入的时间不匹配 那么serverTimezone=Asia/Shanghai
url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC useSSL=false&
username = root
password = root
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration>
<!--输出到控制台-->
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="info"/>
日志输出格式
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</layout>
</appender>
<!--输出到文件(info)-->
<!--将生成“info.log.2014-06-11”这样的日志文件-->
<appender name="fileAppenderInfo" class="org.apache.log4j.DailyRollingFileAppender">
<!-- 输出文件全路径名-->
<param name="File" value="${reids.root}/WEB-INF/logs/info.log" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<!-- 日志输出格式 -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<!--输出到文件(warn)-->
<appender name="fileAppenderWarn" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${reids.root}/WEB-INF/logs/warn.log" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<!--输出到文件(error)-->
<appender name="fileAppenderError" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${reids.root}/WEB-INF/logs/error.log" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<!--屏蔽所有org.springframework.*输出的Debug(及以下)信息-->
<logger name="org.springframework">
<level value="INFO"></level>
</logger>
<root>
<level value="ALL"/>
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppenderInfo" />
<appender-ref ref="fileAppenderWarn" />
<appender-ref ref="fileAppenderError" />
</root>
</log4j:configuration>
redis.properties
#要链接的redis地址 ip
redis.ip=127.0.0.1
#redis端口 默认为6379
redis.port=6379
redis.key=SOA
#默认没有密码可致为空
redis.password=admin
#是否启用redis?true启用,false不启用。
redis.switch=true
jedis.pool.maxActive=3000
jedis.pool.maxIdle=1000
jedis.pool.maxWait=1500
jedis.pool.testOnBorrow=true
jedis.pool.testOnReturn=true
jedis.pool.timeout=5000
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
xmlns:util="http://www.springframework.org/schema/util">
<!-- 配置指定要扫描包含spring注解类的所在包 (只包含Controller) -->
<context:component-scan base-package="
com.gm.controller "
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 告知spring,启用springmvc的注解驱动 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- springMvc静态资源访问 -->
<!-- <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources> -->
<mvc:default-servlet-handler />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>main.jsp</welcome-file>
</welcome-file-list>
<!-- 配置spring 对log4j的监听 -->
<context-param>
<param-name>webAppRootKey</param-name>
<!-- Web目录的绝对路径 变量 -->
<param-value>redis.root</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<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>/*</url-pattern>
</filter-mapping>
<!--防止spring内存溢出 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Spring 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
首先是引入三个工具类ConfigUtils、JedisUtils、RedisCache
ConfigUtils(主要链接redis)
package com.gm.util;
import java.io.IOException;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConfigUtils {
private static Logger logger=LoggerFactory.getLogger(ConfigUtils.class);
public static boolean redisSwitch;
public static int maxIdle;
public static boolean testOnBorrow;
public static boolean testOnReturn;
public static String ip;
public static int port;
public static String key;
public static String password;
public static int timeout;
public static int fail_count=0;
static{
Properties props=new Properties();
try {
props.load(JedisUtils.class.getResourceAsStream("/redis.properties"));
redisSwitch=Boolean.valueOf(props.getProperty("redis.switch"));
maxIdle=Integer.valueOf(props.getProperty("jedis.pool.maxIdle"));
testOnBorrow=Boolean.valueOf(props.getProperty("jedis.pool.testOnBorrow"));
testOnReturn=Boolean.valueOf(props.getProperty("jedis.pool.testOnReturn"));
ip=String.valueOf(props.getProperty("redis.ip"));
port=Integer.valueOf(props.getProperty("redis.port"));
password=String.valueOf(props.getProperty("redis.password"));
key=String.valueOf(props.getProperty("redis.key"));
timeout=Integer.valueOf(props.getProperty("jedis.pool.timeout"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//对redis开关进行设置,有三种情况:1.如果现在开关开,则关闭redispool 2.如果开关关闭,则设置redispool为开启状态 3.现在状态和要设置的状态一致,不做操作。
public static void setSwitch(boolean redisSwitch){
if(true==ConfigUtils.redisSwitch && false== redisSwitch){
logger.info("switch:open-->close");
JedisUtils.closeJedisPool();
}else if(ConfigUtils.redisSwitch==false && true == redisSwitch){
logger.info("switch:close-->open");
JedisUtils.getInstence();
}
ConfigUtils.redisSwitch=redisSwitch;
}
//当redis连接异常超过一定数量之后,不再走redis,但是没有一个机制,当reids恢复后重新使用redis
public static void setFail(){
if(redisSwitch){
fail_count=fail_count+1;
if(fail_count >10){
logger.info("setSwitch(false)");
setSwitch(false);
}
}
}
public static void setSucc(){
if(fail_count >0){
fail_count=0;
}
if(!redisSwitch){
setSwitch(true);
}
}
}
JedisUtils(主要是添加,查找,删除等redis中的数据)
package com.gm.util;
import org.mybatis.caches.redis.SerializeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisUtils {
private static Logger logger=LoggerFactory.getLogger(JedisUtils.class);
private static JedisPool JEDISPOOL;
public static void getInstence(){
if(JEDISPOOL == null){
logger.info("JeidsUtils getInstence...");
try {
JedisPoolConfig conf=new JedisPoolConfig();
conf.setMaxIdle(ConfigUtils.maxIdle);
conf.setTestOnBorrow(ConfigUtils.testOnBorrow);
//当配置中配置有password时,则创建带密码的缓存池
if(ConfigUtils.password !=null && !"".equals(ConfigUtils.password)){
JEDISPOOL=new JedisPool(conf,ConfigUtils.ip,ConfigUtils.port,ConfigUtils.timeout,ConfigUtils.password);
logger.info("--------JEDISPOOL:"+JEDISPOOL.getResource());
}else{
//没有配置则用无密码的缓存池。
JEDISPOOL=new JedisPool(conf,ConfigUtils.ip,ConfigUtils.port,ConfigUtils.timeout);
}
} catch (Exception e) {
logger.error("加载【jedis.properties】异常,异常信息为:"+e.getMessage());
}
}
}
public static Jedis getJedis(){
try {
return JEDISPOOL.getResource();
} catch (Exception e) {
return null;
}
}
public static void closeJedis(Jedis jedis){
if(jedis !=null){
jedis.quit();
}
}
public static void closeJedisPool(){
if(JEDISPOOL !=null){
JEDISPOOL.destroy();
}
}
//redis 序列化存储Object
public static void put(String id,Object key,Object value){
Jedis jedis=getJedis();
logger.info("redis put ... key =["+key+"]");
try {
jedis.hset(SerializeUtil.serialize(id), SerializeUtil.serialize(key), SerializeUtil.serialize(value));
ConfigUtils.setSucc();
} catch (Exception e) {
ConfigUtils.setFail();
logger.error("redis执行异常【"+e.getMessage()+"】");
}finally{
closeJedis(jedis);
}
}
public static Object get(String id,Object key){
Jedis jedis=getJedis();
try {
Object object = SerializeUtil.unserialize(jedis.hget(SerializeUtil.serialize(id), SerializeUtil.serialize(key)));
logger.info("redis get ... key=["+key+"],value=["+object+"]");
ConfigUtils.setSucc();
return object;
} catch (Exception e) {
ConfigUtils.setFail();
logger.error("Redis执行异常【"+e.getMessage()+"】");
}finally{
closeJedis(jedis);
}
return null;
}
public static Long remove(String id,Object key){
Jedis jedis=getJedis();
try {
Long num = jedis.hdel(id.toString(), key.toString());
ConfigUtils.setSucc();
return num;
} catch (Exception e) {
ConfigUtils.setFail();
logger.error("Redis执行异常,异常信息:"+e.getMessage());
}finally{
closeJedis(jedis);
}
return 0l;
}
public static void removeAll(String id){
Jedis jedis=getJedis();
try {
jedis.del(id.toString());
ConfigUtils.setSucc();
} catch (Exception e) {
ConfigUtils.setFail();
logger.error("Redis执行异常【"+e.getMessage()+"】");
}finally{
closeJedis(jedis);
}
}
public static int getSize(String id){
Jedis jedis=getJedis();
try {
return jedis.hgetAll(SerializeUtil.serialize(id)).size();
} catch (Exception e) {
ConfigUtils.setFail();
logger.error("Redis执行异常【"+e.getMessage()+"】");
}finally{
closeJedis(jedis);
}
return -1;
}
}
RedisCache
package com.gm.util;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RedisCache implements Cache {
private static Logger logger = LoggerFactory.getLogger(RedisCache.class);
private String cacheId;
/**
* 读写锁:分为读锁和写锁,多个读锁不互斥,读锁与写锁互斥,这是由jvm自己控制的,你只要上好相应的锁即可。如果你的代码只读数据,可以很多人
* 同时读,但不能同时写,那就上读锁;如果你的代码修改数据,只能有一个人在写,且不能同时读取,那就上写锁。总之,读的时候上读锁,写的时候上
* 写锁!
*/
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
private final Lock read = readWriteLock.readLock();
private final Lock write = readWriteLock.writeLock();
public RedisCache(String cacheId) {
if (cacheId == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.cacheId = ConfigUtils.key + "." + cacheId;
logger.info("查询结果存入缓存对应的缓存空间生成的名字cacheId: " + this.cacheId);
if (ConfigUtils.redisSwitch) {
JedisUtils.getInstence();
}
}
@Override
public String getId() {
return cacheId;
}
@Override
public void putObject(Object key, Object value) {
// TODO 从缓存中写数据,用写锁锁定,不允许读
logger.info("NTSRedisCache putObject=" + cacheId);
if (ConfigUtils.redisSwitch) {
write.lock();
try {
JedisUtils.put(cacheId, key, value);
} finally {
write.unlock();
}
}
}
@Override
public Object getObject(Object key) {
// TODO 从缓存中读数据,用读锁锁定,不允许写
// logger.info("从缓存cacheId="+cacheId+"中拿数据key="+key+"对应的value");
logger.info("》》》》》》》》》》》》》》》》》在缓存中根据cacheid="+cacheId+" 拿对应的key和value");
if (ConfigUtils.redisSwitch) {
read.lock();
try {
return JedisUtils.get(cacheId, key);
} finally {
read.unlock();
}
}
return null;
}
@Override
public Object removeObject(Object key) {
// TODO 从缓存中改动数据,用写锁锁定,不允许读,改动结束后释放写锁。
logger.info("NTSRedisCache clear =" + cacheId);
if (ConfigUtils.redisSwitch) {
write.lock();
try {
return JedisUtils.remove(cacheId, key);
} finally {
write.unlock();
}
}
return null;
}
@Override
public void clear() {
// TODO 从缓存中改动数据,用写锁锁定,不允许读,改动结束后释放写锁。
logger.info("NTSRedisCache clear =" + cacheId);
if (ConfigUtils.redisSwitch) {
write.lock();
try {
JedisUtils.removeAll(cacheId);
} finally {
write.unlock();
}
}
}
@Override
public int getSize() {
// TODO Auto-generated method stub
logger.info("NTSRedisCache clear =" + cacheId);
if (ConfigUtils.redisSwitch) {
read.lock();
try {
return JedisUtils.getSize(cacheId);
} finally {
read.unlock();
}
}
return -1;
}
@Override
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
}
定义实体类
User(需要实现序列化,实现 java.io.Serializable 的接口 否则会报java.io.NotSerializableException异常)
package com.gm.entity;
import java.io.Serializable;
/**
* 用户实体类(tb_user)
*
* @author Administrator
*
*/
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int user_id;// 用户id
private String nickname;// 用户昵称
private int phone;// 用户电话
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
}
然后定义dao层接口以及接口对应的xml
UserDao
package com.gm.dao;
import java.util.List;
import com.gm.entity.User;
/**
* 用户dao层接口
* @author Administrator
*
*/
public interface UserDao {
/**
* 查询所有用户信息
* @return
*/
List<User> selectUserAll();
}
UserDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 命名空间 依靠dao层接口 方法id与dao层接口的方法名一致 -->
<mapper namespace="com.gm.dao.UserDao">
<!-- 查询所有用户信息 -->
<!-- 引入自定义缓存类实现cache接口 -->
<cache type="com.gm.util.RedisCache"/>
<select id="selectUserAll" resultType="User">
select user_id,nickname,phone from tb_user
</select>
</mapper>
业务层的接口 以及实现
UserService
package com.gm.service;
import java.util.List;
import com.gm.entity.User;
/**
* 用户业务层接口
* @author Administrator
*
*/
public interface UserService {
/**
* 查询所有用户信息
* @return
*/
List<User> selectUserAll();
}
UserServiceImpl
package com.gm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gm.dao.UserDao;
import com.gm.entity.User;
import com.gm.service.UserService;
/**
* 用户业务层实现类
*
* @author Administrator
*
*/
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public List<User> selectUserAll() {
List<User> list = userDao.selectUserAll();
if (list != null && list.size() > 0) {
for (User user : list) {
System.out.println("查询得到的用户的姓名:" + user.getNickname() + ",学生的电话:" + user.getPhone());
}
}
return list;
}
}
接下来就可以尝试调用
UserController
package com.gm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gm.entity.User;
import com.gm.service.UserService;
/**
* 用户控制类
* @author Administrator
*
*/
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
UserService userService;
@ResponseBody
@RequestMapping("getUserAll")
public void getUserAll() {
//查询所有用户
List<User> list=userService.selectUserAll();
}
}
调用结果(由于我调用过 所以直接从redis缓存中取出数据)
[16:32:00:112] [INFO] - com.gm.util.RedisCache.getObject(RedisCache.java:61) - 》》》》》》》》》》》》》》》》》在缓存中根据cacheid=SOA.com.gm.dao.UserDao 拿对应的key和value
[16:32:00:125] [INFO] - com.gm.util.JedisUtils.get(JedisUtils.java:76) - redis get ... key=[-273895532:3612635778:com.gm.dao.UserDao.selectUserAll:0:2147483647:select user_id,nickname,phone from tb_user:SqlSessionFactoryBean],value=[[com.gm.entity.User@68e109de, com.gm.entity.User@4ce5c8da, com.gm.entity.User@50430872]]
查询得到的用户的姓名:张三,学生的电话:1235436
查询得到的用户的姓名:李四,学生的电话:1231243
查询得到的用户的姓名:王五,学生的电话:31421
搞定!