springboot simple query integration redis

Creating a project springboot

Here Insert Picture Description

Configuration yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
    username: root
    password: root

  redis:
    host: 192.168.233.128
    #服务器地址
    port: 6379
    #服务器端口
    database: 1
    # Redis数据库索引(默认为0) 密码之类的 默认是没有的可以写

logging:
  level:
    com.jianglin.demo_redis.dao: debug
    # 打印SQL语句

Configuration config class

Here Insert Picture Description

config class

package com.huipeng.redisdome.config;

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 java.net.UnknownHostException;
@Configuration
public class StudentConfig {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //修改默认的序列化规则
        //1.创建序列化规则对象
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
        //2.更改默认的序列化规则
        template.setDefaultSerializer(jackson2JsonRedisSerializer);
        return template;
    }
}

## 编写实体类pojo层

Here insert the code sheets

package com.huipeng.redisdome.pojo;

import lombok.Data;

@Data
public class Student {

    private Integer id;
    private Integer gradeid;
    private String name;
    private String sex;
}

Write Mapper layer

package com.huipeng.redisdome.dao;

import com.huipeng.redisdome.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface StudentMapper {

    @Select("select * from student")
    List<Student> selAll();

}

Write and implement service layer class

package com.huipeng.redisdome.service;

import com.huipeng.redisdome.pojo.Student;

import java.util.List;

public interface StudentServlce {

    List<Student> selAll();
}

实现类

package com.huipeng.redisdome.service;

import com.huipeng.redisdome.dao.StudentMapper;
import com.huipeng.redisdome.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentSercieImpl implements StudentServlce {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Student> selAll() {
        String key = "student";
        ListOperations<String, Student> operations = redisTemplate.opsForList();

        // 缓存存在
        boolean hasKey = redisTemplate.hasKey(key);

        if (hasKey) {

            return operations.range(key,0,-1);
        }else{
            List<Student> list = studentMapper.selAll();
            System.out.println(list);
            operations.leftPushAll(key, list);
            return list;
        }
    }
}


Write controller layer

package com.huipeng.redisdome.controller;

import com.huipeng.redisdome.pojo.Student;
import com.huipeng.redisdome.service.StudentServlce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private StudentServlce studentServlce;

    @GetMapping("/findAll")
    public List<Student> findAll(){
        return studentServlce.selAll();
    }
}

Finally, look at the start that there is no added this commentHere Insert Picture Description

Write out a simple query

Guess you like

Origin blog.csdn.net/zhp9364/article/details/91414857