package com.oracle.xiaomi.service;
import com.oracle.xiaomi.pojo.ProductInfo;
import java.util.List;
public interface ProductInfoService {
//显示所有商品
public List<ProductInfo> getAll();
}
package com.oracle.xiaomi.service.impl;
import com.oracle.xiaomi.mapper.ProductInfoMapper;
import com.oracle.xiaomi.pojo.ProductInfo;
import com.oracle.xiaomi.pojo.ProductInfoExample;
import com.oracle.xiaomi.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductInfoServiceImpl implements ProductInfoService {
//切记切记:要有Mapper接口对象,此对象由spring创建并注入
@Autowired
ProductInfoMapper productInfoMapper;
@Override
public List<ProductInfo> getAll() {
//构建查询条件的类,虽然此处并没有条件,也得借助于ProductInfoExample完成查询全部功能
ProductInfoExample productInfoExample=new ProductInfoExample();
return productInfoMapper.selectByExample(productInfoExample);
}
}
package com.oracle.xiaomi.controller;
import com.oracle.xiaomi.pojo.ProductInfo;
import com.oracle.xiaomi.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/prod")
public class ProductInfoController {
//切记切记:必须让spring依赖注入Service对象
@Autowired
ProductInfoService productInfoService;
@RequestMapping("/getall")
public String getAll(Model model){
List<ProductInfo> list = productInfoService.getAll();
model.addAttribute("plist", list);
return "product";
}
}
借助PageHelper jar包实现分页
package com.oracle.xiaomi.service;
import com.github.pagehelper.PageInfo;
import com.oracle.xiaomi.pojo.ProductInfo;
import java.util.List;
public interface ProductInfoService {
//显示所有商品
public List<ProductInfo> getAll();
//实现分页处理
public PageInfo splitPage(int page, int pageSize);
}
package com.oracle.xiaomi.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oracle.xiaomi.mapper.ProductInfoMapper;
import com.oracle.xiaomi.pojo.ProductInfo;
import com.oracle.xiaomi.pojo.ProductInfoExample;
import com.oracle.xiaomi.pojo.ProductType;
import com.oracle.xiaomi.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductInfoServiceImpl implements ProductInfoService {
//切记切记:要有Mapper接口对象,此对象由spring创建并注入
@Autowired
ProductInfoMapper productInfoMapper;
@Override
public List<ProductInfo> getAll() {
//构建查询条件的类,虽然此处并没有条件,也得借助于ProductInfoExample完成查询全部功能
ProductInfoExample productInfoExample=new ProductInfoExample();
return productInfoMapper.selectByExample(productInfoExample);
}
@Override
public PageInfo splitPage(int page, int pageSize) {
ProductInfoExample productInfoExample = new ProductInfoExample();
//为了在最前面显示新上线的产品进行排序,指定数据库中有的列名排序
productInfoExample.setOrderByClause("p_id desc");
//使用分页插件设置数据,切记切记:在提取数据集合之前,一定要使用分页工具设置当前页和每页的记录数
PageHelper.startPage(page, pageSize);
List<ProductInfo> list = productInfoMapper.selectByExample(productInfoExample);
//将查到的数据放入pageInfo工具类中
PageInfo<ProductInfo> pageInfo = new PageInfo<>(list);
return pageInfo;
}
}
package com.oracle.xiaomi.controller;
import com.github.pagehelper.PageInfo;
import com.oracle.xiaomi.pojo.ProductInfo;
import com.oracle.xiaomi.service.ProductInfoService;
import com.oracle.xiaomi.service.impl.ProductInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
@RequestMapping("/prod")
public class ProductInfoController {
public static final Integer PAGE_SIZE = 5;
//切记切记:必须让spring依赖注入Service对象
@Autowired
ProductInfoService productInfoService;
@RequestMapping("/getall")
public String getAll(Model model){
List<ProductInfo> list = productInfoService.getAll();
model.addAttribute("plist", list);
return "product";
}
@RequestMapping("/split")
public String split(
@RequestParam(defaultValue= "1")
Integer page, Model model) {
}
}
<!--分页栏-->
<div id="bottom">
<div>
<nav aria-label="..." style="text-align:center;">
<ul class="pagination">
<li>
<a href="javascript:showPage(${info.prePage}" aria-label="Previous"><span aria-hidden="true">«</span></a>
</li>
<c:forEach begin="1" end="${info.pages}" var="i">
<c:if test="${info.pageNum==i}">
<li><a style="background-color:lightslategray;color: #fff;"
href="javascript:showPage(${i})">${i}</a>
</li>
</c:if>
<c:if test="${info.pageNum!=i}">
<li>
<a href="javascript:showPage(${i})">${i}</a>
</li>
</c:if>
</c:forEach>
<li>
<a href="javascript:showPage(${info.nextPage})" aria-label="Next"><span aria-hidden="true">»</span></a>
</li>
<li style=" margin-left:150px;color: #0e90d2;height: 35px; line-height: 35px;">总共 <font
style="color:orange;">${info.pages}</font> 页 当前 <font
style="color:orange;">
<c:if test="${info.pageNum==0}">
1
</c:if>
<c:if test="${info.pageNum!=0}">
${info.pageNum}
</c:if></font> 页
</li>
</ul>
</nav>
</div>
</div>
</div>