springBoot-redis整合
写在前面,使用的持久层实现是hibernate,redis测试时同时兼容了jpa的整合
####1.springBoot-redis整合之环境配置
<?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>
<groupId>com.xcl</groupId>
<artifactId>springboot_redis</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MySQL连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springBoot JPA的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--jdk9需要导入如下坐标-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<!-- 配置使用redis启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
####2.springBoot-redis整合之实体类---->redis保存的结构是<String,Object>实体类需要序列化
package com.xcl.domain;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name="tb_student")
public class Student implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="stu_id")
private Long stuId;
@Column(name="stu_name")
private String stuName;
@Column(name="stu_age")
private String stuAge;
public Student() {
}
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", stuName='" + stuName + '\'' +
", stuAge='" + stuAge + '\'' +
'}';
}
public Student(String stuName, String stuAge) {
this.stuName = stuName;
this.stuAge = stuAge;
}
@Column(name="stu_id")
public Long getStuId() {
return stuId;
}
public void setStuId(Long stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuAge() {
return stuAge;
}
public void setStuAge(String stuAge) {
this.stuAge = stuAge;
}
}
####3.springBoot-redis整合之dao接口
package com.xcl.dao;
import com.xcl.domain.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface StudentDao extends JpaRepository<Student,Long>,JpaSpecificationExecutor<Student> {
}
####4.pringBoot-redis整合之配置文件application.properties
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/boot?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#JPA Configuration:
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
#Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
####5.pringBoot-reids整合之测试–>只要有缓存,就要有缓存同步,缓存不可影响原来逻辑的正常运行
package com.xcl.test;
import com.xcl.MySpringBootApplication;
import com.xcl.dao.StudentDao;
import com.xcl.domain.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringBootApplication.class)
public class StudentDaoTest {
@Autowired
private StudentDao studentDao;
/**
* 测试springdataJpa
*/
@Test
public void testSave(){
Student student = new Student("小黑", "0.5");
studentDao.save(student);
}
@Autowired
private RedisTemplate redisTemplate;
/**
* 测试缓存:只要有缓存就要有缓存同步,缓存不能影响程序的正常运行
*
* 注意事项:如果实体类有带参构造,那么必须申明无参构造,进行缓存时必须实现序列化接口
*/
@Test
public void testRedis(){
//从缓存库中读出数据
String id = 1+"";
Student student = (Student) redisTemplate.boundValueOps("student:" + id).get();
if (student == null){
//如果缓存库中没有,从数据库中获取
Optional<Student> byId = studentDao.findById(Long.parseLong(id));
System.out.println(byId.isPresent());
student = byId.get();
System.out.println(student);
//缓存同步
redisTemplate.boundValueOps("student:"+id).set(student);
System.out.println("从数据库中读取数据");
}else {
System.out.println("从缓存中读取数据");
}
}
}
恭喜你完成了整合,可以开始你的代码之旅了!!!