Redis數據緩存

代碼的整體結構

配置文件

<?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>com.cc</groupId><artifactId>springboot-redis-cache</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot-redis-cache</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.8.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

?創建實體類

  • 這里使用的@Getter和@Setter注解,可以避免寫一大堆的get和set方法
  • 需要引入Lombak,在file->settings->plugins里面查找

package com.cc.springbootrediscache.entity;import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {private Integer id;private String username;private String password;private Integer status;public User(String username, String password, Integer status) {this.username = username;this.password = password;this.status = status;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}
}

UserMapper

package com.cc.springbootrediscache.mapper;import com.cc.springbootrediscache.entity.User;
import org.apache.ibatis.annotations.*;import java.util.List;public interface UserMapper {@Options(useGeneratedKeys = true, keyProperty = "id")@Insert("insert into user (username,password,status) values (#{username}, #{password}, #{status})")Integer addUser(User user);@Delete("delete from user where id=#{0}")Integer deleteUserById(Integer id);@Update("update user set username=#{username}, password=#{password}, status=#{status}")Integer updateUser(User user);@Select("select * from user where id=#{0}")User getById(Integer id);@Select("select * from user")List<User> queryUserList();}

UserService?

package com.cc.springbootrediscache.service;import com.cc.springbootrediscache.entity.User;
import com.cc.springbootrediscache.mapper.UserMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;import javax.annotation.Resource;@Service
public class UserService {@Resourceprivate UserMapper userMapper;/*** 添加和更新都調用這個方法* @param user* @return user_1*/@CachePut(value = "usercache", key = "'user_' + #user.id.toString()", unless = "#result eq null")public User save(User user) {if (null != user.getId()) {userMapper.updateUser(user);} else {userMapper.addUser(user);}return user;}@Cacheable(value = "usercache", key = "'user_' + #id", unless = "#result eq null")public User findUser(Integer id) {return userMapper.getById(id);}@CacheEvict(value = "usercache", key = "'user_' + #id", condition = "#result eq true")public boolean delUser(Integer id) {return userMapper.deleteUserById(id) > 0;}
}

?UserController

package com.cc.springbootrediscache.controller;import com.cc.springbootrediscache.entity.User;
import com.cc.springbootrediscache.service.UserService;
import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;@RestController
@RequestMapping("/user")
public class UserController {@Resourceprivate UserService userService;@PutMappingpublic User add(@RequestBody User user) {return userService.save(user);}@DeleteMapping("{id}")public boolean delete(@PathVariable Integer id) {return userService.delUser(id);}@GetMapping("{id}")public User getUser(@PathVariable Integer id) {return userService.findUser(id);}@PostMappingpublic User update(@RequestBody User user) {return userService.save(user);}
}

使用MapperScan添加對于mapper的掃描

package com.cc.springbootrediscache;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.cc.springbootrediscache.mapper")
public class SpringbootRedisCacheApplication {public static void main(String[] args) {SpringApplication.run(SpringbootRedisCacheApplication.class, args);}
}

?配置文件

server.port= 8099spring.datasource.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc://mysql.rds.aliyuncs.com:3306/login?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF8
spring.datasource.url=jdbc:mysql://192.168.133.130:3306/usr?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF8
spring.datasource.username=
spring.datasource.password=########################################################
###REDIS (RedisProperties) redis基本配置;
########################################################
# database name
spring.redis.database=0
# server host1 單機使用,對應服務器ip
spring.redis.host=192.168.133.130
# server password 密碼,如果沒有設置可不配
#spring.redis.password=
#connection port  單機使用,對應端口號
spring.redis.port=10190
# pool settings ...池配置
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1logging.level.com.cc.springbootrediscache.mapper=debug

?RedisCacheConfig

package com.cc.springbootrediscache.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {@Beanpublic CacheManager cacheManager(RedisTemplate redisTemplate) {RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);redisCacheManager.setDefaultExpiration(360);return redisCacheManager;}@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);template.setValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/447263.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/447263.shtml
英文地址,請注明出處:http://en.pswp.cn/news/447263.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

hevc/265 開源項目及相關

1.X265 個是有兩個版本&#xff0c;一個是國內人搞的&#xff0c;是國外公司搞的 1.國外公司版本 只是一個編碼器&#xff0c;目前沒有支持解碼 開發語言 c web url: www.x265.org source url: https://bitbucket.org/multicoreware/x265 x265 is an open-source projec…

IPFS星際文件系統的簡介

IPFS簡介 IPFS&#xff08;InterPlanetary File System&#xff09;叫星際文件傳輸系統&#xff0c;本質是一個基于點對點的分布式超媒體分發協議&#xff0c;它整合了分布式系統&#xff0c;為所有人提供全球統一的可尋址空間&#xff0c;因為他具有良好的安全性、較高的傳輸…

ARM和NEON指令 very nice

在移動平臺上進行一些復雜算法的開發&#xff0c;一般需要用到指令集來進行加速。目前在移動上使用最多的是ARM芯片。 ARM是微處理器行業的一家知名企業&#xff0c;其芯片結構有&#xff1a;armv5、armv6、armv7和armv8系列。芯片類型有&#xff1a;arm7、arm9、arm11、corte…

IPFS下載安裝和配置

參考鏈接 因為這個網站訪問速度很慢&#xff0c;我提供了IPFS的MAC版本。有需要的查看我的資源下載。 大致流程 安裝 $ ls go-ipfs_v0.4.10_darwin-amd64.tar.gz $ tar xvfz go-ipfs_v0.4.10_darwin-amd64.tar.gz x go-ipfs/build-log x go-ipfs/install.sh x go-ipfs/ipfs…

IPFS的相關操作命令

新增文件 在桌面新建名字為1121的文件夾&#xff0c;在文件夾里面新建file.txt文件&#xff0c;在文件里面輸入數據&#xff0c;保存退出 $ pwd /Users/CHY/Desktop $ mkdir 1121 $ cd 1121/ $ vi file.txt $ cat file.txt 哈哈&#xff0c;為什么只有我不快樂 給文件輸入內容…

Neon Intrinsics各函數介紹

#ifndef __ARM_NEON__ #error You must enable NEON instructions (e.g. -mfloat-abisoftfp -mfpuneon) to use arm_neon.h #endif/*(1)、正常指令&#xff1a;生成大小相同且類型通常與操作數向量相同的結果向量&#xff1b; (2)、長指令&#xff1a;對雙字向量操作數執行運算…

npm安裝包總是失敗了的,請參考

鏡像使用方法 &#xff08;三種辦法任意一種都能解決問題&#xff0c;建議使用第三種&#xff0c;將配置寫死&#xff0c;下次用的時候配置還在&#xff09;: 1.通過config命令 npm config set registry https://registry.npm.taobao.org npm info underscore //&#xff08…

arm 開發工具比較(ADS vs RealviewMDK vs RVDS)

ADS REALVIEW MDK RVDS 公司 ARM Keil&#xff08;后被ARM收購&#xff09; ARM 版本 最新1.2 ,被RVDS取代 最新4.0 是否免費 破解情況 有 有 工程管理 CodeWarrior IDE nVision IDE Eclipse/ CodeWarrior IDE 編譯器 ARM C compiler for AD…

解決macOS Catalina(10.15)解決阻止程序運行“macOS無法驗證此App不包含惡意軟件”

在終端里面輸入如下命令 sudo spctl --master-disable 下面圖片對比執行命令前后&#xff0c;安全性與隱私 界面上顯示的差異&#xff1a;使用命令之后&#xff0c;界面變了

MSYS2 + MinGW-w64 + Git + gVim 環境配置

原文 http://dantvt.is-programmer.com/posts/63161.html 以前用 MSYS 的多&#xff0c;最近重裝系統順帶把環境重新配一下&#xff0c;發現 MSYS2 挺順手的。 一、安裝 MSYS2 先裝 MSYS2 的好處是之后可以將 $HOME 設為 /home/name/&#xff0c;再裝其他 *nix 系工具時配置…

MAC版 的最新Docker 2.2版本配置國內代理的解決辦法

點擊Docker圖標&#xff0c;選擇Preference選項&#xff0c;進行國內代理的問題 輸入內容如下 {"experimental": false,"debug": true,"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn", "https://hub-mirror.c.163.…

常用的Homebrew的命令的使用

&#xff08;1&#xff09;安裝軟件&#xff1a;brew install 軟件名&#xff0c;例如&#xff1a;brew install wget &#xff08;2&#xff09;搜索軟件&#xff1a;brew search 軟件名 &#xff08;3&#xff09;卸載軟件&#xff1a;brew uninstall 軟件名 &#xff08;…

微軟正式提供Visual Studio 2013正式版下載(附直接鏈接匯總)

轉自 http://www.iruanmi.com/visual-studio-2013/ 微軟已經向MSDN訂閱用戶提供了Visual Studio 2013正式版鏡像下載&#xff0c;不過非MSDN用戶可以在微軟的Visual Studio 2013官方網站上下載到正式版鏡像&#xff08;通過下載專業版本&#xff0c;已驗證與MSDN版本一致&…

《算法的樂趣》作者王曉華訪談:多看、多做、多想是秘訣

摘要&#xff1a;王曉華是一位熱衷于算法研究的程序員&#xff0c;他是CSDN算法專欄的超人氣博主&#xff0c;也是《算法的樂趣》一書的作者。近日&#xff0c;筆者采訪了王曉華&#xff0c;請他分享算法的經驗之道。 王曉華是一位熱衷于算法研究的程序員&#xff0c;他是CSDN…

基于Mac環境搭建以太坊私有區塊鏈進行挖礦模擬

第一步&#xff1a;相關軟件的安裝 go-ethereum客戶端安裝Go-ethereum客戶端通常被稱為Geth&#xff0c;它是個命令行界面&#xff0c;執行在Go上實現的完整以太坊節點。Geth得益于Go語言的多平臺特性&#xff0c;支持在多個平臺上使用(比如Windows、Linux、Mac)。Geth是以太坊…

Springboot 添加server.servlet.context-path

Springboot 2.0變革后的配置區別 1、springboot 2.0之前&#xff0c;配置為 server.context-path 2、springboot 2.0之后&#xff0c;配置為 server.servlet.context-path

vs2015 支持Android arm neon Introducing Visual Studio’s Emulator for Android

visual studio 2015支持Android開發了。 Microsoft released Visual Studio 2015 Preview this week and with it you now have options for Android development. When choosing one of those Android development options, Visual Studio will also install the brand new Vi…

基于linux環境采用update-alternatives 方式進行python版本切換

采用update-alternatives 切換版本 update-alternatives是Debian提供的一個工具&#xff0c;通過鏈接的方式&#xff0c;但是其切換的過程非常方便。首先看一下update-alternatives的幫助信息&#xff1a; $ update-alternatives --help 用法&#xff1a;update-alternatives …

FFmpeg示例程序合集-批量編譯腳本

此前做了一系列有關FFmpeg的示例程序&#xff0c;組成了《 最簡單的FFmpeg示例程序合集》&#xff0c;其中包含了如下項目&#xff1a;simplest ffmpeg player: 最簡單的基于FFmpeg的視頻播放器simplest ffmpeg audio player: 最簡單的基于FFmpeg的音頻…

基于Ubuntu環境使用docker搭建對于中文識別的chineseocr_lite項目

光學字符識別&#xff08;OCR&#xff09; 光學字符識別&#xff08;OCR&#xff09;目前已經有了很廣泛的應用&#xff0c;很多開源項目都會嵌入OCR 來擴展原有的能力&#xff0c;例如身份證識別、出入停車場的車牌識別、拍照翻譯等等本文介紹的開源的中文 OCR 項目&#xff…