redis缓存在我们的开发中是经常使用到的非关系型数据库,可以用于缓存数据库数据,也可以用于存放session Redis安装和运行可以点击教程链接详细看一下:教程(我是安装在linux,windows端安装RedisDesktopManager,通过远程lia) Redis是一个key-value的nosql数据库,先存到内存中,会根据一定的策略持久化到磁盘,即使断电也不会丢失数据,支持的数据类型比较多 看图: 导入pom文件 1.2.1
创建数据库 创建包名 完善实体类代码(User) 完善dao层代码(UserJpa) 完善service层代码(UserService) 完善service层实现类代码(UserserviceImpl) 完善视图层代码(UserController) 完善配置文件代码(application.properties) 启动类添加注解@EnableCaching 启动测试,可以使用Debug进行测试 Redis:
Redis项目中的运行过程:


Redis使用(注解的方式)
Redis代码进行测试,使用SpringBoo项目
创建项目(SpringBoot项目)
1.1.1
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies>
create database ssmIDEA; use ssmIDEA; create table user( id int primary key auto_increment, name varchar(20), age int ); select*from user; -- 测试数据 insert into user(name,age)values("huang",20); insert into user(name,age)values("lin",20);
1.2.2

1.3.1
package com.example.demo.domain; import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; import java.io.Serializable; @Data @Entity public class User implements Serializable { private static final long serialVersionUID = -3055518357571903443L; @Id private Integer id; private String name; private Integer age; } 1.3.2
package com.example.demo.dao; import com.example.demo.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository("user") public interface UserJpa extends JpaRepository<User,Integer> { } 1.3.3
package com.example.demo.service; import com.example.demo.domain.User; import java.util.List; public interface UserService { public List<User> list(); } 1.3.4
package com.example.demo.service.impl; import com.example.demo.dao.UserJpa; import com.example.demo.domain.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired UserJpa userJpa; @Override public List<User> list() { List<User> all = userJpa.findAll(); return all; } } 1.3.5
package com.example.demo.controller; import com.example.demo.dao.UserJpa; import com.example.demo.domain.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller public class UserController { @Autowired UserService userService; @Autowired UserJpa userJpa; @RequestMapping("/list") @ResponseBody //condition = "#id.length()>3":id的长度大于3进行缓存 //unless = "#result.size()!=0"返回结果不等于null时缓存,unless代表反 @Cacheable(cacheNames = "list",key = "#id",condition = "#id.length()>3",unless = "#result.size()==null") @CachePut @CacheEvict public List<User> list(@RequestParam("id")String id){ List<User> list = userService.list(); return list; } } 1.3.6
Spring.dataSource.driver-class-name=com.mysql.cj.jdbc.Driver Spring.dataSource.url=jdbc:mysql://localhost:3306/ssmidea?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false Spring.dataSource.username=root Spring.dataSource.password=010126 Spring.jpa:shop-sql:true Spring.redis.host:192.168.137.237 Spring.redis.port:63791.3.7
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 1.4.1

说明缓存成功
细节问题看注释
原创文章,未经允许禁止盗用
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算
官方软件产品操作指南 (170)