SprngBoot结合Redis数据库(充当缓存)

SpringBoot入口类

package SpringBootRedisConfig;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Date 2020/11/12 21:54
 */
@SpringBootApplication
@MapperScan("SpringBootRedisConfig.Dao")
@EnableCaching
public class SptingbootConfig {
    public static void main(String[] args) {
        SpringApplication.run(SptingbootConfig.class,args);
    }

    //配置缓存管理器
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory){
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
        return new RedisCacheManager(redisCacheWriter,getRedisCacheConfiguration(20), getRedisCacheConfigurationMap());
    }

    private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap(){
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        redisCacheConfigurationMap.put("users", getRedisCacheConfiguration(100));
        redisCacheConfigurationMap.put("books", getRedisCacheConfiguration(60));
        return redisCacheConfigurationMap;
    }
    //RedisCacheConfiguration 用于负责Redis的缓存配置
    private RedisCacheConfiguration getRedisCacheConfiguration(int seconds){
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        return redisCacheConfiguration.serializeValuesWith(
                RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .entryTtl(Duration.ofSeconds(seconds));
    }
}

Dao

package SpringBootRedisConfig.Dao;


import SpringBootRedisConfig.Entity.Hero;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface HeroDao {

    List<Hero> selectAllHero(@Param("sort") String sort, @Param("order") String order, @Param("search") String search, @Param("offset") Integer offset, @Param("limit") Integer limit);

    List<Hero> selectAll(@Param("offset") Integer offset, @Param("limit") Integer limit);

    Integer selectCount();

    void insert(Hero hero);

    void delete(@Param("heroid") Integer[] heroid);

    void remove(Integer heroid);

    Hero updateid(Integer id);

    void update(Hero hero);

    void updateHero(@Param("hero") Hero hero, @Param("heroid") Integer heroid);

}

实体类

package SpringBootRedisConfig.Entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.io.Serializable;
import java.util.Date;

/**
 * @Date 2020/10/27 16:54
 */
public class Hero implements Serializable {
    private Integer heroid;//id
    private String heroname;//姓名
    private String heropicture;//图片
    private Integer heroage;//年龄
    private Integer herostature;//身高
    private String heroattribute;//属性
    private Integer heroaggressivity;//攻击力
    private String herodistrict;//区域
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date herotime;//上线时间


    public Hero() {
    }

    public Hero(Integer heroid, String heroname, String heropicture, Integer heroage, Integer herostature, String heroattribute, Integer heroaggressivity, String herodistrict, Date herotime, String file) {
        this.heroid = heroid;
        this.heroname = heroname;
        this.heropicture = heropicture;
        this.heroage = heroage;
        this.herostature = herostature;
        this.heroattribute = heroattribute;
        this.heroaggressivity = heroaggressivity;
        this.herodistrict = herodistrict;
        this.herotime = herotime;

    }



    public String getHeropicture() {
        return heropicture;
    }

    public void setHeropicture(String heropicture) {
        this.heropicture = heropicture;
    }

    public Date getHerotime() {
        return herotime;
    }

    public void setHerotime(Date herotime) {
        this.herotime = herotime;
    }

    public Integer getHeroid() {
        return heroid;
    }

    public void setHeroid(Integer heroid) {
        this.heroid = heroid;
    }

    public String getHeroname() {
        return heroname;
    }

    public void setHeroname(String heroname) {
        this.heroname = heroname;
    }

    public Integer getHeroage() {
        return heroage;
    }

    public void setHeroage(Integer heroage) {
        this.heroage = heroage;
    }

    public Integer getHerostature() {
        return herostature;
    }

    public void setHerostature(Integer herostature) {
        this.herostature = herostature;
    }

    public String getHeroattribute() {
        return heroattribute;
    }

    public void setHeroattribute(String heroattribute) {
        this.heroattribute = heroattribute;
    }

    public Integer getHeroaggressivity() {
        return heroaggressivity;
    }

    public void setHeroaggressivity(Integer heroaggressivity) {
        this.heroaggressivity = heroaggressivity;
    }

    public String getHerodistrict() {
        return herodistrict;
    }

    public void setHerodistrict(String herodistrict) {
        this.herodistrict = herodistrict;
    }

    @Override
    public String toString() {
        return "Hero{" +
                "heroid=" + heroid +
                ", heroname='" + heroname + '\'' +
                ", heropicture='" + heropicture + '\'' +
                ", heroage=" + heroage +
                ", herostature=" + herostature +
                ", heroattribute='" + heroattribute + '\'' +
                ", heroaggressivity=" + heroaggressivity +
                ", herodistrict='" + herodistrict + '\'' +
                ", herotime=" + herotime +
                '}';
    }
}

Service

package SpringBootRedisConfig.Service;


import SpringBootRedisConfig.Entity.Hero;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface HeroService {

    List<Hero> selectAllHero(@Param("sort") String sort, @Param("order") String order, @Param("search") String search, @Param("offset") Integer offset, @Param("limit") Integer limit);

    List<Hero> selectAll(@Param("page") Integer page, @Param("limit") Integer limit);

    Integer selectCount();

    void insert(Hero hero);

    void remove(Integer heroid);

    void delete(@Param("heroid") Integer[] heroid);

    Hero updateid(Integer id);

    void updateHero(@Param("hero") Hero hero, @Param("heroid") Integer heroid);

    void update(Hero hero);
}
**Serviceimpl**
package SpringBootRedisConfig.Service.impl;


import SpringBootRedisConfig.Dao.HeroDao;
import SpringBootRedisConfig.Entity.Hero;
import SpringBootRedisConfig.Service.HeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class  HeroServiceimpl implements HeroService {
    @Autowired//注入属性
    private HeroDao heroDao;

    @Override
    @Cacheable(value="users",key="#root.methodName+#sort+#order+#limit+#offset+#search")
    public List<Hero> selectAllHero(String sort, String order, String search, Integer offset, Integer limit) {
        System.out.println("查询HeroServiceimpl.selectAllHero");
        return heroDao.selectAllHero(sort,order,search,offset,limit);
    }

    @Override
    @Cacheable(value = "users", key = "#root.methodName+#hero")
    public void insert(Hero hero) {
        System.out.println("增加HeroServiceimpl.insert");
        heroDao.insert(hero);
    }

    @Override
    @Cacheable(value="users",key="#root.methodName")
    public Integer selectCount() {
        System.out.println("查询数量HeroServiceimpl.selectCount");
        return heroDao.selectCount();
    }

    @Override
    @Cacheable(value="users",key="#root.methodName+#heroid")
    public void delete(Integer[] heroid) {
        System.out.println("批量删除HeroServiceimpl.delete");
        heroDao.delete(heroid);
    }

    @Override
    @Cacheable(value="users",key="#root.methodName+#id")
    public Hero updateid(Integer id) {
        System.out.println("查询单个HeroServiceimpl.updateid");
        return heroDao.updateid(id);
    }

    @Override
    @Cacheable(value="users",key="#root.methodName+#hero+#heroid")
    public void updateHero(Hero hero, Integer heroid) {
        System.out.println("更新HeroServiceimpl.updateHero");
        heroDao.updateHero(hero, heroid);
    }

    @Override
    @Cacheable(value="users",key="#root.methodName+#heroid")
    public void remove(Integer heroid) {
        System.out.println("删除HeroServiceimpl.remove");
        heroDao.remove(heroid);
    }

    @Override
    @Cacheable(value="users",key="#root.methodName+#hero")
    public void update(Hero hero) {
        System.out.println("更新HeroServiceimpl.update");
        heroDao.update(hero);
    }

    @Override
    @Cacheable(value="users",key="#root.methodName+#page+#limit")
    public List<Hero> selectAll(Integer page, Integer limit) {
        System.out.println("查询HeroServiceimpl.selectAll");
        Integer offset = page*limit-limit;
        return heroDao.selectAll(offset, limit);
    }

}
# application.yml
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.72.1:3306/student?useUnicode=true&characterEncoding=utf-8
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
  redis:
    lettuce:
      pool:
        max-active: 500
        max-idle: 50
        min-idle: 10
        max-wait: 30000
    host: 192.168.72.200
    port: 6379

#定义Mapper路径

mybatis:
  mapper-locations: classpath:Mapper/*Dao.xml
  type-aliases-package: SpringBootRedisConfig.Entity

pom.xml

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.3.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.10.3</version>
    </dependency>
    
    <!-- mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.48</version>
    </dependency>
    <!--jdbc驱动-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.48</version>
    </dependency>

    <!--连接池依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.14</version>
    </dependency>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.6.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>``
      <version>2.1.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.0</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
      <version>2.1.13.RELEASE</version>
    </dependency>
  </dependencies>
posted @ 2020-11-13 09:03  花红  阅读(151)  评论(0)    收藏  举报