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(); } }
最后看一下启动有没有这添加这个注解
简单查询就写出来了
相关文章推荐
- SpringBoot2.X 整合RedisTemplate 简单实现消息队列
- Java爬虫初体验:简单抓取IT之家热评(整合Spring Boot+Elasticsearch+Redis+Mybatis)
- Redis使用(二):SpringBoot整合Redis 配置文件及项目种简单应用
- SpringBoot整合redis简单例子
- 【spring boot】SpringBoot初学(8)– 简单整合redis
- springboot与redis的简单整合实例
- SpringBoot 整合 Redis 的简单案例
- SpringBoot 整合 Redis 的简单案例
- SpringBoot整合redis集群并使用StringRedisTemplate和RedisTemplate简单操作Redis集群
- spring boot进阶(一) springboot整合redis,可操作java对象。最完整、简单易懂、详细的spring boot教程。
- spring boot 整合redis对查询数据做缓存( 利用spring的AOP技术)
- spring boot 整合redis对查询数据做缓存( 利用spring的AOP技术)
- Spring Boot整合集成StringRedisTemplate的简单记录
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo
- spring boot整合Mybatis如何查询某一时间段内的信息
- SpringBoot整合Kafka:简单收发消息案例
- SpringBoot整合MybatisPlus的简单教程实现(简单整合)
- SpringBoot整合Redis集群
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
- SpringBoot之整合redis实现缓存