緩存:即將常用的並且不會頻繁變更的數據存入內存當中,從而減少服務器數據庫壓力,提升訪問速率。
使用redis作爲緩存技術方案,主要是redis緩存技術的特點就在於高效,因爲目前涉及的數據量逐漸增多,在對於數據的存儲上面和sql以及服務器資源優化上面就來的特別的重要。而redis可以幫助解決由於數據庫壓力造成的延遲現象,針對於很少做改變的數據並且經常使用的數據,我們可以一致性加入內存。這樣可以一方面減少數據庫壓力,一方面提高讀寫效率。下面筆者通過一個demo演示springboot整合mybatis以及redis的詳細教程,對於springboot集成mybatis不熟悉的請移步這裏查看教程詳解《最全面的springBoot集成mybatis+mysql項目搭建經驗分享》
1、在pom.xml文件添加Redis依賴
<!-- Spring Boot Reids 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
2、在application.yml文件中配置redis
#默認是8080,此處使用配置8060
server:
port: 8060
spring:
#數據源配置
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
#springboot集成redis配置
redis:
# Redis服務器地址
host: 127.0.0.1
# Redis數據庫索引(默認爲0
database: 0
port: 6379
# Redis服務器連接密碼(默認爲空)
password: 123456
jedis:
pool:
# 連接池最大連接數(使用負值表示沒有限制)
max-active: 8
max-idle: 8
min-idle: 0
mybatis:
#掃描實體類的位置,在此處指明掃描實體類的包,在mapper中就可以不用寫實體類的全路徑名了
typeAliasesPackage: com.guo.springboot01.entity
mapperLocations: classpath:mapper/*Dao.xml
3、controller,service,dao以及mapping映射文件代碼
@RestController
@EnableAutoConfiguration
public class UserController {
@Autowired
private IUserService userService;
@AnalysisActuator(note = "測試自定義註解")
@RequestMapping("findById")
public User getUser(Integer id) {
return userService.getUserById(id);
}
@RequestMapping("findAll")
public List<User> getAllUser() {
return userService.findAll();
}
}
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserDao userDao;
@Autowired
private RedisTemplate redisTemplate;
/**
* 獲取用戶策略:先從緩存中獲取用戶,沒有則取數據表中查
* 數據,再將數據寫入緩存
*/
@Override
public User getUserById(Integer id) {
String key = id+"";
ValueOperations <String,User> operations = redisTemplate.opsForValue();
boolean hasKey = redisTemplate.hasKey(key);
//如果緩存存在
if(hasKey){
User user = operations.get(key);
System.out.println("==========從緩存中獲得數據=========");
System.out.println(user.getUserName());
System.out.println("==============================");
return user;
}else{
User user = userDao.findById(id);
System.out.println("==========從數據表中獲得數據=========");
System.out.println(user.getUserName());
System.out.println("==============================");
//插入緩存
operations.set(key, user,5, TimeUnit.MINUTES);
return user;
}
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
}
public interface UserDao {
public User findById(Integer id);
public List<User> findAll();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.guo.springboot01.dao.UserDao" >
<resultMap id="BaseResultMap" type="com.guo.springboot01.entity.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<select id="findById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select * from user where id = #{id}
</select>
<select id="findAll" resultMap="BaseResultMap">
select * from user
</select>
</mapper>
4、瀏覽器測試
測試前先在本地mysql數據庫裏新建user表,插入2條測試數據,並且啓動本地Redis服務
第一次請求,控制檯輸出:
第二次請求,控制檯輸出
歡迎開發者朋友一起學習,成長,交流,筆者電話(微信):18629374628