springBoot整合Spring-Data-JPA, Redis Redis-Desktop-Manager2020 windows
2021-01-23 07:13
标签:取数 ffffff pos aic autowire set show 查询 二次 源码地址:https://gitee.com/ytfs-dtx/SpringBoot Redis-Desktop-Manager2020地址: https://ytsf.lanzous.com/b01bei1bc 密码:2qan ?
? 从数据库中获取数据 ? 从redis中获取数据 ? ? 集成Spring Data JPA
添加Spring Data JPA的起步依赖
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
添加数据库驱动依赖
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
dependency>
在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起步依赖
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-data-redisartifactId>
dependency>
启动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
第一次执行
第二次执行
打开redis可视化工具(redis desktop manager),发现键值已存在
文章标题:springBoot整合Spring-Data-JPA, Redis Redis-Desktop-Manager2020 windows
文章链接:http://soscw.com/index.php/essay/45795.html