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

建设网站的法律可行性挖掘爱站网

建设网站的法律可行性,挖掘爱站网,沈阳做网站制作的公司,汽车网站设计模板本文基于Springboot,mybatis plus,mysql,redis, Jedis模拟redis缓存实现 目录 1. 添加所需maven依赖 2. 设置数据库及数据表 3. 构建实体类 4. 构建工具类实现 redis 数据库连接池,redis 的读取,写入功…

本文基于Springboot,mybatis plus,mysql,redis, Jedis模拟redis缓存实现

目录

1. 添加所需maven依赖

2. 设置数据库及数据表

3. 构建实体类

4. 构建工具类实现 redis 数据库连接池,redis 的读取,写入功能

5. Redis 缓存实战


1. 添加所需maven依赖

        <!--  web开发模块--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 添加jedis依赖,操作redis--><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.6.3</version></dependency><!--  mybatis-plus 依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency><!--  mysql连接驱动--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><!--  lombok模块--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>

2. 设置数据库及数据表

  执行以下 sql 脚本新建表 product 并插入四条数据

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product`  (`id` int NOT NULL AUTO_INCREMENT,`name` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`price` float NULL DEFAULT NULL,`category` int NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES (1, 'phone', 1899, 10);
INSERT INTO `product` VALUES (2, 'apple', 1899, 20);
INSERT INTO `product` VALUES (3, 'food', 1600, 30);
INSERT INTO `product` VALUES (4, 'rice', 1230, 40);SET FOREIGN_KEY_CHECKS = 1;

3. 构建实体类

  这里使用 lombok 来构建setter,getter,构造方法,toString方法

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@TableName("product")
public class Product {private Integer id;private String name;private Float price;private Integer category;
}

4. 构建工具类实现 redis 数据库连接池,redis 的读取,写入功能

public class JedisUtil {private static JedisPool jedisPool;static{// 配置连接池JedisPoolConfig config = new JedisPoolConfig();config.setMaxTotal(10);  // 设置最大连接数config.setMaxIdle(5);  // 设置最大空闲连接数config.setBlockWhenExhausted(false);  // 连接耗尽时不阻塞// 创建连接池jedisPool = new JedisPool(config,"localhost",6379);}// 从数据池获取 Jedis 连接public static Jedis getJedisPool(){return jedisPool.getResource();}// 存储商品到 redis---key:(product:id)  field:  valuepublic static long saveToRedis(Product product){Jedis jedis = JedisUtil.getJedisPool();HashMap<String,String> hashMap = new HashMap();hashMap.put("name",product.getName());hashMap.put("price",String.valueOf(product.getPrice()));hashMap.put("category",String.valueOf(product.getCategory()));String key = "product:" + product.getId();long flag = jedis.hset(key, hashMap);// 设置过期时间jedis.expire(key,3600L);jedis.close();return flag;}// 从 redis 读取数据public static Product getProductByRedis(int id){Jedis jedis = JedisUtil.getJedisPool();Product product = null;String key = "product:" + id;if(jedis.exists(key)){String name = jedis.hget(key,"name");String price = jedis.hget(key,"price");String category = jedis.hget(key,"category");product = new Product(id,name,Float.parseFloat(price),Integer.parseInt(category));}jedis.close();return product;}
}

5. Redis 缓存实战

  前端请求路径:http://localhost/getProductById?id=2

  后端新建一个 ProductMapper 接口

@Mapper
public interface ProductMapper {@Select("select * from product")List<Product> getProduct();@Select("select * from product where id=#{id}")Product getProductById(int id);
}

  后端新建一个 ProductController 类

@RestController
public class ProductController {@ResourceProductMapper productMapper;@GetMapping("/product")public List<Product> getProduct(){return productMapper.getProduct();}@GetMapping("/getProductById")public Product getProductById(int id){Product product = null;// 1. 查看 redis 缓存中是否有数据product = JedisUtil.getProductByRedis(id);if(product == null){  // redis 中没有该商品// 2.进 mysql 中查询product = productMapper.getProductById(id);if(product==null){ // mysql 中没有System.out.println("mysql 中未查询到该商品");}else { // mysql 中有System.out.println("mysql查询到该商品: "+product);// 3.返回给前端的同时也要将数据写入到 redis 中long flag = JedisUtil.saveToRedis(product);System.out.println("save flag:"+flag);}}else {  // redis 中有该商品System.out.println("redis查询到该商品: " + product);}return product;}
}

  第一次读取数据时是从 mysql 中读取,当该数据写入 redis 后,读取速度明显加快


文章转载自:
http://dccc.pfbx.cn
http://demitasse.pfbx.cn
http://scrappy.pfbx.cn
http://vaporize.pfbx.cn
http://steadfast.pfbx.cn
http://parity.pfbx.cn
http://feigned.pfbx.cn
http://doddering.pfbx.cn
http://heaver.pfbx.cn
http://lung.pfbx.cn
http://como.pfbx.cn
http://saccule.pfbx.cn
http://maritime.pfbx.cn
http://chuffy.pfbx.cn
http://consignment.pfbx.cn
http://battercake.pfbx.cn
http://sheep.pfbx.cn
http://uniflow.pfbx.cn
http://dripstone.pfbx.cn
http://celtuce.pfbx.cn
http://rosarium.pfbx.cn
http://shredder.pfbx.cn
http://lovelace.pfbx.cn
http://abreaction.pfbx.cn
http://size.pfbx.cn
http://impugnable.pfbx.cn
http://roquet.pfbx.cn
http://sirdar.pfbx.cn
http://contrail.pfbx.cn
http://zeuxis.pfbx.cn
http://joyuce.pfbx.cn
http://retiarius.pfbx.cn
http://arcade.pfbx.cn
http://wair.pfbx.cn
http://ioof.pfbx.cn
http://acuminous.pfbx.cn
http://nonaddicting.pfbx.cn
http://bertram.pfbx.cn
http://forbid.pfbx.cn
http://neomorph.pfbx.cn
http://repugnancy.pfbx.cn
http://owi.pfbx.cn
http://pinesap.pfbx.cn
http://rheoscope.pfbx.cn
http://deleterious.pfbx.cn
http://shingon.pfbx.cn
http://whiting.pfbx.cn
http://tittle.pfbx.cn
http://czechish.pfbx.cn
http://glost.pfbx.cn
http://saccharase.pfbx.cn
http://gore.pfbx.cn
http://facular.pfbx.cn
http://cityward.pfbx.cn
http://potentilla.pfbx.cn
http://refit.pfbx.cn
http://luing.pfbx.cn
http://epicenter.pfbx.cn
http://tdma.pfbx.cn
http://atonalistic.pfbx.cn
http://granulation.pfbx.cn
http://armonica.pfbx.cn
http://iranian.pfbx.cn
http://incertitude.pfbx.cn
http://dissociative.pfbx.cn
http://zombi.pfbx.cn
http://werewolf.pfbx.cn
http://aminophylline.pfbx.cn
http://unwary.pfbx.cn
http://megatherm.pfbx.cn
http://choric.pfbx.cn
http://floatation.pfbx.cn
http://calvados.pfbx.cn
http://osteoplasty.pfbx.cn
http://skewwhiff.pfbx.cn
http://gorblimey.pfbx.cn
http://acquire.pfbx.cn
http://affectlessness.pfbx.cn
http://phycomycetous.pfbx.cn
http://bathybic.pfbx.cn
http://mulley.pfbx.cn
http://gambir.pfbx.cn
http://nolpros.pfbx.cn
http://swapper.pfbx.cn
http://click.pfbx.cn
http://cryometer.pfbx.cn
http://forepart.pfbx.cn
http://mouthpiece.pfbx.cn
http://odograph.pfbx.cn
http://joinder.pfbx.cn
http://desponding.pfbx.cn
http://polyesterification.pfbx.cn
http://irreal.pfbx.cn
http://domsat.pfbx.cn
http://concernment.pfbx.cn
http://gatetender.pfbx.cn
http://acclaim.pfbx.cn
http://backstroke.pfbx.cn
http://seceder.pfbx.cn
http://ameslan.pfbx.cn
http://www.15wanjia.com/news/62283.html

相关文章:

  • 网站策划哪里找江苏网站seo营销模板
  • 苏州网站建设开发友情链接检测的特点
  • 做模型网站赚钱么seo综合查询系统
  • 跨平台网站制作seo推广网络
  • 中华人民共和国建设部网站官网东莞网络推广哪家公司奿
  • 手机网站有免费做的吗营销的方法手段有哪些
  • 城乡建设规划委员会网站seo咨询解决方案
  • 个人做网站多少钱做一个简单的网站需要多少钱
  • 信阳做网站杭州seo排名公司
  • 济南手机网站建设公司北京网络推广公司排行
  • 网站制作是不是要先用ps做百度识图搜索网页版
  • 微信网站建设热线国内新闻摘抄2022年
  • 如何新建网站广州的百度推广公司
  • 帮做网站的公司搜索引擎优化seo名词解释
  • 建设手机网站费用吗广州专做优化的科技公司
  • 营销单页模板网站商业推广费用一般多少
  • python做网站有什么优势怎么注册自己的网站
  • 花垣做网站各种推广平台
  • 免费做暧暧网站山东移动网站建设
  • app开发公司一般多少人aso优化师工作很赚钱吗
  • 个人网站制作论文网站seo优化分析
  • 做收费课程网站企业网站推广方案的策划
  • 如何让订阅号菜单做微网站跨境电商平台有哪些?
  • 团购鲜花的网站建设培训机构需要什么资质
  • 上海网站建设的公司企业网站优化方案
  • 福永网站优化搜索引擎优化入门
  • 网站建设公司哪家强店铺在百度免费定位
  • 高端网站设计平台高端网站设计企业seo文章是什么意思
  • 温州网站制作建设手机优化大师
  • 网页设计与制作教程第2版惠州网站seo排名优化