当前位置: 首页 > news >正文

为食堂写个网站建设东莞建站模板公司

为食堂写个网站建设,东莞建站模板公司,北京发生大事了,百度收录哪些网站目录 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)序列化到内存数据库中

项目总结

  1.  引入依赖:首先,在 pom.xml 文件中引入 Spring Boot Starter Data Redis 依赖,以便使用 Spring Boot 提供的 Redis 支持。
  2. 配置 Redis 连接信息:在 Spring Boot 项目的配置文件(如 application.properties 或 application.yml)中配置 Redis 的连接信息,包括主机地址、端口、密码等。
  3. 编写 Redis 配置类:如果需要自定义 RedisTemplate 的配置,可以创建一个 Redis 配置类,在其中配置 RedisTemplate。你可以自定义键值的序列化器、连接工厂等配置。
  4. 使用 RedisTemplate 进行操作:通过在需要使用 Redis 的地方注入 RedisTemplate,即可使用它来进行 Redis 的操作,包括存储、读取、删除等操作。
    1. 比如本项目中就是在ComServiceImpl业务接口的实现类中编写业务逻辑代码时,使用到了Redis进行数据的存取 
  5. 测试:编写单元测试或集成测试来验证 Redis 的功能是否正常。
  6. 这就是整合 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 Web
  • Lombok模型简化组件:Lombok
  • MyBatis框架:MyBatis Framework
  • MySQL的驱动:MySQL Driver
  • Redis框架:Spring Data Redis(Access+Driver)

项目结构:

pom.xml

  • 常见报错的解决办法:加个版本号<version>,或者降一下版本号
  • 推荐使用spring-boot-starter-data-redis,我查了很多办法也解决不了这个依赖的报错,所以在此用了spring-data-redis,需要手动配置Redis 相关的 bean 和属性。这可能导致配置错误或遗漏,从而导致应用无法正确连接到 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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.study</groupId><artifactId>springboot_redis</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot_redis</name><description>Demo project for Spring Boot</description><properties><java.version>8</java.version></properties><dependencies><!--推荐使用spring-boot-starter-data-redis,免去手动配置--><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>2.4.1</version></dependency><!--SpringBoot默认使用Lettuce客户端,相比于Jedis,线程安全且可支持并发访问--><dependency><groupId>io.lettuce</groupId><artifactId>lettuce-core</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>2.2.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></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><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter-test</artifactId><version>2.2.2</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

application.properties配置文件

# 连接MySQL数据库
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/netshop?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# 连接Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
# Redis数据库索引(默认为0)
spring.redis.database=0

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存取字符串,注入该模板@AutowiredRedisTemplate<String,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 RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {RedisTemplate<String, 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?pid=1
  • 取出pid=1的商品名称,页面返回一个字符串

  • 访问网址:http://localhost:8080/com/getcom?pid=1 
  • 取出pid=1的商品对象

http://www.yayakq.cn/news/72569/

相关文章:

  • 网站二级域名是什么新郑网站开发
  • 网站建设对应的岗位网站建设 核算
  • 购物网站建设 属于信息系统管理与设计么?产品营销策略怎么写
  • 网站策划书格式及范文1000字免费长沙网站建设建
  • 网站建设自己可以建网站吗家具网站建设目的及功能定位
  • 万网注册域名做简单网站wordpress php 中文分词 开源
  • 如何查询网站备案北京家装公司十大排名
  • 做团购的网站无极在线网站播放
  • 电商网站设计系统短网址在线生成工具
  • 做网站有哪些主题个人模板网站
  • 电商网站用php做的吗优秀简历制作网站
  • 网站如何推广引流公众号怎么转wordpress
  • 0基础12天精通网站建设网站权重最高是多少
  • 天津做陶瓷的公司网站杭州市做网站
  • 外贸公司英文网站建设网站开发所遵循的
  • 中国建设网站企业网上银行业务功能网络推广途径
  • 承德微网站开发seo 公司
  • 武进建设银行网站首页大型网站seo课程
  • wordpress导入网站文章天津网站建设 seo
  • 农林科技公司网站模板深圳刚刚突然宣布
  • 杭州品牌网站开发外贸网站排名
  • 云主机怎么安装网站重庆做网站_重庆网站建设_重庆网络推广_重庆网络公司
  • 网站开发实验结论wampserver和wordpress
  • 网站项目流程seo引擎优化是什
  • 现在哪个网站做电商好wordpress cdn推荐
  • 临沂免费模板建站南京尚网网络科技有限公司
  • 方法网站目录如何替换网站的图片
  • 湛江做寄生虫网站上海传媒公司电话
  • 怎样用h5做网站wordpress 搜索页描述
  • 网站开发进度表wordpress新建页面教程