版权声明:本文为博主原创文章,未经博主允许不得转载
redis简介
redis介绍
(1)redis是一个key-vlue 存储系统。和Memcached 类似,它支持存储的value 类型相对更多,包括String(字符串),list(链表),hash(散表),set(集合),zset(有序集合),这些数据类型都支持 push/pop,add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。
(2)在此基础上redis支持各种不同的排序,与memcached 一样为了保证效率,数据都是缓存在内存中的,区别是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步
为什么使用缓存
大规模互联网应用数据 过多时候采取的方案,采用key-value形式存储,这样可以极大增强系统的可扩展性。rdbms形式 拓展性太弱
云存储,大部分云计技术都会采用key-value的数据格式实现,因为普通的rdbms的数据结构无法满足 key-value的结构分布式框架决定了只要有更多的机器,就支持更多的数据存储,并且支持数量很多的并发查询官方给出的数据可以到达11w每秒,对于rdbms来说,链接呢最多可以设置到几百个并发。
开发工具:idea
SpringBoot:2.13版本
redis:3.2版本
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
创建项目
2.
3.
get set
4.
项目测试:
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mr</groupId>
<artifactId>springboot_redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_redis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--alibaba的druid数据库连接池 (德鲁伊)-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
2.application.properties
server.port=8082
#连接数据源
spring.datasource.druid.username=用户名
spring.datasource.druid.password=密码
spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/数据库名?serverTimezone=GMT%2B8
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.initial-size=5
#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
#spring.redis.password=123
mybatis 配置
#使用mybatis配置文件,需要指定该文件的文件路径
#指定mapper.xml文件的路径,如果mapper.xml与接口在一起则不需要该配置
#扫描pojo包中的实体类并起别名
#日志级别改为debug可以显示sql语句,logging.level后为存放mapper接口的包
#mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.mr.model
logging.level.com.mr.mapper=debug
开启驼峰命名法 mybatis.configuration.map-underscore-to-camel-case: true
mybatis.configuration.map-underscore-to-camel-case=true
``
3.Car.java
package com.mr.model;
import lombok.Data;
import java.io.Serializable;
/**
*
*/
@Data
public class Car implements Serializable{
private Integer carId;
private String carName;
private Integer carPrice;
}
4.CarController.java
package com.mr.controller;
import com.mr.model.Car;
import com.mr.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
*
*/
@Controller
@RequestMapping("car")
public class CarController {
@Autowired
private ICarService carService;
/**
* 查询全部
* @return
* //modelMap支持默认请求视图返回(与new HashMap最大区别)
*/
@GetMapping("queryAll")
public String queryAll(ModelMap map){
List<Car> list=carService.queryAll();
map.put("list",list);
return "list";
}
}
5.ICarService.java
package com.mr.service;
import com.mr.model.Car;
import java.util.List;
/**
*
*/
public interface ICarService {
//查询全部
List<Car> queryAll();
}
6.CarServiceImpl.java
import com.mr.mapper.CarMapper;
import com.mr.model.Car;
import com.mr.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import java.util.List;
/**
*
*/
@Service(value="carService")
public class CarServiceImpl implements ICarService {
@Autowired
private CarMapper carMapper;
/**
* 注入模板序列化
* @return
*/
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
//查询全部
@Override
public List<Car> queryAll() {
//字符串的序列化器
RedisSerializer redisSerializer=new StringRedisSerializer();
redisTemplate.setKeySerializer(redisSerializer);
//首先查一下缓存数据
List<Car> list= (List<Car>) redisTemplate.opsForValue().get("selectCar");
//判断
if(list==null){
//如果是缓存空的话去查数据库
list=carMapper.queryAll();
//把查询的值放进缓存中
redisTemplate.opsForValue().set("selectCar",list);
}
return list;
}
}
``
7.CarMapper.java
import com.mr.model.Car;
import java.util.List;
/**
*
*/
public interface CarMapper {
List<Car> queryAll();
}
8.CarMapper.xml
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mr.mapper.CarMapper">
<!-- List<Car> queryAll();-->
<select id="queryAll" resultType="car">
SELECT * from t_car
</select>
</mapper>
9.list.ftl
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<#list list as car>
<tr>
<td>${car.carId}</td>
<td>${car.carName}</td>
<td>${car.carPrice}</td>
</tr>
</#list>
</table>
</body>
</html>