哪个网站推广产品好,网站空间租用,上海猎头公司前十名,做网站的人多吗目录
Redis
创建Commodity表
启动MySQL和Redis
新建一个SpringBoot项目
pom.xml
application.properties
Commodity实体类 ComMapper接口
ComService业务层接口
ComServiceImpl业务接口的实现类
ComController控制器
RedisConfig配置类
SpringbootRdisApplication启…目录
Redis
创建Commodity表
启动MySQL和Redis
新建一个SpringBoot项目
pom.xml
application.properties
Commodity实体类 ComMapper接口
ComService业务层接口
ComServiceImpl业务接口的实现类
ComController控制器
RedisConfig配置类
SpringbootRdisApplication启动类
启动项目进行测试 Redis Redis是一个高性能的 key-value 数据库支持多种数据结构常用作缓存、消息代理和配置中心用途 1、缓存热点数据 由于Redis的访问速度快、支持的数据类型很丰富所以很适合用来存储热点数据其内置的expire可以对缓存的数据设置过期时间在缓存的数据过期后再设置新的缓存数据2、计数器 Redis的incrby命令是原子性地递增因此可以运用于商城系统的高并发的秒杀活动、分布式序列号的生成等场景3、排行榜 可以使用Redis的SortedSet进行热点数据的排序4、分布式锁 Redis的setnx命令的作用是如果当前的缓存数据不存在则设置缓存成功并返回1否则设置缓存失败并返回0。可以利用这个特性在Redis集群中检测锁的有效时间如果超时那么等待的进程将有机会获得锁从而防止项目出现死锁在SpringBoot中要存储和访问Redis中的数据可以使用 RedisTemplate 和 StringRedisTemplate 模板类用模板操作Redis实际就是通过set()/get()方法存取数据 StringRedisTemplate 是 RedisTemplate 的子类StringRedisTemplate 只针对键值都是字符串类型的数据而 RedisTemplate 可以操作对象类型的数据StringRedisTemplate 默认使用 StringRedisSerializer 序列化器而 RedisTemplate 默认使用 JdkSerializationRedisSerializer 序列化器RedisTemplate 提供了5种数据结构的操作方法 opsForValue操作字符串类型opsForHash操作哈希类型opsForList操作列表类型opsForSet操作集合类型opsForZSet操作有序集合类型当数据存储到 Redis中时键和值都是通过SpringBoot提供的序列化器Serializer序列化到内存数据库中 项目总结 引入依赖首先在 pom.xml 文件中引入 Spring Boot Starter Data Redis 依赖以便使用 Spring Boot 提供的 Redis 支持。配置 Redis 连接信息在 Spring Boot 项目的配置文件如 application.properties 或 application.yml中配置 Redis 的连接信息包括主机地址、端口、密码等。编写 Redis 配置类如果需要自定义 RedisTemplate 的配置可以创建一个 Redis 配置类在其中配置 RedisTemplate。你可以自定义键值的序列化器、连接工厂等配置。使用 RedisTemplate 进行操作通过在需要使用 Redis 的地方注入 RedisTemplate即可使用它来进行 Redis 的操作包括存储、读取、删除等操作。 比如本项目中就是在ComServiceImpl业务接口的实现类中编写业务逻辑代码时使用到了Redis进行数据的存取 测试编写单元测试或集成测试来验证 Redis 的功能是否正常。这就是整合 Spring Boot 与 Redis 的基本工作流程和思路。通过这些步骤你可以在 Spring Boot 项目中方便地使用 Redis 来实现缓存、分布式锁等功能。 创建Commodity表
CREATE DATABASE netshop;
USE netshop;
CREATE TABLE commodity(Pid INT(8) NOT NULL PRIMARY KEY,TCode CHAR(3) NOT NULL,SCode CHAR(8) NOT NULL,PName VARCHAR(32) NOT NULL,PPrice DECIMAL(7,2) NOT NULL,Stocks INT UNSIGNED DEFAULT 0
);
INSERT INTO commodity(Pid,TCode,SCode,PName,PPrice,Stocks) VALUES(1,11A,SXLC001A,洛川红富士苹果冰糖心10斤箱装,44.80,3601);
INSERT INTO commodity(Pid,TCode,SCode,PName,PPrice,Stocks) VALUES(2,11A,SXLC002A,烟台红富士苹果10斤箱装,29.80,5698);
INSERT INTO commodity(Pid,TCode,SCode,PName,PPrice,Stocks) VALUES(3,11A,SXLC003A,库尔勒香梨10斤箱装,69.80,8902); 启动MySQL和Redis
命令行启动MySQL不要关闭小黑窗 双击redis-server.exe启动redis服务器 新建一个SpringBoot项目 添加Spring Boot基本框架Spring WebLombok模型简化组件LombokMyBatis框架MyBatis FrameworkMySQL的驱动MySQL DriverRedis框架Spring Data Redis(AccessDriver) 项目结构 pom.xml 常见报错的解决办法加个版本号version或者降一下版本号推荐使用spring-boot-starter-data-redis我查了很多办法也解决不了这个依赖的报错所以在此用了spring-data-redis需要手动配置Redis 相关的 bean 和属性。这可能导致配置错误或遗漏从而导致应用无法正确连接到 Redis。 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.12.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.study/groupIdartifactIdspringboot_redis/artifactIdversion0.0.1-SNAPSHOT/versionnamespringboot_redis/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version8/java.version/propertiesdependencies!--推荐使用spring-boot-starter-data-redis,免去手动配置--dependencygroupIdorg.springframework.data/groupIdartifactIdspring-data-redis/artifactIdversion2.4.1/version/dependency!--SpringBoot默认使用Lettuce客户端,相比于Jedis,线程安全且可支持并发访问--dependencygroupIdio.lettuce/groupIdartifactIdlettuce-core/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.2/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter-test/artifactIdversion2.2.2/versionscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project
application.properties配置文件
# 连接MySQL数据库
spring.datasource.urljdbc:mysql://127.0.0.1:3306/netshop?useUnicodetruecharacterEncodingutf-8serverTimezoneUTCuseSSLtrue
spring.datasource.usernameroot
spring.datasource.passwordadmin
spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver# 连接Redis
spring.redis.host127.0.0.1
spring.redis.port6379
spring.redis.password
# Redis数据库索引(默认为0)
spring.redis.database0
Commodity实体类
package com.study.springboot_redis.model;import lombok.Data;
import java.io.Serializable;Data //该注解包含了getter,setter,toString()等方法
public class Commodity implements Serializable {//序列化private int pid;//商品号private String tcode;//商品分类编码private String scode;//商家编码private String pname;//商品名称private float pprice;//商品价格private int stocks;//商品库存
}ComMapper接口
package com.study.springboot_redis.mapper;import com.study.springboot_redis.model.Commodity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;/*** 这是一个公共接口定义了与commodity表进行交互的方法*/
Mapper //这个注解表示该接口是一个Mapper接口用于与数据库进行交互
public interface ComMapper {//Param(pid)注解用于将方法参数与SQL语句中的pid参数进行映射Select(SELECT * FROM commodity WHERE Pid #{pid})Commodity queryByPid(Param(pid) int pid);
}ComService业务层接口
package com.study.springboot_redis.service;import com.study.springboot_redis.model.Commodity;/*** 业务层*/
public interface ComService {public String getPNameFromRedis(int pid);//从Redis获取商品名称public Commodity getComFromRedis(int pid);//从Redis获取商品记录对象
}ComServiceImpl业务接口的实现类
package com.study.springboot_redis.service;import com.study.springboot_redis.mapper.ComMapper;
import com.study.springboot_redis.model.Commodity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.StringRedisTemplate;/*** 业务接口的实现类* 用模板操作Redis实际就是通过set()/get()方法存取数据*/
Service //将一个类标识为服务层组件
public class ComServiceImpl implements ComService{AutowiredComMapper comMapper;AutowiredStringRedisTemplate stringRedisTemplate;//StringRedisTemplate模板用于从Redis存取字符串,注入该模板AutowiredRedisTemplateString,Object redisTemplate;//RedisTemplate模板用于从Redis存取对象,注入该模板Overridepublic String getPNameFromRedis(int pid) {Commodity commodity comMapper.queryByPid(pid);//从MyBatis接口读取商品记录stringRedisTemplate.opsForValue().set(pname,commodity.getPname());//使用set()方法存入Redisreturn stringRedisTemplate.opsForValue().get(pname);//使用get()方法从Redis中获取}Overridepublic Commodity getComFromRedis(int pid) {Commodity commodity comMapper.queryByPid(pid);redisTemplate.opsForValue().set(String.valueOf(commodity.getPid()),commodity);return (Commodity) redisTemplate.opsForValue().get(String.valueOf(pid));}
}ComController控制器
package com.study.springboot_redis.controller;import com.study.springboot_redis.model.Commodity;
import com.study.springboot_redis.service.ComServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(com)
public class ComController {Autowired //注入业务层操作Redis的服务实体ComServiceImpl comService;RequestMapping(getpname)//从Redis获取商品名称public String getPNameByPid(int pid){return comService.getPNameFromRedis(pid);}RequestMapping(getcom)//从Redis获取商品记录对象public Commodity getComByPid(int pid){return comService.getComFromRedis(pid);}
}RedisConfig配置类
package com.study.springboot_redis.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;Configuration //声明该类为配置类
public class RedisConfig {/*** RedisTemplate使用JdkSeriallizationRedisSerializer来序列化数据,以二进制的形式存储,不便可视化,所以在此自定义序列化器* 在此自定义一个JSON格式的序列化类*/Beanpublic RedisTemplateString, Object redisTemplate(LettuceConnectionFactory connectionFactory) {RedisTemplateString, Object redisTemplate new RedisTemplate();redisTemplate.setKeySerializer(new StringRedisSerializer());//GenericJackson2JsonRedisSerializer这个序列化器是一个通用的 JSON 序列化器// 它可以序列化和反序列化任意类型的对象而不需要指定对象的类型redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.setConnectionFactory(connectionFactory);return redisTemplate;}
}
SpringbootRdisApplication启动类
package com.study.springboot_redis;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class SpringbootRedisApplication {public static void main(String[] args) {SpringApplication.run(SpringbootRedisApplication.class, args);}
}启动项目进行测试 访问网址http://localhost:8080/com/getpname?pid1取出pid1的商品名称页面返回一个字符串 访问网址http://localhost:8080/com/getcom?pid1 取出pid1的商品对象