一.SpringBoot是什么?

SpringBoot是spring家族中微型框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。

Spring Boot可以轻松创建独立的,生产级的基于Spring的应用程序,您可以“运行”。

特征:

  • 创建独立的Spring应用程序
  • 直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
  • 提供自以为是的“入门”依赖项以简化构建配置
  • 尽可能自动配置Spring和第三方库
  • 提供生产就绪功能,例如指标,运行状况检查和外部化配置
  • 绝对没有代码生成,也不需要XML配置

SpringBoot要解决的问题?

随着Java语言的慢慢发展,Java的开发显得格外的笨重,繁多的配置,低下的开发效率,复杂的部署流程以及第三方技术集成难度非常大而且杂。

在上述环境中,SpringBoot营运而生,它使用“习惯由于配置”的理念让你的项目快速的运行起来,使用SpringBoot很容易创建一个独立运行。它内置Tomcat,Servlet,Spring等等基本开发的功能。

SpringBoot的优势?

快速构建项目,敏捷式开发

项目可独立运行,无须外部依赖Servlet容器。

对主流框架无配置支持,简化开发,也可以修改默认值。

提供运行时的应用监控。极大的提高了开发、部署效率。

与spring cloud天然集成。

限制:将现有或传统的Spring Framework项目转换为Spring Boot应用程序是一个非常困难和耗时的过程。它仅适用于全新Spring项目。

什么是微服务?

首先微服务并没有一个官方的定义,想要直接描述微服务比较困难。

它是一种“软件的架构风格”,一个应用应该是一组小型服务。各个小型服务运行在各自的环境中,通过http的方式进行互通。

微服务化的核心就是将传统的一站式应用,根据业务拆分成一个一个的服务,彻底地去耦合,每一个微服务提供单个业务功能的服务,一个服务做一件事,从技术角度看就是一种小而独立的处理过程,类似进程概念,能够自行单独启动或销毁,拥有自己独立的数据库

二、springboot是什么

spring是一个为了解决企业级应用开发的复杂性而创建的,简化开发

三、spring是如何简化开发的?

为了降低Java开发的复杂性,Spring提供了以下四种关键策略:

1.基于pojo的轻量级和最小侵入性编程

2.通过IOC、依赖注入(DI)和面向接口实现松耦合

3.基于切面(AOP)和惯例进行声明式编程

4.通过切面和模板减少样式代码

四、springboot

springboot基于spring开发,springboot本身不提供spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于spring框架的应用程序。也就是说,它并不是用来替代spring的解决方案,而是和spring框架紧密结合用于提升spring开发者体验的工具。springboot以约定大于配置的核心思想,默认帮我们进行了很多设置,多数springboot应用只需要很少的spring配置。同时它集成了大量常用的第三方库配置(例如Redis、MongoDB、Jpa、RabbitMQ、Quartz等等),springboot应用中这些第三方库几乎可以零配置的开箱即用。所以,springboot是整合了所有的框架,它不是什么新框架。

商品热销排行

1 商品-创建数据表

1.使用use命令先选中store数据库。

USE store;
复制代码

2.在store数据库中创建t_product数据表。

CREATE TABLE t_product (id int(20) NOT NULL COMMENT '商品id',category_id int(20) DEFAULT NULL COMMENT '分类id',item_type varchar(100) DEFAULT NULL COMMENT '商品系列',title varchar(100) DEFAULT NULL COMMENT '商品标题',sell_point varchar(150) DEFAULT NULL COMMENT '商品卖点',price bigint(20) DEFAULT NULL COMMENT '商品单价',num int(10) DEFAULT NULL COMMENT '库存数量',image varchar(500) DEFAULT NULL COMMENT '图片路径',status int(1) DEFAULT '1' COMMENT '商品状态  1:上架   2:下架   3:删除',priority int(10) DEFAULT NULL COMMENT '显示优先级',created_time datetime DEFAULT NULL COMMENT '创建时间',modified_time datetime DEFAULT NULL COMMENT '最后修改时间',created_user varchar(50) DEFAULT NULL COMMENT '创建人',modified_user varchar(50) DEFAULT NULL COMMENT '最后修改人',PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
复制代码

2 商品-创建实体类

创建com.cy.store.entity.Product类,并继承自BaseEntity类。在类中声明与数据表中对应的属性。

package com.cy.store.entity;/** 商品数据的实体类 */
public class Product extends BaseEntity implements Serializable {private Integer id;private Integer categoryId;private String itemType;private String title;private String sellPoint;private Long price;private Integer num;private String image;private Integer status;private Integer priority;// Generate: Getter and Setter、Generate hashCode() and equals()、toString()
}
复制代码

3 商品-热销排行-持久层

3.1 规划需要执行的SQL语句

查询热销商品列表的SQL语句大致是。

SELECT * FROM t_product WHERE status=1 ORDER BY priority DESC LIMIT 0,4
复制代码

3.2 接口与抽象方法

在com.cy.store.mapper包下创建ProductMapper接口并在接口中添加查询热销商品findHotList()的方法。

package com.cy.store.mapper;
import com.cy.store.entity.Product;
import java.util.List;/** 处理商品数据的持久层接口 */
public interface ProductMapper {/*** 查询热销商品的前四名* @return 热销商品前四名的集合*/List<Product> findHotList();
}
复制代码

3.3 配置SQL映射

1.在main\resources\mapper文件夹下创建ProductMapper.xml文件,并在文件中配置findHotList()方法的映射。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cy.store.mapper.ProductMapper"><resultMap id="ProductEntityMap" type="com.cy.store.entity.Product"><id column="id" property="id"/><result column="category_id" property="categoryId"/><result column="item_type" property="itemType"/><result column="sell_point" property="sellPoint"/><result column="created_user" property="createdUser"/><result column="created_time" property="createdTime"/><result column="modified_user" property="modifiedUser"/><result column="modified_time" property="modifiedTime"/></resultMap><!-- 查询热销商品的前四名:List<Product> findHostList() --><select id="findHotList" resultMap="ProductEntityMap">SELECT*FROMt_productWHEREstatus=1ORDER BYpriority DESCLIMIT 0,4</select>
</mapper>
复制代码

2.在com.cy.store.mapper包下创建ProductMapperTests测试类,并添加测试方法。

package com.cy.store.mapper;
import com.cy.store.entity.Product;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductMapperTests {@Autowiredprivate ProductMapper productMapper;@Testpublic void findHotList() {List<Product> list = productMapper.findHotList();System.out.println("count=" + list.size());for (Product item : list) {System.out.println(item);}}
}
复制代码

4 商品-热销排行-业务层

4.1 规划异常

说明:无异常。

4.2 接口与抽象方法

创建com.cy.store.service.IProductService接口,并在接口中添加findHotList()方法。

package com.cy.store.service;
import com.cy.store.entity.Product;
import java.util.List;/** 处理商品数据的业务层接口 */
public interface IProductService {/*** 查询热销商品的前四名* @return 热销商品前四名的集合*/List<Product> findHotList();
}
复制代码

4.3 实现抽象方法

1.创建com.cy.store.service.impl.ProductServiceImpl类,并添加@Service注解;在类中声明持久层对象以及实现接口中的方法。

package com.cy.store.service.impl;
import com.cy.store.entity.Product;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;/** 处理商品数据的业务层实现类 */
@Service
public class ProductServiceImpl implements IProductService {@Autowiredprivate ProductMapper productMapper;@Overridepublic List<Product> findHotList() {List<Product> list = productMapper.findHotList();for (Product product : list) {product.setPriority(null);product.setCreatedUser(null);product.setCreatedTime(null);product.setModifiedUser(null);product.setModifiedTime(null);}return list;}
}
复制代码

2.在com.cy.store.service包下创建测试类ProductServiceTests,并编写测试方法。

package com.cy.store.service;
import com.cy.store.entity.Product;
import com.cy.store.service.ex.ServiceException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductServiceTests {@Autowiredprivate IProductService productService;@Testpublic void findHotList() {try {List<Product> list = productService.findHotList();System.out.println("count=" + list.size());for (Product item : list) {System.out.println(item);}} catch (ServiceException e) {System.out.println(e.getClass().getSimpleName());System.out.println(e.getMessage());}}
}
复制代码

5 商品-热销排行-控制器

5.1 处理异常

说明:无异常。

5.2 设计请求

1.设计用户提交的请求,并设计响应的方式。

请求路径:/products/hot_list
请求参数:无
请求类型:GET
响应结果:JsonResult<List<Product>>
是否拦截:否,需要将index.html和products/**添加到白名单
复制代码

2.在LoginInterceptorConfigurer类中将index.html页面和products/**请求添加到白名单。

patterns.add("/web/index.html");
patterns.add("/products/**");
复制代码

5.3 处理请求

1.创建com.cy.controller.ProductController类继承自BaseController类,类添加@RestController和@RequestMapping("products")注解,并在类中添加业务层对象。

package com.cy.store.controller;
import com.cy.store.entity.Product;
import com.cy.store.service.IProductService;
import com.cy.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;@RestController
@RequestMapping("products")
public class ProductController extends BaseController {@Autowiredprivate IProductService productService;
}
复制代码

2.在类中添加处理请求的getHotList()方法。

@RequestMapping("hot_list")
public JsonResult<List<Product>> getHotList() {List<Product> data = productService.findHotList();return new JsonResult<List<Product>>(OK, data);
}
复制代码

3.完成后启动项目,直接访问http://localhost:8080/products/hot_list进行测试。

6 商品-热销排行-前端页面

1.在index.html页面给“热销排行”列表的div标签设置id属性值。

<div id="hot-list" class="panel-body panel-item"><!-- ... -->
</div>
复制代码

2.在index.html页面中body标签内部的最后,添加展示热销排行商品的代码。

<script type="text/javascript">
$(document).ready(function() {showHotList();
});function showHotList() {$("#hot-list").empty();$.ajax({url: "/products/hot_list",type: "GET",dataType: "JSON",success: function(json) {let list = json.data;console.log("count=" + list.length);for (let i = 0; i < list.length; i++) {console.log(list[i].title);let html = '<div class="col-md-12">'+ '<div class="col-md-7 text-row-2"><a href="product.html?id=#{id}">#{title}</a></div>'+ '<div class="col-md-2">¥#{price}</div>'+ '<div class="col-md-3"><img src="..#{image}collect.png" class="img-responsive" /></div>'+ '</div>';html = html.replace(/#{id}/g, list[i].id);html = html.replace(/#{title}/g, list[i].title);html = html.replace(/#{price}/g, list[i].price);html = html.replace(/#{image}/g, list[i].image);$("#hot-list").append(html);}}});
}
</script>
复制代码

3.完成后启动项目,直接访问http://localhost:8080/web/index.html进行测试。

显示商品详情

1 商品-显示商品详情-持久层

1.1 规划需要执行的SQL语句

根据商品id显示商品详情的SQL语句大致是。

SELECT * FROM t_product WHERE id=?
复制代码

1.2 接口与抽象方法

在ProductMapper接口中添加抽象方法。

/*** 根据商品id查询商品详情* @param id 商品id* @return 匹配的商品详情,如果没有匹配的数据则返回null*/
Product findById(Integer id);
复制代码

1.3 配置SQL映射

1.在ProductMapper.xml文件中配置findById(Integer id)方法的映射。

<!-- 根据商品id查询商品详情:Product findById(Integer id) -->
<select id="findById" resultMap="ProductEntityMap">SELECT*FROMt_productWHEREid=#{id}
</select>
复制代码

2.在ProductMapperTests测试类中添加测试方法。

@Test
public void findById() {Integer id = 10000017;Product result = productMapper.findById(id);System.out.println(result);
}
复制代码

2 商品-显示商品详情-业务层

2.1 规划异常

如果商品数据不存在,应该抛出ProductNotFoundException,需要创建com.cy.store.service.ex.ProductNotFoundException异常。

package com.cy.store.service.ex;/** 商品数据不存在的异常 */
public class ProductNotFoundException extends ServiceException {// Override Methods...
}
复制代码

2.2 接口与抽象方法

在业务层IProductService接口中添加findById(Integer id)抽象方法。

/*** 根据商品id查询商品详情* @param id 商品id* @return 匹配的商品详情,如果没有匹配的数据则返回null*/
Product findById(Integer id);
复制代码

2.3 实现抽象方法

1.在ProductServiceImpl类中,实现接口中的findById(Integer id)抽象方法。

@Override
public Product findById(Integer id) {// 根据参数id调用私有方法执行查询,获取商品数据Product product = productMapper.findById(id);// 判断查询结果是否为nullif (product == null) {// 是:抛出ProductNotFoundExceptionthrow new ProductNotFoundException("尝试访问的商品数据不存在");}// 将查询结果中的部分属性设置为nullproduct.setPriority(null);product.setCreatedUser(null);product.setCreatedTime(null);product.setModifiedUser(null);product.setModifiedTime(null);// 返回查询结果return product;
}
复制代码

2.在ProductServiceTests测试类中编写测试方法。

@Test
public void findById() {try {Integer id = 100000179;Product result = productService.findById(id);System.out.println(result);} catch (ServiceException e) {System.out.println(e.getClass().getSimpleName());System.out.println(e.getMessage());}
}
复制代码

3 商品-显示商品详情-控制器

3.1 处理异常

在BaseController类中的handleException()方法中添加处理ProductNotFoundException的异常。

// ...
else if (e instanceof ProductNotFoundException) {result.setState(4006);
}
// ...
复制代码

3.2 设计请求

设计用户提交的请求,并设计响应的方式。

请求路径:/products/{id}/details
请求参数:@PathVariable("id") Integer id
请求类型:GET
响应结果:JsonResult<Product>
复制代码

3.3 处理请求

1.在ProductController类中添加处理请求的getById()方法。

@GetMapping("{id}/details")
public JsonResult<Product> getById(@PathVariable("id") Integer id) {// 调用业务对象执行获取数据Product data = productService.findById(id);// 返回成功和数据return new JsonResult<Product>(OK, data);
}
复制代码

2.完成后启动项目,直接访问http://localhost:8080/products/10000017/details进行测试。

4 商品-显示商品详情-前端页面

1.检查在product.html页面body标签内部的最后是否引入jquery-getUrlParam.js文件,如果引入无需重复引入。

<script type="text/javascript" src="../js/jquery-getUrlParam.js"></script>
复制代码

2.在product.html页面中body标签内部的最后添加获取当前商品详情的代码。

<script type="text/javascript">
let id = $.getUrlParam("id");
console.log("id=" + id);
$(document).ready(function() {$.ajax({url: "/products/" + id + "/details",type: "GET",dataType: "JSON",success: function(json) {if (json.state == 200) {console.log("title=" + json.data.title);$("#product-title").html(json.data.title);$("#product-sell-point").html(json.data.sellPoint);$("#product-price").html(json.data.price);for (let i = 1; i <= 5; i++) {$("#product-image-" + i + "-big").attr("src", ".." + json.data.image + i + "_big.png");$("#product-image-" + i).attr("src", ".." + json.data.image + i + ".jpg");}} else if (json.state == 4006) { // 商品数据不存在的异常location.href = "index.html";} else {alert("获取商品信息失败!" + json.message);}}});
});
</script>

springboot的商品设计热销排行实现相关推荐

  1. 商品热销排行【项目 商城】

    商品热销排行[项目 商城] 商品热销排行 1.商品--创建数据表 2.商品--创建实体类 3.商品热销排行--持久层 3.1 规划查询的SQL语句 3.2 接口与抽象方法 3.3 配置SQL映射 4. ...

  2. Django项目实战——14—(列表页热销排行、商品搜索、Haystack建立数据索引、渲染商品搜索结果、商品详情页)

    1.列表页热销排行 根据路径参数category_id查询出该类型商品销量前二的商品. 使用Ajax实现局部刷新的效果. 查询列表页热销排行数据 请求方式 请求参数:路径参数 响应结果:JSON {& ...

  3. 【store商城项目09】商品热销排行

    1. 创建数据表 1.1在store数据库中创建t_product数据表 CREATE TABLE t_product (id int(20) NOT NULL COMMENT '商品id',cate ...

  4. php商品状态精品 热销,ecshop商品列表,商品页,热销,精品,搜索列表页调用商品销售量(已销售数量)-ECSHOP教程网...

    ecshop各个页面调用商品销售量方法(原创可用)ECSHOP模板 首页的推荐商品包括热销推荐和促销三个文件 只对热销商品为例 第一步:打开根目录/includes/lib_goods.php文件.在 ...

  5. HTML5期末大作业:美食餐饮网站设计——美食餐饮管理(8页) HTML+CSS+JavaScript 美食餐饮商品设计 食物海鲜网页

    HTML5期末大作业:美食餐饮网站设计--美食餐饮管理(8页) HTML+CSS+JavaScript 美食餐饮商品设计 食物海鲜网页 作品介绍 1.网页作品简介 :HTML期末大学生网页设计作业 A ...

  6. 墨器杯垫 文创商品设计特优

    教育部昨举行「102年国立馆所文创商品设计比赛」颁奖典礼,台北科技大学创新设计研究所硕士生谢镇宇,为TW艺术教育馆设计「墨器」杯垫,取「默契」谐音,用5片压克力板,展现水墨画层层渲染效果,增加立体视觉 ...

  7. 统计不同省份的商品点击排行(两种实现方式)

    需求: 统计不同省份的商品点击排行 //数据val srcDatas = List(("zhangsan", "河北", "鞋"),(&qu ...

  8. Springboot项目如何设计接口中敏感字段模糊查询?

    目录 前言 场景分析 实现方案 环境配置 依赖配置 代码实现 总结 前言 在<Springboot项目如何设计接口中敏感字段的加密.解密>和<Springboot项目如何设计接口中敏 ...

  9. Java生鲜电商平台-商品基础业务架构设计-商品设计

    Java生鲜电商平台-商品基础业务架构设计-商品设计 在生鲜电商的商品中心,在电子商务公司一般是后台管理商品的地方.在前端而言,是商家为了展示商品信息给用户的地方,它是承担了商品的数据,订单,营销活动 ...

最新文章

  1. MySQL两表联查,根据不同条件获得不同数据
  2. 编程之美——买书问题:贪心算法
  3. SAP CRM Fiori my Opportunity Etag handling
  4. 谈谈如何构建技术部门的知识库
  5. OneAlert:国内首家 SaaS 模式的云告警平台
  6. Spring Boot 菜鸟教程 application.properties 常用配置
  7. Adaptive Execution patch and how to bulid on cdh5
  8. Luakit的前世今生
  9. java虚拟机内存_java虚拟机内存区域的划分以及作用详解
  10. python网络图可视化_蜘蛛网图实现Python可视化的方法
  11. 宝塔Inode信息使用率100%满了怎么清理?
  12. 地理图例大全整理初中
  13. Salesforce入门教程(中文)-019 VF创建输入表单
  14. 汉字编码与拼音输入法
  15. 艾伦·麦席森·图灵——如谜的解谜者
  16. 关于“上家公司离职原因”应聘者回答技巧分享
  17. 交通仿真软件国内外详情分析及发展概述
  18. 网易云课堂---布尔教育《8小时学会html》 笔记
  19. Linux虚拟文件系统、文件描述符、管道
  20. Android视频播放器ExoPlayer全屏

热门文章

  1. 服务器怎么装虚拟打印机,虚拟打印机安装不成功的原因
  2. Office 如何添加Adobe Acrobat虚拟PDF打印机
  3. 简单计算 ( 山东科技大学第二届ACM校赛)
  4. 制作linux包 u盘安装
  5. 渗透测试工程师的职业发展
  6. unity编辑器详细介绍
  7. 小白c++ C6386缓冲溢出问题请教
  8. 浅谈MySQL查询优化
  9. mariadb Galera集群部署
  10. RabbitMQ延时队列