前期准备
springboot和redis的概念东西就不介绍了,直接上环境
springboot环境:本人使用jdk1.8+Maven3.0
redis环境:本人使用的虚拟机centos6.5,具体安装请参考本人的
http://blog.csdn.net/qq_28089993/article/details/70331821
直接上部分代码
springboot极大地简化了开发,下面是pom.xml文件:
<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>com.example</groupId>
<artifactId>springboot1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot1</name>
<url>http://maven.apache.org</url>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
</properties>
<dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Redis 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
本次整合是包括了Mybatis,redis。所以大家可以通过依赖看出。然后是application.properties,如下
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=192.168.117.88
## 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
其中Redis服务器地址是我的虚拟机的ip地址,密码默认为空。
最后部分代码主要是redis的使用思路,下面只列出了查询方法的,删除修改简单说明一下其基本思路一样,先执行删除修改方法,然后判断是否存在缓存,存在则删除缓存,不存在则不处理。
/**
* 如果缓存存在,从缓存中获取城市信息
* 如果缓存不存在,从数据库中获取城市信息,并且插入缓存
*/
@Override
public City findCityById(Long id) {
//从缓存中获取城市信息
String key = "city_" + id;
ValueOperations<String, City> operations = redisTemplate.opsForValue();
//缓存存在
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey) {
City city = operations.get(key);
LOGGER.info("CityServiceImpl.findCityById() : 从缓存中获取了城市 >> " + city.toString());
return city;
}
//缓存不存在,将数据存入缓存
City city = cityDao.findById(id);
//插入缓存操作
operations.set(key, city);
//插入的数据设置10秒刷新
//operations.set(key, city, 10, TimeUnit.SECONDS);
LOGGER.info("CityServiceImpl.findCityById() : 城市插入缓存 >> " + city.toString());
return city;
}
其中的`operations.set(key, city, 10, TimeUnit.SECONDS);`设置10s刷新redis缓存大家可以试一下。
注意如果这里的**实体类City没有序列化,将无法保存在缓存中**,且会报以下异常: Serializable java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type,请注意。
这里主要是对自己学习的一个记录,同时让后来人少走些弯路,可是如果你自己不走一边,你是不会记住哪里有坑的,所以动手去做。这里的代码借鉴了「泥瓦匠BYSocket 」大哥的,大家可以去看看www.bysocket.com
本人菜鸟一枚,望大神指教。完整代码
https://github.com/kongliang94/springboot-redis/