先搭建好redis环境

需要的jar如下:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/cache_test
jdbc.username=root
jdbc.password=
redis.host=127.0.0.1
redis.port=6379
redis.maxIdle=2000
redis.maxActive=60000
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=100000
defaultCacheExpireTime=60
<?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> <!-- 配置mybatis的缓存,延迟加载等相关属性 -->
<settings>
<!-- 是否开启全局缓存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<setting name="lazyLoadingEnabled" value="false"/>
<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
<setting name="aggressiveLazyLoading" value="true"/>
</settings> </configuration>
<?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:lang="http://www.springframework.org/schema/lang"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- 包扫描 -->
<context:component-scan base-package="sysone.zr.com" /> <!-- 引入外部文件 -->
<context:property-placeholder location="/WEB-INF/config/jdbc.properties,/WEB-INF/config/redis.properties"/> <!-- 开启动态代理 -->
<aop:aspectj-autoproxy/> <!-- 事务属性配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 事务传播属性 -->
<tx:attributes>
<!-- 所有已get、query、select开头的方法都是只读 -->
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<!-- 其它的所有方法支持事务设置的属性(异常回滚) -->
<tx:method name="*" rollback-for="java.lang.Throwing" />
</tx:attributes>
</tx:advice> <aop:config>
<!-- 配置切点 -->
<aop:pointcut id="fooServiceOperation" expression="execution(* sysone.zr.com.service.sysone.zr.com.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
</aop:config> <!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- 配置数据源管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="/WEB-INF/config/mybatis-config.xml"/>
<property name="typeAliasesPackage" value="sysone.zr.com.mapper.model"/>
<property name="mapperLocations">
<list>
<value>classpath:sysone/zr/com/mapper/xml/*Mapper.xml</value>
</list>
</property>
</bean> <!-- mybatis 扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="sysone.zr.com.mapper.imapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean> <!-- 配置redis 单机版 -->
<!-- redis数据源 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="${redis.maxIdle}" />
<!-- 最大空闲数 -->
<property name="maxTotal" value="${redis.maxActive}" />
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="${redis.maxWait}" />
<!-- 返回连接时,检测连接是否成功 -->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <!-- Spring-redis连接池管理工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- IP地址 -->
<property name="hostName" value="${redis.host}" />
<!-- 端口号 -->
<property name="port" value="${redis.port}" />
<!-- 超时时间 -->
<property name="timeout" value="${redis.timeout}" />
<property name="poolConfig" ref="poolConfig" />
</bean> <!-- redis 集群 -->
<!-- <bean id="redisClient" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.10.105"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.10.77"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
</constructor-arg>
</bean> --> <!-- redis模板类,提供了对缓存的增删改查 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean> <!-- StrRedisTemplate -->
<bean id="strRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<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>
</bean> <!-- 使用中间类解决RedisCache.jedisConnectionFactory的静态注入,从而使MyBatis实现第三方缓存 -->
<bean id="redisCacheTransfer" class="sysone.zr.com.utils.RedisCacheTransfer">
<property name="jedisConnectionFactory" ref="jedisConnectionFactory"/>
</bean>
<!-- //End 单机版Redis集成 --> <!-- Redis缓存管理对象 -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" ref="redisTemplate" />
</bean> <!-- 开启Spring缓存 -->
<cache:annotation-driven cache-manager="cacheManager"/> </beans>
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
<!-- 包扫描 -->
<context:component-scan base-package="sysone.zr.com" />
<!-- 开启Springmvc注解驱动 -->
<mvc:annotation-driven /> <!-- SpringMVC视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- 拦截器 -->
<!-- <bean id="urlhandler" class="sysone.zr.com.interception.URLHandler"></bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/lf/online/**"/>
<ref bean="urlhandler"/>
</mvc:interceptor>
</mvc:interceptors> --> </beans>
<?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/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0"> <!-- 配置springmvc核心 Servlet-->
<servlet>
<servlet-name>pringmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置文件加载的路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>pringmvc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!-- END 配置springmvc核心 Servlet--> <!-- spring 路径的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring.xml</param-value>
</context-param>
<!--END spring 路径的配置 --> <!-- 配置spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--END 配置spring监听器 --> </web-app>
package sysone.zr.com.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import sysone.zr.com.mapper.model.Person;
import sysone.zr.com.service.PersonService; @Controller
public class TestController { @Resource
private PersonService personService; @RequestMapping("/person.htm")
public void getPersonInfo(){ List<Person> persons = personService.getAllPerson();
if (persons!=null) {
for (Person person : persons) {
System.out.println("person:"+person);
}
} } }
package sysone.zr.com.mapper.imapper;

import java.util.List;

import sysone.zr.com.mapper.model.Person;

public interface PersonMapper {

    /**
* 获取所有person的信息
* @return
*/
List<Person> getAllPerson(); }
package sysone.zr.com.mapper.model;

import java.io.Serializable;

public class Person implements Serializable {

    private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person() {
super();
}
public Person(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
} }
<?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">
<mapper namespace="sysone.zr.com.mapper.imapper.PersonMapper">
<!-- 支持缓存配置 -->
<cache type="sysone.zr.com.utils.RedisCache" /> <resultMap type="sysone.zr.com.mapper.model.Person" id="baseMap">
<id property="id" column="id" />
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> <select id="getAllPerson" resultMap="baseMap">
SELECT id,name,age FROM PERSON
</select> </mapper>
package sysone.zr.com.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import sysone.zr.com.mapper.imapper.PersonMapper;
import sysone.zr.com.mapper.model.Person;
import sysone.zr.com.service.PersonService; @Service
public class PersonServerImpl implements PersonService { @Resource
private PersonMapper mapper; @Override
public List<Person> getAllPerson() {
System.out.println("===== 获取person信息 =====");
return mapper.getAllPerson();
} }
package sysone.zr.com.service;

import java.util.List;

import sysone.zr.com.mapper.model.Person;

public interface PersonService {

    /**
* 获取所有的person信息
* @return
*/
List<Person> getAllPerson(); }
package sysone.zr.com.utils;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer; public class RedisCache implements Cache { private static JedisConnectionFactory jedisConnectionFactory; private final String id; private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); public RedisCache(final String id){
if (id == null) {
throw new IllegalArgumentException("cache instances require an ID");
}
this.id = id;
} @Override
public void clear() {
JedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
connection.flushDb();
connection.flushAll();
} catch (Exception e) {
e.printStackTrace();
}finally{
if (connection != null) {
connection.close();
}
}
} @Override
public String getId() {
return this.id;
} @Override
public Object getObject(Object key) {
System.out.println("--------------------------------key:"+key);
Object result = null;
JedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
result = serializer.deserialize(connection.get(serializer.serialize(key)));
} catch (Exception e) {
e.printStackTrace();
}finally{
if (connection != null) {
connection.close();
}
} return result;
} @Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
} @Override
public int getSize() {
int result = 0;
JedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
result = Integer.valueOf(connection.dbSize().toString());
} catch (Exception e) {
e.printStackTrace();
}finally{
if (connection != null) {
connection.close();
}
}
return result;
} @Override
public void putObject(Object key, Object value) {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>key:"+key); JedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
System.out.println("**"+serializer.serialize(key));
connection.set(serializer.serialize(key), serializer.serialize(value));
} catch (Exception e) {
e.printStackTrace();
}finally{
if (connection != null) {
connection.close();
}
} } @Override
public Object removeObject(Object key) {
JedisConnection connection = null;
Object result = null;
try {
connection = jedisConnectionFactory.getConnection();
RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
result = connection.expireAt(serializer.serialize(key), 0);
} catch (Exception e) {
e.printStackTrace();
}finally{
if (connection != null) {
connection.close();
}
}
return result;
} public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
RedisCache.jedisConnectionFactory = jedisConnectionFactory;
} }
package sysone.zr.com.utils;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

public class RedisCacheTransfer {

    public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
RedisCache.setJedisConnectionFactory(jedisConnectionFactory);
}
}

spring+springmvc+mybatis+redis实现缓存的更多相关文章

  1. Spring Boot + Mybatis + Redis二级缓存开发指南

    Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...

  2. spring+springmvc+mybatis+redis 实现两重数据缓存

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  3. Spring+SpringMVC+MyBatis+Redis框架学习笔记

    在Java互联网中,以Spring+Spring MVC+MyBatis (SSM) 作为主流框架. SSM+Redis的结构图 在这种框架系统中: Spring IoC 承担了一个资源管理.整合.即 ...

  4. 【JAVA】SSM开源项目源码--城市学院移动后勤-毕业设计(Spring SpringMvc Mybatis Mui Redis )

    项目简介 大学时期老师给我做的项目,学校后勤管理中心,也作为毕业设计项目. 有 后勤保修 二手交易 失物招领 后勤通知 等功能. 城市学院移动后勤 有APP端(webapp)和WEB端(PC) 后端使 ...

  5. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势

    作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 简介 这是一篇关于Redis使用的总结类型文章,会先简单的谈一下缓存 ...

  6. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十二)Spring集成Redis缓存

    作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...

  7. 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)

    如约而至,Java 10 正式发布!   3 月 20 日,Oracle 宣布 Java 10 正式发布. 官方已提供下载:http://www.oracle.com/technetwork/java ...

  8. Spring+SpringMVC+MyBatis深入学习及搭建(八)——MyBatis查询缓存

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6956206.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(七)——My ...

  9. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(九)Linux下安装redis及redis的常用命令和操作

    redis简介 Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis与其他key-value缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存 ...

随机推荐

  1. 基于Video4Linux的视频采集模块开发(转)

    Linux系统中,摄像头驱动程序安装好后,为了进行视频采集必须加入Video4Linux模块,从而可以通过Video4Linux模块提供的编程接口(API)从摄像头设备中获取图像帧.下面具体研究基于V ...

  2. phpMyAdmin“缺少 mcrypt 扩展。请检查 PHP 配置。”解决办法

    在ecmall二次开发中因php版本要求低于5.3,而如下更新要求升级PHP,所以以下方式不适合于ecmall商城项目. 解决办法:安装php-mcrypt libmcrypt libmcrypt-d ...

  3. 老齐python-基础5(运算符、语句)

    1.运算符 1.1算术运算符 1.2比较运算符 >>> a = 10 >>> b = 20 >>> a > b False >> ...

  4. 将socket程序从linux移植到windows上

    今天突然想试下纯socket编程在两个系统上代码重合量有多大,只要不使用VC自定义的宏(比如SOCKET.SOCKADDR等等)感觉代码重合量挺大的. 比如最简单的TCP客户端和服务端对话,在VC中用 ...

  5. java代码--------随机输出100个随机数,要求每行10个数

    总结:不敢爱你么开口 package com.sads; ///实现随机输出100个数字,数字是0到9之间,每行输出10个 public class Wss { public static void ...

  6. java代码----FileInputStream 和File

    总结:程序运行后,发现新建的两个文件里的东西突然i清空了.以为是程序出错了. 然后慌了,之后我再运行时,发现可以了.是电脑的问题吧 一如既往的打扰他,只因为他优秀 package com.a.b; i ...

  7. mysql 下字符集知识汇总

    Do not issue the query set names with Connector/J, as the driver will not detect that the character ...

  8. ECMAScript有6种继承方式(实现继承)

    本人对于ECMAScript继承机制有些见解,如果说的不对,敬请赐教~~~~ 继承是OO语言(面向对象)挺好的概念,许多OO语言都支持两种继承方式(接口只继承方法签名.实际继承则继承实际的方法),但是 ...

  9. py基础2--列表,元祖,字典,集合,文件

    本节内容 列表.元祖操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 三元运算&生成式&成员运算&解压法&队列堆栈&数据类型转换 1. 列表操作 ...

  10. setTimeout和setInterval的unref()和ref()用法

    var testFunction=function(){ console.log("guoyansi"); } var timer=setInterval(testFunction ...