SpringBoot项目实战(下)

13.商品热销排行


13.1 商品-创建数据表

DROP TABLE IF EXISTS 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;

DROP TABLE IF EXISTS t_product_category;

CREATE TABLE t_product_category (
  id int(20) NOT NULL COMMENT '主键',
  parent_id bigint(20) DEFAULT NULL COMMENT '父分类id',
  name varchar(150) DEFAULT NULL COMMENT '名称',
  status int(1) DEFAULT '1' COMMENT '状态   1:正常   0:删除',
  sort_order int(4) DEFAULT NULL COMMENT '排序号',
  is_parent int(1) DEFAULT NULL COMMENT '是否是父分类   1:是  0:否',
  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;

13.2 创建实体类

创建Product.java实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
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;
}

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

13.3.1 分析SQL语句

select * from t_product where status=1 order by priority desc limit 0,4;

13.3.2 接口与抽象方法

创建ProductMapper接口并在接口中添加查询热销商品findHotProductList()方法。

/** 处理商品数据持久层接口 */
public interface ProductMapper {
   
	/**
	 * 查询热销商品前四
	 * @return
	 */
	List<Product> findHotList();
}

13.3.3 Mapper映射文件

创建ProductMapper.xml文件并添加查询热销商品findHotProductList()方法对应的sql语句。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.company.store.mapper.ProductMapper">

	<select id="findHotList" resultType="Product">
		SELECT * FROM t_product WHERE status=1 ORDER BY priority DESC LIMIT 0,4
	</select>
</mapper>

13.3.4 单元测试

在ProductMapperTest测试类中添加测试代码。

@SpringBootTest
public class ProtuctMapperTest {
   
	@Autowired
	ProductMapper productMapper;
	
	@Test
	public void getByParent() {
   
		List<Product> list = productMapper.findHotList();
		for(Product district:list) {
   
			System.out.println(district);
		}
	}
	
}

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

13.4.1 规划异常

13.4.2 接口与抽象方法

创建IProductService接口并在接口中添加查询热销商品findHotProductList()方法。

/** 处理商品数据的业务层接口 */
public interface IProductService {
   
	
	/**
	 * 查询热销商品前四
	 * @return
	 */
	List<Product> findHotProductList();
}

13.4.3 实现类

在ProductServiceImpl实现类中添加查询热销商品findHotProductList()方法。

@Service
public class ProductServiceImpl implements IProductService {
   
	@Autowired
	private ProductMapper productMapper;
	@Override
	public List<Product> findHotProductList() {
   

		List<Product> list = productMapper.findHotList();
		for(Product product:list) {
   
			product.setPriority(null);
			product.setModifiedTime(null);
			product.setModifiedUser(null);
			product.setCreatedTime(null);
			product.setCreatedUser(null);
		}
		return list;
	}

}

13.5 商品-热销排行-控制层

13.5.1 规划异常

13.5.2 设计请求

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

在这里插入图片描述

  • 在LoginInterceptorConfig添加白名单
patterns.add("/product/**");

13.5.3 处理请求

  • 创建ProductController类。
@RestController
@RequestMapping("product")
public class ProductController extends BaseController {
   
	@Autowired
	private IProductService productService;
	
	@RequestMapping("hot_list")
	public JsonResult<List<Product>> getHotList(){
   
		List<Product> data=productService.findHotProductList();
		return new JsonResult<List<Product>>(OK,data);
	}
}

13.5.4 测试请求

  • 直接访问: http://localhost:8080/product/hot_list

13.6 商品-商品详情-前端页面

  • 在index.html添加
<script type="text/javascript">
$(document).ready(function(){
   
$.ajax({
   
	url:"/product/hot_list",
	type:"GET",
	dataType:"JSON",
	success:function(res){
   
		let list=res.data;
		console.log("count:"+list.length);
		for(let i=0;i<list.length;i++){
   
			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(/#{price}/g,list[i].price);
			html=html.replace(/#{image}/g,list[i].image);
			html=html.replace(/#{title}/g,list[i].title);
			$("#hot-list").append(html);
		}
	}
});
});
</script>

14.显示商品详情


14.1 商品-商品详情-持久层

14.1.1 规划执行sql语句

根据商品id返回商品详情

select * from t_product where id=?

14.1.2 接口与抽象方法

  • ProductMapper
/**
* 根据商品id查询商品详情
* @param id
* @return
*/
Product findById(Integer id);  
  • 在ProductMapper.xml中添加映射
<select id="findById" resultType="Product">
	SELECT * FROM t_product WHERE id=#{id}
</select>

14.2 商品-商品详情-业务层

14.2.1 规划异常

  • 根据id没有找到商品的异常,在service.ex新建ProductNotFoundException继承ServiceException

14.2.2 接口和抽象方法

  • 在IProductService中添加抽象方法
/**
* 根据商品id查询商品详情
* @param id
* @return
*/
Product findById(Integer id);

14.2.3 抽象方法实现

  • 在ProductServiceImpl中实现抽象方法
@Override
public Product findById(Integer id) {
   
	Product product = productMapper.findById(id);
	if(product==null) {
   
		throw new ProductNotFoundException("尝试访问的商品数据不存在");
	}
	product.setPriority(null);
	product.setModifiedTime(null);
	product.setModifiedUser(null);
	product.setCreatedTime(null);
	product.setCreatedUser(null);
	return product;
}

14.3 商品-商品详情-控制层

14.3.1 规划异常

  • 在BaseController添加
else if(e instanceof ProductNotFoundException) {
   
			result.setState(4006);
		}

14.3.2 设计请求

在这里插入图片描述

14.3.3 处理请求

  • 在ProductController中添加方法
@GetMapping("{id}/detail")
public JsonResult<Product> getById(@PathVariable("id") Integer id){
   
	Product data= productService.findById(id);
	return new JsonResult<>(OK,data);
}
  • 尝试访问 http://localhost:8080/product/10000017/detail 进行测试、

14.4 商品-商品详情-前端

<script type="text/javascript" src="../js/jquery-getUrlParam.js"></script>
<script type="text/javascript">
	let id=$.getUrlParam("id");
	console.log("id="+id);
	$(document).ready(function(){
   
		$.get
		$.ajax({
   
			url:"/product/"+id+"/detail",
			type:"GET",
			contentType:"application/json",
			success:function(res){
   
				if(res.state==200){
   
					console.log("title="+res.data.title);
					$("#product-title").html(res.data.title);
					$("#product-sell-point").html(res.data.sell.point);
					$("#product-price").html(res.data.price);
					for(let i=0;i<=5;i++){
   
						$("#product-image-"+i+"-big"
  • 19
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SpringBoot项目实战笔记可以按照以下步骤进行: 1. 首先,你可以通过观看B站上的教程视频来学习SpringBoot项目实战。在视频中,你可以学习到如何使用SpringBoot、MyBatis和MySQL创建一个电脑商城项目。 2. 确保你的SpringBoot项目能够成功启动。找到被@SpringBootApplication注解修饰的入口启动类,并运行该类。如果你能够观察到图形化的界面,那么说明你的项目成功启动了。 3. 如果你还没有创建SpringBoot项目,你可以使用Spring Initializr来初始化一个新的项目。Spring Initializr是一个Web应用程序,可以为你生成Spring Boot项目的基本结构。你可以选择使用Maven或Gradle作为构建工具,并添加适合你的项目的依赖。然后,你只需要编写应用程序的代码即可。 希望以上信息对你有帮助!如果还有其他问题,请随时提问。123 #### 引用[.reference_title] - *1* *2* [SpringBoot项目实战笔记:电脑商城项目实战SpringBoot+MyBatis+MySQL)](https://blog.csdn.net/weixin_44260350/article/details/127746667)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [《SpringBoot实战》读书笔记](https://blog.csdn.net/sanhewuyang/article/details/104494202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值