mysql二级缓存redis_SpringBoot+Mybatis+redis(二级缓存)搭建

本文介绍了如何使用SpringBoot、Mybatis和Redis搭建一个带有二级缓存功能的简单开发框架。配置了数据源、Mybatis、Redis,并在Mybatis中实现了Redis缓存,通过RedisCache类进行缓存操作。详细步骤包括创建项目、配置文件、Mapper接口和XML文件等。
摘要由CSDN通过智能技术生成

刚刚开始接触Spring Boot,因为极简单的配置开发,搭建一个通用的Spring Boot+Mybaitis+redis的开发框架。

一、用maven构建项目,pom.xml文件如下:

org.springframework.boot

spring-boot-starter-parent

1.5.1.RELEASE

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-logging

org.springframework.boot

spring-boot-starter-log4j2

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.mybatis.spring.boot

mybatis-spring-boot-starter

${mybatis-spring-boot}

mysql

mysql-connector-java

${mysql-connector}

org.springframework.boot

spring-boot-starter-cache

org.springframework.boot

spring-boot-starter-redis

1.4.6.RELEASE

junit

junit

4.12

因为用到了spring-boot-starter-parent的版本为1.5.1.RELEASE,所以需要指定spring-boot-starter-redis的版本为1.4.6.RELEASE。r

二、使用

配置文件application.properties如下

## 数据源配置

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

## Mybatis 配置

mybatis.typeAliasesPackage=org.spring.springboot.domain

mybatis.mapperLocations=classpath\:mapper/*.xml

mybatis.config-location=classpath\:mybatis-config.xml

logging.config=classpath\:log4j2.xml

spring.redis.host=localhost

spring.redis.port=6379

spring.redis.pool.max-idle=8

spring.redis.pool.min-idle=0

spring.redis.pool.max-active=8

spring.redis.pool.max-wait=-1

使用MyBatis

创建持久化bean

public class City implements Serializable{

/**

*

*/

private static final long serialVersionUID = -2081742442561524068L;

/**

* 城市编号

*/

private Long id;

/**

* 省份编号

*/

private Long provinceId;

/**

* 城市名称

*/

private String cityName;

/**

* 描述

*/

private String description;

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public Long getProvinceId() {

return provinceId;

}

public void setProvinceId(Long provinceId) {

this.provinceId = provinceId;

}

public String getCityName() {

return cityName;

}

public void setCityName(String cityName) {

this.cityName = cityName;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

}

数据库Dao接口,只有一个findByName方法

public interface CityDao {

/**

* 根据城市名称,查询城市信息

*

* @param cityName 城市名

*/

City findByName(@Param("cityName") String cityName);

}

CityMapper.xml

id, province_id, city_name, description

select

from city

where city_name = #{cityName}

三、因为使用了Redis作为二级缓存,所以在CityMapper.xml中添加了Redis的缓存实现

Mybatis与Redis的整合如下,在mybatis-config.xml开启缓存

Redis的缓存实现

public class RedisCache implements Cache

{

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

@Autowired

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 = (JedisConnection) 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 = (JedisConnection) 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 = (JedisConnection) 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 = (JedisConnection) 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 = (JedisConnection) 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;

}

}

Redis的管理由Spring实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值