您的位置:首页 > 数据库 > Redis

springboot 简单查询 整合redis

2019-06-11 10:39 459 查看

创建一个springboot项目

配置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语句

配置config类

config类

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层

在这里插入代码片

package com.huipeng.redisdome.pojo;

import lombok.Data;

@Data
public class Student {

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

编写Mapper层

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();

}

编写service层和实现类

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;
}
}
}

编写controller层

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();
}
}

最后看一下启动有没有这添加这个注解

简单查询就写出来了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: