源码地址:https://gitee.com/ytfs-dtx/SpringBoot
Redis-Desktop-Manager2020地址:
https://ytsf.lanzous.com/b01bei1bc
密码:2qan
集成Spring Data JPA
添加Spring Data JPA的起步依赖
添加数据库驱动依赖
在application.yml中配置数据库和JPA的相关属性
#DB Configuration
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
jpa:
database: mysql
show-sql: true
generate-ddl: false
hibernate:
ddl-auto: update
创建实体配置实体
package xyz.ytfs.entity;
import javax.persistence.*;
/**
* @author by 雨听风说
* @Classname User
* @Description TODO(用户的JavaBean)
* @Date 2020/5/13 17:09
*/
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/*
@Column属性:当数据库的列明与实体类的属性名一致的时候可以不用配置
*/
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "name")
private String name;
//此处省略setter和getter方法... ...
}
编写测试类
package xyz.ytfs.test;
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.test.context.junit4.SpringRunner;
import xyz.ytfs.SpringBootJpaApplication;
import xyz.ytfs.repository.UserRepository;
/**
* @author by 雨听风说
* @Classname JpaTest
* @Description TODO(JPA的测试)
* @Date 2020/5/13 17:18
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootJpaApplication.class)
public class JpaTest {
@Autowired
private UserRepository userRepository;
@Test
public void testFindALl() {
this.userRepository.findAll().forEach(System.out::println);
}
}
控制台打印信息
集成Redis--为了测试方便使用windows版的Redis
添加Redis起步依赖
启动redis服务器
配置Redis连接信息
#DB Configuration
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
jpa:
database: mysql
show-sql: true
generate-ddl: false
hibernate:
ddl-auto: update
#redis
redis:
port: 6399
host: 127.0.0.1
注入RedisTemplate测试redis
package xyz.ytfs.test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;
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 xyz.ytfs.SpringBootJpaApplication;
import xyz.ytfs.entity.User;
import xyz.ytfs.repository.UserRepository;
import java.util.List;
/**
* @author by 雨听风说
* @Classname RedisTest
* @Description TODO(Redis 测试)
* @Date 2020/5/13 20:55
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootJpaApplication.class)
public class RedisTest {
/*
泛型代表的是key-value的类型
*/
@Autowired
private RedisTemplate
@Autowired
private UserRepository repository;
/*
jackson的序列化工具,springBoot自动集成redis
*/
private static final ObjectMapper MAPPER = new ObjectMapper();
@Test
public void test() throws JsonProcessingException {
//1.从Redis中获取数据 数据格式json字符串
String usersJson = this.redisTemplate.boundValueOps("user.findAll").get();
//2.判断redis中书否存在这个数据
if (StringUtils.isBlank(usersJson)){
//3.不存在:数据库查询
List
//将查询出的数据存储到redis缓存中,先将users集合转换成json格式字符串 使用jackson进行转换
usersJson = MAPPER.writeValueAsString(users);
//存储在redis中
this.redisTemplate.boundValueOps("user.findAll").set(usersJson);
System.out.println("---------------------------------------");
System.out.println("\"从数据库中查询数据\" = " + "从数据库中查询数据");
}else {
System.out.println("\"从redis中获取数据\" = " + "从redis中获取数据");
}
//4.存在:直接返回(测试直接在控制台打印)
System.out.println("usersJson = " + usersJson);
}
}
第一次执行
从数据库中获取数据
第二次执行
从redis中获取数据
打开redis可视化工具(redis desktop manager),发现键值已存在