日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2025/3/8 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql二级缓存redis_SpringBoot+Mybatis+redis(二级缓存)搭建 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

剛剛開始接觸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實現。

總結

以上是生活随笔為你收集整理的mysql二级缓存redis_SpringBoot+Mybatis+redis(二级缓存)搭建的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。