mybatis使用redis做二级缓存

两盒软妹~` 提交于 2020-01-09 12:30:25

redis安装

springboot+mybatis搭建

maven

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

application.yml

spring:
  #DataSource
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shop?charset=utf8mb4&useSSL=false&serverTimezone=UTC
    username: root
    password: root
    #Hikari 数据源专用配置
    hikari:
      maximum-pool-size: 20
      minimum-idle: 5
#mybatis
mybatis:
  type-aliases-package: com.java.xrc.entity
  mapper-locations: classpath:mappers/*.xml

User

@Alias("user")
public class User implements Serializable {
    private Integer id;
    private String name;
    private String password;
    private String account;
	/get set ...
}

UserMapper

@Mapper
public interface UserMapper {
    Optional<User> getById(Integer id);
    Integer insert(User user);
}

UserMapper.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" >
<mapper namespace="com.java.xrc.dao.UserMapper">
    <select id="getById" parameterType="int" resultType="user">
        select id,name,password,account from user where id=#{id}
    </select>
    <insert id="insert" useGeneratedKeys="true"  keyProperty="id">
        insert into user (name,password,account) values (#{name},#{password},#{account})
    </insert>
</mapper>

使用redis做二级缓存

cache

使用mysql的二级缓存需要实现mybatis提供的cache接口

public interface Cache {
    String getId();
    void putObject(Object var1, Object var2);
    Object getObject(Object var1);
    Object removeObject(Object var1);
    void clear();
    int getSize();
    default ReadWriteLock getReadWriteLock() {
        return null;
    }
}

RedisCache

mybatis官方提供了redis做二级缓存的包,引入包

<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-redis</artifactId>
    <version>1.0.0-beta2</version>
</dependency>

官方的实现类RedisCache

package org.mybatis.caches.redis;
public final class RedisCache implements Cache {
...
}

采用的默认配置JedisPoolConfig

package org.mybatis.caches.redis;
public class RedisConfig extends JedisPoolConfig {
    private String host = "localhost";
    private int port = 6379;
    private int connectionTimeout = 2000;
    private int soTimeout = 2000;
    private String password;
    private int database = 0;
	...
}

使用

在mapper.xml中使用指定实现类

<mapper namespace="com.java.xrc.dao.UserMapper">
    <cache type="org.mybatis.caches.redis.RedisCache"></cache>
</mapper>

如果官方提供的不满足业务需求,则可以自行实现cache接口,使用自定义缓存。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!