SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置

SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
项目环境: 在SpringMVC + MyBatis + Mysql。Redis部署在Linux虚拟机。
1、整体思路
参考Ehcache实现MyBatis二级缓存代码(Maven引用对应jar查阅)
使用Spring管理Redis连接池
模仿EhcacheCache,实现RedisCache
2、pom.xml中加入Maven依赖
[Java] 纯文本查看 复制代码
?


org.springframework.data
spring-data-redis
1.6.2.RELEASE



redis.clients
jedis
2.8.0



org.mybatis
mybatis-ehcache
1.0.0


3、引入applicationContext.xml中引入redis配置
[Java] 纯文本查看 复制代码
?


id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">


    
        classpath:jdbc.properties
        classpath:redis.properties
    



  
  
  
  



p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"  p:pool-config-ref="poolConfig"/>  



4、创建缓存实现类RedisCache
[Java] 纯文本查看 复制代码
?

public class RedisCache implements Cache
{

private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);

private static JedisConnectionFactory jedisConnectionFactory;

private final String id;

/**
 * The {@code ReadWriteLock}.
 */
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

public RedisCache(final String id) {
    if (id == null) {
        throw new IllegalArgumentException("Cache instances require an ID");
    }
    logger.debug("MybatisRedisCache:id=" + id);
    this.id = id;
}

@Override
public void clear()
{
    JedisConnection connection = null;
    try
    {
        connection = jedisConnectionFactory.getConnection();
        connection.flushDb();
        connection.flushAll();
    }
    catch (JedisConnectionException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (connection != null) {
            connection.close();
        }
    }
}

@Override
public String getId()
{
    return this.id;
}

@Override
public Object getObject(Object key)
{
    Object result = null;
    JedisConnection connection = null;
    try
    {
        connection = jedisConnectionFactory.getConnection();
        RedisSerializer serializer = new JdkSerializationRedisSerializer();
        result = serializer.deserialize(connection.get(serializer.serialize(key)));
    }
    catch (JedisConnectionException 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 (JedisConnectionException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (connection != null) {
            connection.close();
        }
    }
    return result;
}

@Override
public void putObject(Object key, Object value)
{
    JedisConnection connection = null;
    try
    {
        connection = jedisConnectionFactory.getConnection();
        RedisSerializer serializer = new JdkSerializationRedisSerializer();
        connection.set(serializer.serialize(key), serializer.serialize(value));
    }
    catch (JedisConnectionException 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 serializer = new JdkSerializationRedisSerializer();
        result =connection.expire(serializer.serialize(key), 0);
    }
    catch (JedisConnectionException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (connection != null) {
            connection.close();
        }
    }
    return result;
}

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

}
5、创建中间类RedisCacheTransfer,完成RedisCache.jedisConnectionFactory的静态注入
[Java] 纯文本查看 复制代码
?

public class RedisCacheTransfer
{

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

}
6、配置文件redis.properties

Redis settings

redis.host=192.168.25.132
redis.port=6379
redis.pass=
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
7、mapper中加入MyBatis二级缓存
[Java] 纯文本查看 复制代码




8、Mybatis全局配置
[Java] 纯文本查看 复制代码
?


    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">




    
    

    
    

    
    

    
    

    
    

    
    

    
    

    
    

    
    

    
    

    
    

    
    

    
    

    
    


9、打印Sql日志,方便测试

定义LOG输出级别为INFO

log4j.rootLogger=INFO,Console,File

定义日志输出目的地为控制台

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out

可以灵活地指定日志输出格式,下面一行是指定具体的格式

log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

文件大小到达指定尺寸的时候产生一个新的文件

log4j.appender.File = org.apache.log4j.RollingFileAppender

指定输出目录

log4j.appender.File.File = logs/ssm.log

定义文件最大大小

log4j.appender.File.MaxFileSize = 10MB

输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志

log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] %d{yyyy-MM-dd HH:mm:ss}%m%n

显示本项目SQL语句部分

log4j.logger.com.strive.cms=DEBUG

你可能感兴趣的:(spring-mvc,mybatis,mysql,redis)