写在前面
本篇我们来介绍后台商品列表动态分页和后台商品搜索功能的开发,这里面会涉及到mybatis-pagehelper的使用。
商品列表动态分页
忘了告诉你,你可以按住shift+ctrl+T进行类的搜索,这个功能是非常好用的。好了,我们正式开始介绍后台商品列表动态分页功能的实现。
首先打开ProductManageController.java文件,在里面新增以下代码:
/***
*
* 后台管理员--后台商品列表及动态分页
* */
@RequestMapping("list.do")
@ResponseBody //自动序列化json功能
public ServerResponse getList(HttpSession session,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
//判断一下登录情况
User user=(User) session.getAttribute(Const.CURRENT_USER);
if(user ==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
}
//判断一下是不是管理员身份
if(iUserService.checkAdminRole(user).isSuccess()){
//如果是管理员就进行后台商品列表及动态分页的逻辑
return iProductService.getProductList(pageNum,pageSize);
}else{
return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
}
}
接着打开ProductServiceImpl.java文件,里面写入以下代码:
/***
*
* 后台管理员--后台商品列表及动态分页
* */
public ServerResponse<PageInfo> getProductList(int pageNum,int pageSize){
/***
* 使用mybatis-pagehelper的三个步骤:
* 1、startPage--start
* 2、填充自己的sql查询逻辑
* 3、pageHelper-收尾
* */
//startPage--start
PageHelper.startPage(pageNum,pageSize);
//填充自己的sql查询逻辑
List<Product> productList =productMapper.selectList();
List<ProductListVo> productListVoList = Lists.newArrayList(); //Lists这是guava对list的封装
for(Product productItem : productList){
ProductListVo productListVo = assembleProductListVo(productItem);
productListVoList.add(productListVo);
}
//pageHelper-收尾
PageInfo pageResult = new PageInfo(productList);
pageResult.setList(productListVoList);
return ServerResponse.createBySuccess(pageResult);
}
//新的商品用于商品列表信息的获取
private ProductListVo assembleProductListVo(Product product) {
ProductListVo productListVo = new ProductListVo();
productListVo.setId(product.getId());
productListVo.setCategoryId(product.getCategoryId());
productListVo.setName(product.getName());
productListVo.setSubtitle(product.getSubtitle());
productListVo.setMainImage(product.getMainImage());
productListVo.setPrice(product.getPrice());
productListVo.setStatus(product.getStatus());
productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix", "http://img.licheetools.top/"));
return productListVo;
}
关于这段代码,我需要说明几点事项:
1、因为这里我们使用了mybatis-pagehelper这个分页插件,因此有必要了解一下它的官方文档:使用方法,觉得看不懂得可以参看这篇文章:手把手教你如何玩转插件:分页插件(Pagehelper)。
2、因为我们后台已经有一个实现商品详情信息的功能了,这里我们只是对其进行分页而已,而且是不需要获取更多的信息,因此有必要新建一个product对象,我们称其为ProductListVo。所以我们就要打开vo这个包,在里面新建一个ProductListVo类,里面的代码如下:
package top.store.vo;
import java.math.BigDecimal;
public class ProductListVo {
private Integer id;
private Integer categoryId;
private String name;
private String subtitle;
private String mainImage;
private BigDecimal price;
private Integer status;
private String imageHost;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getMainImage() {
return mainImage;
}
public void setMainImage(String mainImage) {
this.mainImage = mainImage;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getImageHost() {
return imageHost;
}
public void setImageHost(String imageHost) {
this.imageHost = imageHost;
}
}
也许你也注意到,我们就是选择自己需要的信息然后才进行组装成一个新的product。
3、我们继续回到ProductServiceImpl.java文件,这后面的那段组装新的Product的代码是不是非常眼熟,对就是和我们之前的商品详情信息组装几乎是一模一样的,但是还依然要注意ImageHost这个字段的获取,你可以照搬前面的代码。
4、看到List<Product> productList =productMapper.selectList();
这段代码,说明我们需要需要去productMapper.java文件里面添加以下代码:
List<Product> selectList();
然后打开productMapper.xml文件,往里面添加以下代码:
<select id="selectList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from store_product
order by id asc
</select>
最后打开IProductService.java文件,里面写入以下代码:
ServerResponse<PageInfo> getProductList(int pageNum, int pageSize); //后台管理员--后台商品列表及动态分页
后台商品搜索功能开发
老规矩,我们首先打开ProductManageController.java文件,在里面新增以下代码:
/***
*
* 后台管理员--后台商品搜索功能
* */
@RequestMapping("search.do")
@ResponseBody //自动序列化json功能
public ServerResponse productSearch(HttpSession session,String productName,Integer productId,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
//判断一下登录情况
User user=(User) session.getAttribute(Const.CURRENT_USER);
if(user ==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
}
//判断一下是不是管理员身份
if(iUserService.checkAdminRole(user).isSuccess()){
//如果是管理员就进行后台商品搜索功能开发的逻辑
return iProductService.productSearch(productName,productId,pageNum,pageSize);
}else{
return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
}
}
接着打开ProductServiceImpl.java文件,里面写入以下代码:
/***
*
* 后台管理员--后台商品搜索功能(同样也需要分页)
* */
public ServerResponse<PageInfo> productSearch(String productName,Integer productId,int pageNum, int pageSize){
//startPage--start
PageHelper.startPage(pageNum,pageSize);
if(StringUtils.isNotBlank(productName)){
productName = new StringBuilder().append("%").append(productName).append("%").toString();
}
//填充自己的sql查询逻辑
List<Product> productList =productMapper.selectByNameAndProductId(productName,productId);
List<ProductListVo> productListVoList = Lists.newArrayList(); //Lists这是guava对list的封装
for(Product productItem : productList){
ProductListVo productListVo = assembleProductListVo(productItem);
productListVoList.add(productListVo);
}
//pageHelper-收尾
PageInfo pageResult = new PageInfo(productList);
pageResult.setList(productListVoList);
return ServerResponse.createBySuccess(pageResult);
}
关于这段代码,我需要说明几点事项:
1、productName = new StringBuilder().append("%").append(productName).append("%").toString();
这行代码的意思就是构造一个新的productName对象,就是用于数据库字段的查找,我们知道如果你要进行查找,特别是模糊查找,那么相应的sql语句是:
select * from store_product where name like %productName%;
这里我们为了xml里面书写sql语句的便捷,就在这里进行了转化,此时的productName=%productName%
。
2、%:表示任意0个或多个字,可匹配任意类型和长度的字符;_: 表示任意单个字符,匹配单个任意字符,它常用来限制表达式的字符长度语句。
3、看到productMapper.selectByNameAndProductId(productName,productId);
你应该就知道需要打开dao包的ProductMapper.java这个接口,在里面定义一个查询sql的方法:
List<Product> selectByNameAndProductId(@Param(value = "productName") String productName,@Param(value = "productId") Integer productId); //注意在mybatis里面,多个参数时需要使用param注解。
然后我们打开ProductMapper.xml文件,在里面新增sql查询语句:
<select id="selectByNameAndProductId" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List"/>
from store_product
where id=#{productId} || name like #{productName}
</select>
这种你觉得可以莫,我觉得不能使用短路或,因为一旦前面id为空,后面就不进行判断了,所以我们需要进行改写:
下面的如何?使用了if判断条件,但是还是有问题就是假如两个条件都不满足条件,那就会报错,因此这种也是不可以的:
<select id="selectByNameAndProductId" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List"/>
from store_product
<if test="productName != null">
where name like #{productName}
</if>
<if test="productId != null">
and id =#{productId}
</if>
</select>
再来看看下面这个版本,这个经过使用where 1=1
这个条件,的确可以满足要求,但是你不觉得这个太Low了莫?在这里加一个没有任何关系的where 1=1
是不是没必要?对,mybatis给我们提供了一个where判断,可以避免这种情况的发生。
<select id="selectByNameAndProductId" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List"/>
from store_product
where 1=1
<if test="productName != null">
and name like #{productName}
</if>
<if test="productId != null">
and id =#{productId}
</if>
</select>
就是下面的代码:
<select id="selectByNameAndProductId" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List"/>
from store_product
<where>
<if test="productName != null">
and name like #{productName}
</if>
<if test="productId != null">
and id =#{productId}
</if>
</where>
</select>
或者是这样的,都是可以的:
<select id="selectByNameAndProductId" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List"/>
from store_product
<where>
<if test="productName != null">
or name like #{productName}
</if>
<if test="productId != null">
or id =#{productId}
</if>
</where>
</select>
也就是说一旦逻辑传到where里面,而且if判断条件成立的情况下,将所有的or或者and替换为where,这样我们就不需要担心后面的where条件是否成立了。
最后打开IProductService.java文件,里面写入以下代码:
ServerResponse<PageInfo> productSearch(String productName,Integer productId,int pageNum, int pageSize); //后台管理员--后台商品搜索功能(同样也需要分页)
这样我们本篇关于后台商品列表动态分页和后台商品搜索功能的开发介绍就到此为止了,感谢你的赏阅!
网友评论