JAVA全系列 教程
3762个小节阅读:7091k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
接下来我们编写产品管理功能,首先查询产品列表:
编写ProductMapper
,由于查询产品时要查询它关联的类型,所以需要自定义查询方法
xxxxxxxxxx
public interface ProductMapper extends BaseMapper<Product> {
Page<Product> findProductPage(Page<Product> page);
}
编写ProductMapper.xml
xxxxxxxxxx
<mapper namespace="com.itbaizhan.travel.mapper.ProductMapper">
<resultMap id="productMapper" type="com.itbaizhan.travel.pojo.Product">
<id property="pid" column="pid"></id>
<result property="productName" column="productName"></result>
<result property="price" column="price"></result>
<result property="status" column="status"></result>
<result property="hotline" column="hotline"></result>
<result property="productDesc" column="productDesc"></result>
<result property="pImage" column="pImage"></result>
<result property="cid" column="cid"></result>
<association property="category" column="cid" javaType="com.itbaizhan.travel.pojo.Category">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
</association>
</resultMap>
<select id="findProductPage" resultMap="productMapper">
SELECT *
FROM product
LEFT JOIN category ON product.cid = category.`cid`
order by product.pid desc
</select>
</mapper>
编写ProductService
xxxxxxxxxx
@Service
public class ProductService {
@Autowired
private ProductMapper productMapper;
public Page<Product> findPage(int page, int size){
Page selectPage = productMapper.findProductPage(new Page(page, size));
return selectPage;
}
}
编写ProductController
xxxxxxxxxx
@Controller
@RequestMapping("/backstage/product")
public class ProductController {
@Autowired
private ProductService productService;
@Autowired
private CategoryService categoryService;
@RequestMapping("/all")
public ModelAndView all(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size) {
Page<Product> productPage = productService.findPage(page, size);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("productPage", productPage);
modelAndView.setViewName("/backstage/product_all");
return modelAndView;
}
}
编写页面product_all.html
xxxxxxxxxx
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<title>旅游产品管理</title>
<th:block th:replace="/backstage/common_resources::common_css"/>
<th:block th:replace="/backstage/common_resources::common_js"/>
</head>
<body class="hold-transition skin-purple sidebar-mini">
<div class="wrapper">
<!-- 页面头部 -->
<header th:replace="~{/backstage/common_header::header}"></header>
<!-- 导航侧栏 -->
<aside th:replace="~{/backstage/common_aside::aside}"></aside>
<!-- 内容区域 -->
<div class="content-wrapper">
<!-- 内容头部 -->
<section class="content-header">
<h1>
旅游产品管理
<small>全部旅游产品</small>
</h1>
<ol class="breadcrumb">
<li><a th:href="@{/backstage/index}"><i
class="fa fa-dashboard"></i> 首页</a></li>
<li><a th:href="@{/backstage/product/all}">旅游产品管理</a></li>
<li class="active">全部旅游产品</li>
</ol>
</section>
<!-- 内容头部 /-->
<!-- 正文区域 -->
<section class="content"> <!-- .box-body -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">列表</h3>
</div>
<div class="box-body">
<!-- 数据表格 -->
<div class="table-box">
<!--工具栏-->
<div class="pull-left">
<div class="form-group form-inline">
<div class="btn-group">
<a th:href="@{/backstage/product/addPage}">
<button type="button" class="btn btn-default" title="新建">
<i class="fa fa-file-o"></i> 新建
</button>
</a>
</div>
</div>
</div>
<!--数据列表-->
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th></th>
<th>产品图片</th>
<th width="20%">产品名称</th>
<th>价格</th>
<th>咨询电话</th>
<th>产品状态</th>
<th>产品类型</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
<tr th:each="product:${productPage.records}">
<td><input name="ids" type="checkbox" value="${product.pid}"></td>
<td>
<img height="100" th:src="${product.pImage }">
</td>
<td th:text="${product.productName }"></td>
<td th:text="${product.price }"></td>
<td th:text="${product.hotline }"></td>
<td th:text="${product.status }"></td>
<td th:text="${product.category.cname }"></td>
<td class="text-center">
<a th:href="@{/backstage/product/edit(pid=${product.pid})}" class="btn bg-olive btn-xs">修改</a>
<a th:href="@{/backstage/product/updateStatus(pid=${product.pid})}" class="btn bg-gray btn-xs">开启/关闭</a>
</td>
</tr>
</tbody>
</table>
<!--数据列表/-->
</div>
<!-- 数据表格 /-->
</div>
<!-- .box-footer-->
<div class="box-footer">
<div class="pull-left">
<div class="form-group form-inline">
总共<span th:text="${productPage.pages}"></span>页,共<span th:text="${productPage.total}"></span>条数据。
</div>
</div>
<div class="box-tools pull-right">
<ul class="pagination"></ul>
</div>
</div>
<!-- /.box-footer-->
</div>
</section>
<!-- 正文区域 /-->
</div>
<!-- 底部导航 -->
<footer th:replace="~{/backstage/common_footer::footer}"></footer>
</div>
<!-- 在js中获取Model的值 -->
<script th:inline="javascript">
$(function () {
var currentPage = [[${productPage.current}]]; // 当前页
var pages = [[${productPage.pages}]]; // 总页数
var contextPath = [[${#servletContext.contextPath}]]; // 项目绝对路径
// 分页插件
$('.pagination').bootstrapPaginator({
bootstrapMajorVersion: 3, // bootstrap版本
currentPage: currentPage, // 当前页
totalPages: pages, // 总页数
numberOfPages: 5, // 最多显示多少页
itemTexts: function (type, page, current) {
switch (type) {
case "first":
return "首页";
case "prev":
return "上一页";
case "next":
return "下一页";
case "last":
return "末页";
case "page":
return page;
}
},
onPageClicked: function (event, originalEvent, type, page) { // 点击页码执行的方法
location.href = contextPath + "/backstage/product/all?page=" + page;
}
});
})
</script>
</body>
</html>
启动项目,测试功能