Spring Boot 整合redis

Spring Boot 整合redis

application.properties

# 配置redis
spring.redis.host=39.105.24.26
spring.redis.password=liaogui

application.yml

# druid连接池
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    driver-class-name: com.mysql.cj.jdbc.Driver
    platform: mysql
    url: jdbc:mysql://localhost:3306/liaogui?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: liaogui
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    filters: stat,wall,log4j2
    logSlowSql: true

重写redisTemplate,指定序列化方式Json

RedisConfig

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;

@Configuration
public class RedisConfig {
    /*
    重写
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        //指定自己的序列化方式 用json,序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer
                =new Jackson2JsonRedisSerializer(Object.class);
        template.setDefaultSerializer(jackson2JsonRedisSerializer);
        return template;
    }
}

UsersPlusController.java(控制层)

import com.xmx.entity.UsersPlus;
import com.xmx.service.IUsersPlusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

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

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 

* 前端控制器 *

* * @author jobob * @since 2021-07-19 */
@RestController @RequestMapping("/xmx/") public class UsersPlusController { @Autowired IUsersPlusService iUsersPlusService; @GetMapping("getid/{id}") //@PathVariable 将url中占位符参数{}绑定到处理器类方法行参中 public Object getId(@PathVariable("id") Integer id){ ExecutorService es = Executors.newFixedThreadPool(100); for (int i=0;i<=500;i++){ es.submit(new Runnable() { @Override public void run() { Object usersPlus = iUsersPlusService.findById(id); } }); } Object usersPlus = iUsersPlusService.findById(id); return usersPlus; } }

IUsersPlusService.java(接口)

import com.xmx.entity.UsersPlus;
import com.baomidou.mybatisplus.extension.service.IService;

/**
 * 

* 服务类 *

* * @author jobob * @since 2021-07-19 */
public interface IUsersPlusService extends IService<UsersPlus> { Object findById(Integer id); }

UsersPlusServiceImpl.java(服务层)

import com.xmx.entity.UsersPlus;
import com.xmx.mapper.UsersPlusMapper;
import com.xmx.service.IUsersPlusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 

* 服务实现类 *

* * @author jobob * @since 2021-07-19 */
@Service public class UsersPlusServiceImpl extends ServiceImpl<UsersPlusMapper, UsersPlus> implements IUsersPlusService { /** * 第一次查询用数据库,其他的时候用redis * @param id * @return */ @Autowired private RedisTemplate redisTemplate; @Override public Object findById(Integer id) { Object obj = redisTemplate.opsForValue().get(id); if (obj==null){ //锁住字节码文件 synchronized (this.getClass()){ if (obj==null){ //查数据库 UsersPlus usersPlus = getById(id); //存入redis redisTemplate.opsForValue().set(id,usersPlus); System.out.println("存入redis"); return usersPlus; }else { System.out.println("查询redis"); return obj; } } }else{ //查缓存 System.out.println("查询redis "); return obj; } } }

UsersPlus.java(实体类)

import com.xmx.entity.UsersPlus;
import com.xmx.mapper.UsersPlusMapper;
import com.xmx.service.IUsersPlusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 

* 服务实现类 *

* * @author jobob * @since 2021-07-19 */
@Service public class UsersPlusServiceImpl extends ServiceImpl<UsersPlusMapper, UsersPlus> implements IUsersPlusService { /** * 第一次查询用数据库,其他的时候用redis * @param id * @return */ @Autowired private RedisTemplate redisTemplate; @Override public Object findById(Integer id) { Object obj = redisTemplate.opsForValue().get(id); if (obj==null){ //锁住字节码文件 synchronized (this.getClass()){ if (obj==null){ //查数据库 UsersPlus usersPlus = getById(id); //存入redis redisTemplate.opsForValue().set(id,usersPlus); System.out.println("存入redis"); return usersPlus; }else { System.out.println("查询redis"); return obj; } } }else{ //查缓存 System.out.println("查询redis "); return obj; } } }

你可能感兴趣的:(Spring,Boot,redis)