mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Spring Boot 注解实现整合 Redis 作为缓存
This commit is contained in:
58
springboot-mybatis-redis-annotation/pom.xml
Executable file
58
springboot-mybatis-redis-annotation/pom.xml
Executable file
@@ -0,0 +1,58 @@
|
||||
<?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>springboot</groupId>
|
||||
<artifactId>springboot-mybatis-redis-annotation</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>springboot-mybatis-redis-annotation :: 注解实现整合 Redis 作为缓存</name>
|
||||
|
||||
<!-- Spring Boot 启动父依赖 -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.3.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
|
||||
<mysql-connector>5.1.39</mysql-connector>
|
||||
<fastjson-version>1.2.32</fastjson-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring Boot Data Cache 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Data Redis 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Web 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Test 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.spring.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Spring Boot 应用启动类
|
||||
*
|
||||
* Created by bysocket on 16/4/26.
|
||||
*/
|
||||
// Spring Boot 应用的标识
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 程序启动入口
|
||||
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
|
||||
SpringApplication.run(Application.class,args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.spring.springboot.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 城市实体类
|
||||
*
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
public class City implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -1L;
|
||||
|
||||
/**
|
||||
* 城市编号
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 省份编号
|
||||
*/
|
||||
private Long provinceId;
|
||||
|
||||
/**
|
||||
* 城市名称
|
||||
*/
|
||||
private String cityName;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
|
||||
public City(Long id, Long provinceId, String cityName, String description) {
|
||||
this.id = id;
|
||||
this.provinceId = provinceId;
|
||||
this.cityName = cityName;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getProvinceId() {
|
||||
return provinceId;
|
||||
}
|
||||
|
||||
public void setProvinceId(Long provinceId) {
|
||||
this.provinceId = provinceId;
|
||||
}
|
||||
|
||||
public String getCityName() {
|
||||
return cityName;
|
||||
}
|
||||
|
||||
public void setCityName(String cityName) {
|
||||
this.cityName = cityName;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "City{" +
|
||||
"id=" + id +
|
||||
", provinceId=" + provinceId +
|
||||
", cityName='" + cityName + '\'' +
|
||||
", description='" + description + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.spring.springboot.service;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑接口类
|
||||
*
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
public interface CityService {
|
||||
|
||||
/**
|
||||
* 获取城市
|
||||
*
|
||||
*/
|
||||
City getCityByName(String cityName);
|
||||
|
||||
/**
|
||||
* 新增城市信息
|
||||
*
|
||||
*/
|
||||
void saveCity(City city);
|
||||
|
||||
/**
|
||||
* 更新城市信息
|
||||
*
|
||||
*/
|
||||
void updateCityDescription(String cityName, String description);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.spring.springboot.service.impl;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑实现类
|
||||
* <p>
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
@Service
|
||||
public class CityServiceImpl implements CityService {
|
||||
|
||||
|
||||
// 模拟数据库存储
|
||||
private Map<String, City> cityMap = new HashMap<String, City>();
|
||||
|
||||
public void saveCity(City city){
|
||||
// 模拟数据库插入操作
|
||||
cityMap.put(city.getCityName(), city);
|
||||
}
|
||||
|
||||
@Cacheable(value = "baseCityInfo")
|
||||
public City getCityByName(String cityName){
|
||||
// 模拟数据库查询并返回
|
||||
return cityMap.get(cityName);
|
||||
}
|
||||
|
||||
@CachePut(value = "baseCityInfo")
|
||||
public void updateCityDescription(String cityName, String description){
|
||||
City city = cityMap.get(cityName);
|
||||
city.setDescription(description);
|
||||
// 模拟更新数据库
|
||||
cityMap.put(cityName, city);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
## Redis 配置
|
||||
## Redis数据库索引(默认为0)
|
||||
spring.redis.database=0
|
||||
## Redis服务器地址
|
||||
spring.redis.host=127.0.0.1
|
||||
## Redis服务器连接端口
|
||||
spring.redis.port=6379
|
||||
## Redis服务器连接密码(默认为空)
|
||||
spring.redis.password=
|
||||
## 连接池最大连接数(使用负值表示没有限制)
|
||||
spring.redis.pool.max-active=8
|
||||
## 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
spring.redis.pool.max-wait=-1
|
||||
## 连接池中的最大空闲连接
|
||||
spring.redis.pool.max-idle=8
|
||||
## 连接池中的最小空闲连接
|
||||
spring.redis.pool.min-idle=0
|
||||
## 连接超时时间(毫秒)
|
||||
spring.redis.timeout=0
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.spring.springboot;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.spring.springboot.service.impl.CityServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* Created by bysocket on 05/06/2017.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ApplicationTests {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class);
|
||||
|
||||
|
||||
@Autowired
|
||||
private CityService cityService;
|
||||
|
||||
@Test
|
||||
public void testRedis() {
|
||||
City city = getShanghai();
|
||||
// 向 redis 中存入数据
|
||||
cityService.saveCity(city);
|
||||
|
||||
// 从 redis 中取数据
|
||||
City cityInfo = cityService.getCityByName("上海");
|
||||
|
||||
LOGGER.info(cityInfo.toString());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRedisCache() {
|
||||
City city = getBeijing();
|
||||
// 向 redis 中存入数据
|
||||
cityService.saveCity(city);
|
||||
|
||||
// 从 redis 中取数据, 第一次查询
|
||||
City cityInfo = cityService.getCityByName("北京");
|
||||
LOGGER.info("第一次查询:" + cityInfo.toString());
|
||||
|
||||
// 从 redis 中取数据, 第二次查询
|
||||
cityInfo = cityService.getCityByName("北京");
|
||||
LOGGER.info("第二次查询:" + cityInfo.toString());
|
||||
|
||||
// 更新 city 的描述信息后查询
|
||||
cityService.updateCityDescription("北京", "想不想去北京玩玩呢?");
|
||||
cityInfo = cityService.getCityByName("北京");
|
||||
LOGGER.info("更新描述后查询:" + cityInfo.toString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private City getShanghai(){
|
||||
return new City(1L, 10L, "上海", "人称魔都的地方");
|
||||
}
|
||||
|
||||
private City getBeijing(){
|
||||
return new City(2L, 20L, "北京", "中国帝都");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user