本文基于Springboot2.0,使用mysql数据库,通过jpa实现orm,再用redis实现数据库的缓存。
目录
1、项目结构
2、环境配置
3、代码
4、测试
5、参考文章
1、项目结构
项目结构
2、环境配置
1)pom.xml
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-data-redis
2)application.yml
注意:
创建mysql数据库名称为 jpa_redis
redis 端口、
mysql 数据库账号密码
server:
port: 8081
# context-path: /
spring:
redis:
host: localhost
port: 6379
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/jpa_redis
username: root
password: abc
jpa:
hibernate:
ddl-auto: update
show-sql: true
3、代码
1)SpringBootRedisApplication
注意添加 @EnableCaching
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class SpringBootRedisApplication
{
public static void main(String[] args)
{
SpringApplication.run(SpringBootRedisApplication.class, args);
}
}
2)RedisConfig
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCac