SpringBoot整合redis+mysql

SpringBoot整合Redis

测试连接

添加相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sin</groupId>
    <artifactId>demo-springboot-redis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-springboot-redis</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>


        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!-- 
        	apache通用连接池
        	提供了对可复用的对象(如数据库连接,线程等)的池化管理,有助于提高资源的利用率和系统的				   	性能   
        	使用commons-pool2来管理与Redis数据库的连接池
         -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        
        <!-- 通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 解析yaml -->
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.23</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
package com.sin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author sin
 * @date 2022/11/25
 * @apiNote
 */
@SpringBootApplication
public class SpringRedisMySQLRun {
    public static void main(String[] args) {
        SpringApplication.run(SpringRedisMySQLRun.class,args);
        System.out.println("===============启动成功================");
    }
}

配置

## 配置端口
server:
  port: 80

spring:
  ## 配置数据库数据源
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/sindemo
    username: root
    password: 123456
  redis:
    # 地址
    host: 192.168.11.128
    # 端口
    port: 6379
    # 密码
    password: 123456
    # 超时时间 5000毫秒
    timeout: 5000
    jedis:	
		连接池
      pool:
        # 连接池最小空闲连接
        min-idle: 0
        # 连接池的最大空闲连接
        max-idle: 8
        # 连接池最大阻塞等待时间(使用负数表示没有限制)
        max-wait: -1
        # 连接池最大连接数(使用负数表示没有限制)
        max-active: 8
            

# mybatis配置
mybatis:
  # 获取配置文件的地址
  mapper-locations: classpath:/mybatis/mapper/*.xml
  # 获取实体类的地址
  type-aliases-package: com.sin.pojo
  configuration:
    # 开启驼峰命名
    map-underscore-to-camel-case: true

测试项目

package com.sin.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sin
 * @date 2022/11/25
 * @apiNote
 */
@RestController
public class TestController {

    @RequestMapping("/test")
    public String testControler(){
        System.out.println("测试成功");
        return "测试成功";
    }
}

在这里插入图片描述

在这里插入图片描述

开启Redis服务

## 启动redis
[root@My-Server redis]# cd /usr/local/redis/bin
[root@My-Server bin]# ./redis-server /usr/local/redis/etc/redis.conf
## 进入redis
[root@My-Server bin]# cd /usr/local/redis/bin
[root@My-Server bin]# ./redis-cli
## 密码登录
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379>

连接Redis测试

在这里插入图片描述

RedisTemplate是Spring Data Redis提供的一个强大的Redis操作模板,用于在Spring应用程序中与Redis数据库进行交互。对redis底层开发包(Jedis,JRedis,RJC)进行了一个封装,定义了五种数据结构的操作方法,异常处理,序列化,发布订阅

  • opsForValue():操作字符串。
  • opsForList():操作列表。
  • opsForHash():操作哈希。
  • opsForSet():操作集合。
  • opsForZSet():操作有序集合。
添加数据
package com.sin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author sin
 * @date 2022/11/25
 * @apiNote
 */
@RestController // 表示该类是一个RESTful风格的控制器,将该类下的所有的方法的返回值直接以JSON或XML的形式写入到HTTP请求体。
public class RedisController {

    //注入RedisTemplate
    @Autowired
    private RedisTemplate redisTemplate;

    //添加数据
    // @PathVariable 将URL中的key和Value参数映射到方法的参数中
    @PostMapping("/addData/key={key}/value={value}")//动态地址
    public Object addData(@PathVariable("key") String key,
                          @PathVariable("value") String value){
        redisTemplate.opsForValue().set(key,value);
        System.out.println("数据添加成功"+redisTemplate);
        return "addData Success"; // 将字符串"addData Success"作为HTTP响应的主体返回给客户端
    }
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

获取数据
//根据key获取value
@GetMapping("/getData/key={key}")
// @PathVariable 将URL中的key和Value参数映射到方法的参数中
public Object getData(@PathVariable("key") String key){
  // 根据key获取value
   Object value =  redisTemplate.opsForValue().get(key);
    System.out.println("数据为:"+value);
    return value;// 将查询到的数据作为HTTP响应的主体返回给客户端
}

在这里插入图片描述在这里插入图片描述

Redis序列化

看到结果后是看不懂的,这个就跟它的序列化有关了。就是根据RedisTemplate将数据写入到redis中的时候会经过一个序列化操作,序列化操作后就变成了以上数据格式,这个并不影响程序,因为set写入时会有序列化操作,get获取数据时也就有着这么一个序列话操作,但是对于我们来说时很不友好的

在这里插入图片描述

在源码中我们能看到定义了各种数据类型的序列化的一个成员变量

if (defaultSerializer == null) { //当defaultSerializer为null

    // 确保在没有明确指定默认序列化器时,会创建一个JdkSerializationRedisSerializer的实例作为默认的序列化器。
    // 默认情况下,它会使用Java对象的标准序列化方式来将对象序列化为字节流,以便存储到Redis中,或者将从Redis中读取的字节流反序列化为Java对象。
    
   defaultSerializer = new JdkSerializationRedisSerializer(	
   		// 如果存在(classLoader)类加载器,就是用classLoader来初始化JdkSerializationRedisSerializer,
   		// 否则,使用当前类的类加载器(this.getClass().getClassLoader())来初始化。
         classLoader != null ? classLoader : this.getClass().getClassLoader());
}
// 通过默认的序列化来处理所有的键和值,以确保序列化和反序列化的一致性
// 如果keySerializer为空
if (keySerializer == null) {
	//将keySerializer设置为defaultSerializer
	keySerializer = defaultSerializer;
	// 并将defaultUsed标记为true。
	defaultUsed = true;
}
if (valueSerializer == null) {
	valueSerializer = defaultSerializer;
	defaultUsed = true;
}
if (hashKeySerializer == null) {
	hashKeySerializer = defaultSerializer;
	defaultUsed = true;
}
if (hashValueSerializer == null) {
	hashValueSerializer = defaultSerializer;
	defaultUsed = true;
}

通过这种方式,即使没有明确指定特定的序列化器,也可以保证所有的键和值都会使用相同的序列化器进行处理,从而避免因序列化器不一致而引发的问题。

我们没有定义,它就用的jdk的序列化将数据传输

package com.sin.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

/**
 * @author sin
 * @date 2022/11/25
 * @apiNote
 */
@Configuration //表示该类为配置类
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        //实例化RedisTemplate
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        //设置连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);

        //指定k,v的序列化方式(json)
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        //默认序列化为json格式
        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);

        return redisTemplate;
    }
}

Json序列化

在这里插入图片描述

package com.sin.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author sin
 * @date 2022/11/25
 * @apiNote
 */
@Configuration //表示该类为配置类
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        //实例化RedisTemplate
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        //设置连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);

        //指定k,v的序列化方式(json)
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

//        //默认序列化为json格式
//        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
        //key设置为String类型
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //value设置为Json格式
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        return redisTemplate;
    }
}

key为String格式,value为Json格式
在这里插入图片描述

Springboot整合redis使用

RedisConfig

package com.sin.demo.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置类
 *
 */
@EnableCaching
@Configuration//标记该类为配置类,
public class RedisConfig {
    /**
     * 对读写的数据进行序列化操作
     * @param factory 连接工厂,
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){

        //实例化redisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //设置连接工厂
        redisTemplate.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

        //ObjectMapper以json进行读写操作
        ObjectMapper mapper = new ObjectMapper();
        //指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        redisSerializer.setObjectMapper(mapper);

        //设置默认的序列化
        //redisTemplate.setDefaultSerializer(new StringRedisSerializer());

        //实际开发中key为字符串序列化,value为json序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(redisSerializer);

        //hash序列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(redisSerializer);
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }

    /**
     * 对redis操作String数据类型
     * @param redisTemplate
     * @return
     */
    @Bean
    public ValueOperations<String,Object> valueOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForValue();
    }

    /**
     * Redis操作List数据类型
     * @param redisTemplate
     * @return
     */
    @Bean
    public ListOperations<String,Object> listOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForList();
    }

    /**
     * Redis操作Hash数据类型
     * @param redisTemplate
     * @return
     */
    @Bean
    public HashOperations<String,String,Object> hashOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForHash();
    }

    /**
     * Redis操作set数据类型
     * @param redisTemplate
     * @return
     */
    @Bean
    public SetOperations<String,Object> setOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForSet();
    }

    /**
     * Redis操作zSet数据类型
     * @param redisTemplate
     * @return
     */
    public ZSetOperations<String,Object> zSetOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForZSet();
    }

}

RedisTemplate封装

package com.sin.demo.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import sun.security.ec.point.ProjectivePoint;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redisTemplate封装
 */
@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 指定缓存失效时间
     * @param key
     * @param time
     * @return
     */
    public boolean expire(String key,long time){
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 指定key过期时间
     * @param key
     * @return
     */
    public long getExpire(String key){
        return redisTemplate.getExpire(key,TimeUnit.SECONDS);
    }

    /**
     * 判断key是否存在
     * @param key
     * @return
     */
    public boolean hasKey(String key){
        try {
            return redisTemplate.hasKey(key);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除缓存
     * @param key
     */
    @SuppressWarnings("unchecked")
    public void del(String ... key){
        if(key!=null&&key.length>0){
            if(key.length==1){
                redisTemplate.delete(key[0]);
            }else {
                redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
            }
        }
    }


    //=====================================================Redis的String数据类型操作=========================================

    /**
     * 普通缓存的获取
     * @param key
     * @return
     */
    public Object get(String key){
        return key==null?null:redisTemplate.opsForValue().get(key);
    }

    /**
     * 普通缓存的存放
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key,Object value){
        try{
            redisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 普通缓存放入并设置时间
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean set(String key,Object value,long time){
        try{
            if(time>0){
                redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);
            }else {
                set(key,value);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 递增
     * @param key
     * @param delta
     * @return
     */
    public long incr(String key,long delta){
        if (delta<0){
            throw new RuntimeException("递增的条件必须大于0");
        }
        return redisTemplate.opsForValue().increment(key,delta);
    }


    /**
     * 递减
     * @param key
     * @param delta
     * @return
     */
    public long decr(String key,long delta){
        if (delta<0){
            throw new RuntimeException("递减的条件不许大于0");

        }
        return redisTemplate.opsForValue().decrement(key,-delta);
    }

    //===============================================redis的Hash数据类型操作====================================================

    /**
     * Hashget
     * @param key 不能为null
     * @param itme 不能为null
     * @return
     */
    public Object hget(String key,String itme ){
        return redisTemplate.opsForHash().get(key,itme);
    }

    /**
     * 获取hashkey对应的所有键值
     * @param key
     * @return
     */
    public Map<Object,Object> hmget(String key){
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * HashSet
     */
    public boolean hmset (String key,Map<String,Object> map){
        try {
            redisTemplate.opsForHash().putAll(key,map);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * Hashset 设置时间
     * @param key
     * @param map
     * @param time
     * @return
     */
    public boolean hmset(String key,Map<String,Object>map,long time){
        try{
            redisTemplate.opsForHash().putAll(key,map);
            if(time>0){
                expire(key,time);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;

        }

    }

    /**
     * 向一张hash表中放入数据,如果不存在就将其创建
     * @param key
     * @param item 项
     * @param value
     * @return
     */
    public boolean hset(String key,String item,String value){
        try{
            redisTemplate.opsForHash().put(key,item,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 向一张hash表中放入数据,如果不存在就将其创建,对时间的操作
     * @param key
     * @param item
     * @param value
     * @param time
     * @return
     */
    public boolean hset(String key,String item,String value,long time){
        try{
            redisTemplate.opsForHash().put(key,item,value);
            if (time>0){
                expire(key,time);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 删除hash表中的值
     * @param key
     * @param item
     */
    public void hdel(String key,Object ... item){
        redisTemplate.opsForHash().delete(key,item);
    }

    /**
     * 判断Hash表中是否有该项的值
     * @param key
     * @param item
     * @return
     */
    public boolean hHasKey(String key,String item){
        return redisTemplate.opsForHash().hasKey(key,item);
    }

    /**
     * hash递增,如果不存在,就会创建一个,并把新增后的值返回
     * @param key
     * @param item
     * @param by  要增加几(大于0)
     * @return
     */
    public double hincr(String key,String item,double by){
        return redisTemplate.opsForHash().increment(key,item,by);
    }

    /**
     * 递减操作
     * @param key
     * @param item
     * @param by 要减少几(小于0)
     * @return
     */
    public double hdecr(String key,String item,double by){
        return redisTemplate.opsForHash().increment(key,item,by);
    }

    //===============================================redis的set数据类型操作====================================================

    /**
     * 根据key获取set中的所有值
     * @param key
     * @return
     */
    public Set<Object> sGet(String key){
        try {
            return redisTemplate.opsForSet().members(key);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据value从一个set中查询,是否存在
     * @param key
     * @param value
     * @return
     */
    public boolean sHasKey(String key,Object value){
        try {
            return redisTemplate.opsForSet().isMember(key,value);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 将数据放入set缓存中
     * @param key
     * @param values
     * @return 成功个数
     */
    public long sSet(String key,Object ... values){
        try{
            return redisTemplate.opsForSet().add(key,values);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 将set数据放入缓存
     * @param key
     * @param time
     * @param values
     * @return 成功个数
     */
    public long sSetAndTime(String key,long time,Object ... values){
        try{
            Long count = redisTemplate.opsForSet().add(key, values);
            if(time>0){
                expire(key,time);
            }
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取set缓存的长度
     * @param key
     * @return
     */
    public long sGetSetSize(String key){
        try {
            return redisTemplate.opsForSet().size(key);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 移除值为value的
     * @param key
     * @param values
     * @return
     */
    public long setRemove(String key,Object ... values){
        try {
            Long count = redisTemplate.opsForSet().remove(key, values);
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    //===============================================redis的list数据类型操作====================================================

    /**
     * 获取list缓存的内容
     * @param key
     * @param start 开始
     * @param end 结束(0到-1表示所有值)
     * @return
     */
    public List<Object> lGet(String key,long start,long end){
        try {
            return redisTemplate.opsForList().range(key,start,end);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取list缓存的长度
     * @param key
     * @return
     */
    public long lGetListSize(String key){
        try {
            return redisTemplate.opsForList().size(key);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }


    /**
     * 通过索引,获取list中的值
     * @param key
     * @param index 索引,index>=0时,0表示开头,1表示第二个元素;index《=0时,-1表示末尾,-2表示第二个元素,
     * @return
     */
    public Object lGetIndex(String key,long index){
        try{
            return redisTemplate.opsForList().index(key,index);
        }catch (Exception e ){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将list放入缓存
     * @param key
     * @param value
     * @return
     */
    public boolean lSet(String key,Object value){
        try {
            redisTemplate.opsForList().rightPush(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 将list放入缓存中,对时间操作
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean lSet(String key,Object value,long time){
        try {
            redisTemplate.opsForList().rightPush(key,value);
            if (time>0){
                expire(key,time);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 将list放入缓存
     * @param key
     * @param values
     * @return
     */
    public boolean lSetList(String key,List<Object> values){
        try {
            redisTemplate.opsForList().rightPushAll(key,values);
            return true;

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 将list放入缓存中,对时间操作
     * @param key
     * @param values
     * @param time
     * @return
     */
    public boolean lSetList(String key,List<Object> values ,long time){
        try {
            redisTemplate.opsForList().rightPushAll(key,values);
            if(time>=0){
                expire(key,time);
            }
            return true;

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据索引修改list中的数据
     * @param key
     * @param index
     * @param value
     * @return
     */
    public boolean lUpdateIndex(String key,long index,Object value){
        try {
            redisTemplate.opsForList().set(key,index,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 移除n个值
     * @param key
     * @param count
     * @param value
     * @return
     */
    public long lRemove(String key,long count,Object value){
        try{
            return redisTemplate.opsForList().remove(key,count,value);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

}

编写实体类

public class Student {
    private Integer id;
    private String name ;
    private Integer age;
    private String dept;//班级
    set,get方法
    }

RedisController类

package com.sin.controller;

import com.sin.pojo.Student;
import com.sin.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sin
 * @date 2022/11/25
 * @apiNote
 */
@RestController
public class RedisController {

    //注入RedisTemplate
    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private RedisUtil redisUtil;

    //添加数据
    @PostMapping("/addData/key={key}/value={value}")//动态地址
    public Object addData(@PathVariable("key") String key,
                          @PathVariable("value") String value){
        redisTemplate.opsForValue().set(key,value);
        System.out.println("数据添加成功"+redisTemplate);
        return "addData Success";
    }

    //根据key获取value
    @GetMapping("/getData/key={key}")
    public Object getData(@PathVariable("key") String key){
       Object value =  redisTemplate.opsForValue().get(key);
        System.out.println("数据为:"+value);
        return redisTemplate.opsForValue().get(key);
    }



    /**
     * 添加list对象数据到redis中
     * @return
     */
    @RequestMapping(value = "/setList")
    public boolean setList(){
        List<Object> stuList = new ArrayList<>();
        Student student = new Student();
        student.setId(1);
        student.setAge(23);
        student.setDept("saas");
        student.setName("张三");

        Student student1 = new Student();
        student1.setId(2);
        student1.setAge(22);
        student1.setDept("微服务");
        student1.setName("李四");

        Student student2 = new Student();
        student2.setId(3);
        student2.setAge(23);
        student2.setDept("saas");
        student2.setName("王五");

        Student student3 = new Student();
        student3.setId(4);
        student3.setAge(24);
        student3.setDept("微服务");
        student3.setName("赵六");

        stuList.add(student);
        stuList.add(student1);
        stuList.add(student2);
        stuList.add(student3);
        return redisUtil.lSetList("student",stuList);
    }

    /**
     * 获取全部数据
     * @return
     */
    @RequestMapping(value = "getList")
    public  Object getList(){
        return redisUtil.lGet("student",0,-1);
    }

}

在这里插入图片描述
在这里插入图片描述

SpringBoot整合Redis+Mybatis

​ SpringBoot+SpringMVC+redis+mysql来实现项目中的crud操作,redis做缓存数据库,针对频繁需要查询或者解决单点问题都会把数据库到redis来分配服务器压力。

实体类

create table demo_user(
    id int(10) auto_increment primary key not null ,
    user_name varchar(40) not null ,
    password varchar(10) not null ,
    sex int(10) not null );
    
    alter table demo_user charset  = utf8;

alter table demo_user convert to character set utf8;
public class DemoUser implements Serializable {


  private long id;
  private String userName;
  private String password;
  private long sex;
  
    // setter getter方法
}

dao层

package com.sin.mapper;

import com.sin.pojo.DemoUser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * dao层
 */
@Repository
@Mapper
public interface DemoUserMapper {
    //用户列表
    @Select("select * from demo_user")
    @Results({
            @Result(property = "userName",column = "user_name"),
            @Result(property = "password",column = "password")
    })
    List<DemoUser> queryAll();

    //根据id获取demo_user
    @Select("select * from demo_user where id = #{id}")
    @Results({
            @Result(property = "userName",column = "user_name"),
            @Result(property = "password",column = "password")
    })
    DemoUser findUserById(int id);
    
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    @Insert("insert into demo_user(id,user_name,password,sex)values" +
            "(#{id},#{userName},#{password},#{sex})")
    int add(DemoUser user);

    @Select("select * from demo_user where user_name = #{userName} ")
    @Results({
            @Result(property = "userName",column = "user_name"),
            @Result(property = "password",column = "password")
    })
    DemoUser findUserByName(String userName);

    //根据id修改demoUser
    @Update("update demo_user set user_name=#{userName},password=#{password}," +
            "sex=#{sex} where id=#{id}")
    int updateUser(DemoUser demoUser);

    //删除用户
    @Delete("delete from demo_user where id = #{id}")
    int deleteUserById(int id);


}

config

package com.sin.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
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.connection.RedisInvalidSubscriptionException;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * redis 配置类
 * 配置redisTemplate序列化
 */
public class RedisConfig extends CachingConfigurerSupport {
    /**
     *  选择redis作为默认缓存工具
     *
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){
        //设置缓存时间为一个小时
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(1));
        return RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory))
                .cacheDefaults(redisCacheConfiguration).build();
    }

    /**
     * 配置redisTemplate相关配置
     *
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        //配置连接工厂
        redisTemplate.setConnectionFactory(factory);

        //使用Jackson2JsonRedisSerializer来序列化何反序列化redis的value值
        Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        //指定要序列化的域,field,get和set,以及修饰符范围,ANY都是有private和public
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //指定序列化输入的类型,类必须时非final修饰的,
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jsonRedisSerializer.setObjectMapper(objectMapper);

        //值采用json序列化
        redisTemplate.setValueSerializer(jsonRedisSerializer);
        //使用StringRedisSerrializer来序列化和反序列化redis的key值
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //设置hash key 和value序列化模式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jsonRedisSerializer);
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }


    /*
     * 对hash类型的数据操作
     */
    @Bean
    public HashOperations<String,String,Object> hashOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForHash();
    }

    /**
     * 对redis 字符串String类型数据操作
     */
    @Bean
    public ValueOperations<String,Object> valueOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForValue();
    }
    /**
     * 对redis 链表list类型数据操作
     */
    @Bean
    public ListOperations<String,Object>listOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForList();
    }
    /**
     * 对redis 无序集合set类型数据操作
     */
    @Bean
    public SetOperations<String,Object> setOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForSet();
    }

    /**
     * 对redis 有序结合zset类型数据操作
     */
    @Bean
    public ZSetOperations<String,Object> zSetOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForZSet();
    }

}

service

public interface DemoUserService {

    //查询全部
    List<DemoUser> queryAll();
    //根据id查询
    DemoUser findUserById(int id);
    //根据id进行修改
    int updateUser(DemoUser demoUser);
    //根据id进行删除
    int deleteUserById(int id);
    
    //添加数据
    int addUser(DemoUser user);
}

impl

package com.sin.service.impl;

import com.sin.mapper.DemoUserMapper;
import com.sin.pojo.DemoUser;
import com.sin.service.DemoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class DemoUserServiceImpl implements DemoUserService {

    @Autowired
    private DemoUserMapper demoUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    //查询全部操作
    @Override
    public List<DemoUser> queryAll() {
        return demoUserMapper.queryAll();
    }

    /**
     * 根据id进行查询
     * 获取用户策略,先从缓存中获取用户,没有则读取mysql数据,再将读取的数据写入缓存中
     */
    @Override
    public DemoUser findUserById(int id) {
        String key = "user_" + id;
        ValueOperations<String,DemoUser> operations = redisTemplate.opsForValue();
        //判断redis中是否有键为key的缓存
        boolean haskey = redisTemplate.hasKey(key);
        if (haskey){
            DemoUser user = operations.get(key);
            System.out.println("从缓存中获取的数据:"+user.getUserName());
            System.out.println("----------------------------------");
            return user;
        }else {
            DemoUser user = demoUserMapper.findUserById(id);
            System.out.println("查询数据库中的数据"+ user.getUserName());
            System.out.println("--------------------写入数据------------------");
            //写入数据
            operations.set(key,user,5, TimeUnit.HOURS);
            return user;
        }
    }

    /**
     * 更新用户
     * 先更新数据库,成功之后,删除原来的缓存,再更新缓存
     * @param demoUser
     * @return
     */
    @Override
    public int updateUser(DemoUser demoUser) {
        ValueOperations<String,DemoUser> operations = redisTemplate.opsForValue();
        int result = demoUserMapper.updateUser(demoUser);
        if (result!=0){
            String key = "user_"+demoUser.getId();
            boolean haskey = redisTemplate.hasKey(key);
            if (haskey){
                redisTemplate.delete(key);
                System.out.println("删除缓存中的key----"+key);
            }
            //将更新后的数据加入缓存中
            DemoUser demoUserNew = demoUserMapper.findUserById((int) demoUser.getId());
            if (demoUserNew != null){
                operations.set(key,demoUserNew,3,TimeUnit.HOURS);
            }
        }
        return result;
    }

    /**
     * 根据id删除
     * 删除数据表中的数据,然后删除缓存
     * @param id
     * @return
     */
    @Override
    public int deleteUserById(int id) {
        int result = demoUserMapper.deleteUserById(id);
        String key = "user_" + id;
        if (result != 0){
            boolean haskey = redisTemplate.hasKey(key);
            if (haskey){
                redisTemplate.delete(key);
                System.out.println("删除了缓存中的key"+ key);
            }
        }
        return result;
    }
     @Override
    public int addUser(DemoUser user) {
        DemoUser user1 = demoUserMapper.findUserByName(user.getUserName());
        int result;
        if (user1 != null){
            result = 0;
            return result;
        }else{
            ValueOperations<String,Object> operations = redisTemplate.opsForValue();
            result = demoUserMapper.add(user);
            if (result != 0 ){
                String key  = "user_" +user.getId();
                boolean haskey = redisTemplate.hasKey(key);
                if(haskey){
                    redisTemplate.delete(key);
                }
                if (user != null){
                    operations.set(key,user,3,TimeUnit.HOURS);
                }
            }
            return result;
        }

    }
}

controller

package com.sin.controller;

import com.sin.pojo.DemoUser;
import com.sin.service.DemoUserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DemoUserController {

    @Autowired
    private DemoUserService demoUserService;

    @RequestMapping("/queryAll")
    public List<DemoUser> queryAll(){
        List<DemoUser> list = demoUserService.queryAll();
        return list;
    }
    @RequestMapping("/addUser")
    public String addUser(){
        DemoUser user = new DemoUser();
        user.setUserName("李四");
        user.setPassword("12112");
        user.setSex(1);
        user.setBirthday( new Date());
        int resutl= demoUserService.addUser(user);
        if (resutl != 0){
            return "add DemoUser success";

        }
        return "add demouser fail";
    }


    @RequestMapping("/findUserById")
    public Map<String,Object> findUserById(@RequestParam int id){
        DemoUser user = demoUserService.findUserById(id);
        Map<String,Object> result = new HashMap<>();
        result.put("id",user.getId());
        result.put("username",user.getUserName());
        result.put("pwd",user.getPassword());
        result.put("sex",user.getSex());
        result.put("birthday",user.getBirthday());
        return result;
    }

    @RequestMapping("/updateUser")
    public String updateUser(){
        DemoUser user = new DemoUser();
        user.setId(1);
        user.setUserName("张三");
        user.setPassword("123456");
        user.setSex(18);
        user.setBirthday((Timestamp) new Date());

        int result = demoUserService.updateUser(user);

        if (result != 0){
            return "update user success";
        }
        return "fall";
    }


    @RequestMapping("/deleteUserById")
    public String deleteUserById(@RequestParam int id){
        int result = demoUserService.deleteUserById(id);
        if (result != 0){
            return "delete Success";
        }
        return "delete fall";
    }




}

在这里插入图片描述
加入QQ技术交流群(432972658)

在这里插入图片描述

  • 9
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
要实现Spring Boot整合RedisMySQL,并将数据从MySQL导入Redis,可以按照以下步骤进行: 1. 在pom.xml文件中添加RedisMySQL的依赖。 2. 在application.properties文件中配置RedisMySQL的连接信息。 3. 创建一个RedisConfig类,用于配置RedisTemplate。 4. 创建一个MySQLConfig类,用于配置DataSource和JdbcTemplate。 5. 创建一个Service类,用于从MySQL中读取数据,并将数据存储到Redis中。 6. 在Controller中调用Service类中的方法,从Redis中读取数据并返回给前端。 下面是一个简单的示例代码: 1. pom.xml文件中添加RedisMySQL的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 2. application.properties文件中配置RedisMySQL的连接信息: ``` # Redis spring.redis.host=localhost spring.redis.port=6379 # MySQL spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root ``` 3. 创建一个RedisConfig类,用于配置RedisTemplate: ``` @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } ``` 4. 创建一个MySQLConfig类,用于配置DataSource和JdbcTemplate: ``` @Configuration public class MySQLConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } } ``` 5. 创建一个Service类,用于从MySQL中读取数据,并将数据存储到Redis中: ``` @Service public class UserService { private final JdbcTemplate jdbcTemplate; private final RedisTemplate<String, Object> redisTemplate; public UserService(JdbcTemplate jdbcTemplate, RedisTemplate<String, Object> redisTemplate) { this.jdbcTemplate = jdbcTemplate; this.redisTemplate = redisTemplate; } public List<User> getAllUsers() { List<User> users = jdbcTemplate.query("SELECT * FROM user", new BeanPropertyRowMapper<>(User.class)); users.forEach(user -> redisTemplate.opsForValue().set("user:" + user.getId(), user)); return users; } } ``` 6. 在Controller中调用Service类中的方法,从Redis中读取数据并返回给前端: ``` @RestController public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping("/users") public List<User> getAllUsers() { List<User> users = (List<User>) userService.redisTemplate.opsForValue().get("users"); if (users == null) { users = userService.getAllUsers(); userService.redisTemplate.opsForValue().set("users", users); } return users; } } ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆卿之

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值