From bdaff26758003712e4e8435034684fd86a31e744 Mon Sep 17 00:00:00 2001 From: liqiangqiang Date: Mon, 5 Jun 2017 14:58:09 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=20=E6=B3=A8=E8=A7=A3=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=95=B4=E5=90=88=20Redis=20=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-mybatis-redis-annotation/pom.xml | 58 +++++++++++++ .../org/spring/springboot/Application.java | 20 +++++ .../org/spring/springboot/domain/City.java | 82 +++++++++++++++++++ .../springboot/service/CityService.java | 32 ++++++++ .../service/impl/CityServiceImpl.java | 45 ++++++++++ .../src/main/resources/application.properties | 19 +++++ .../spring/springboot/ApplicationTests.java | 70 ++++++++++++++++ 7 files changed, 326 insertions(+) create mode 100755 springboot-mybatis-redis-annotation/pom.xml create mode 100644 springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/Application.java create mode 100644 springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/domain/City.java create mode 100644 springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/CityService.java create mode 100644 springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java create mode 100644 springboot-mybatis-redis-annotation/src/main/resources/application.properties create mode 100644 springboot-mybatis-redis-annotation/src/test/org/spring/springboot/ApplicationTests.java diff --git a/springboot-mybatis-redis-annotation/pom.xml b/springboot-mybatis-redis-annotation/pom.xml new file mode 100755 index 0000000..3199297 --- /dev/null +++ b/springboot-mybatis-redis-annotation/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + springboot + springboot-mybatis-redis-annotation + 0.0.1-SNAPSHOT + springboot-mybatis-redis-annotation :: 注解实现整合 Redis 作为缓存 + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.3.RELEASE + + + + 1.2.0 + 5.1.39 + 1.2.32 + + + + + + + org.springframework.boot + spring-boot-starter-cache + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + junit + junit + 4.12 + + + diff --git a/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/Application.java b/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..5070937 --- /dev/null +++ b/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/Application.java @@ -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); + } +} diff --git a/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/domain/City.java b/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/domain/City.java new file mode 100644 index 0000000..2d70850 --- /dev/null +++ b/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/domain/City.java @@ -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 + '\'' + + '}'; + } +} diff --git a/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/CityService.java b/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/CityService.java new file mode 100644 index 0000000..da3d1ef --- /dev/null +++ b/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/CityService.java @@ -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); + +} diff --git a/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java b/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java new file mode 100644 index 0000000..c0f6d86 --- /dev/null +++ b/springboot-mybatis-redis-annotation/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java @@ -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; + +/** + * 城市业务逻辑实现类 + *

+ * Created by bysocket on 07/02/2017. + */ +@Service +public class CityServiceImpl implements CityService { + + + // 模拟数据库存储 + private Map cityMap = new HashMap(); + + 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); + } + +} diff --git a/springboot-mybatis-redis-annotation/src/main/resources/application.properties b/springboot-mybatis-redis-annotation/src/main/resources/application.properties new file mode 100644 index 0000000..c611a85 --- /dev/null +++ b/springboot-mybatis-redis-annotation/src/main/resources/application.properties @@ -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 \ No newline at end of file diff --git a/springboot-mybatis-redis-annotation/src/test/org/spring/springboot/ApplicationTests.java b/springboot-mybatis-redis-annotation/src/test/org/spring/springboot/ApplicationTests.java new file mode 100644 index 0000000..21494d2 --- /dev/null +++ b/springboot-mybatis-redis-annotation/src/test/org/spring/springboot/ApplicationTests.java @@ -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, "北京", "中国帝都"); + } +}