idea springBoot integration redis

Use spring boot integration redis

Redis installed
at liunx
pulling official mirror, downloads

docker pull redis

Look at the success:

docker images

Start rides Mirror

docker run -p 6379:6379 -d redis:latest redis-server

Then use spring boot integration redis

	/** 架包依赖*/
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

application.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.139.129 
        #服务器地址
        port: 6379
        #服务器端口
        database: 1
        # Redis数据库索引(默认为0) 密码之类的 默认是没有的可以写

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

Here Insert Picture Description

This is not written in detail entity class

dao layer

     @Mapper
    public interface StudentMapper {
    
        @Select("select * from student")
        public List<Student> findAll();
    }


servlce
public interface StudentServlce {

     List<Student> findAll();
}

Implementation class

@Service
public class StudentServlceImpl implements StudentServlce {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Student> findStudentAll() {
        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.findStudentAll();
            System.out.println(list);
            operations.leftPushAll(key, list);
            return list;
        }
    }
}

Controller

@RestController
public class StudentController {

    @Autowired
    private StudentServlce studentServlce;

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

This is then complete!
Here Insert Picture Description
Mainly servlce implementation class, implemented redis cache! !

Guess you like

Origin blog.csdn.net/DLLLL_/article/details/91411809