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.mzph.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…