e3mall项目:商品管理模块(后台)

e3mall项目:商品管理模块

一、页面跳转控制层(PageController)

package cn.e3mall.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 页面管理控制层
 * Author: xushuai
 * Date: 2018/5/12
 * Time: 15:57
 * Description:
 */
@Controller
public class PageController {

    /**
     * 跳转到主页
     * @auther: xushuai
     * @date: 2018/5/12 15:58
     */
    @RequestMapping("/")
    public String toHome(){
        return "index";
    }

    /**
     * 跳转到指定页面
     * @auther: xushuai
     * @date: 2018/5/12 16:02
     */
    @RequestMapping("{page}")
    public String toPage(@PathVariable String page){
        return page;
    }

    /**
     * 跳转到指定页面
     * @auther: xushuai
     * @date: 2018/5/12 16:02
     */
    @RequestMapping("/page/{page}")
    public String toPage2(@PathVariable String page){
        return page;
    }



}


二、商品管理

(1)商品控制层(ItemController)

package cn.e3mall.controller;

import cn.e3mall.common.entity.E3Result;
import cn.e3mall.common.entity.EasyUIDataGridResult;
import cn.e3mall.entity.TbItem;
import cn.e3mall.service.ItemService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 商品控制层
 * Author: xushuai
 * Date: 2018/5/11
 * Time: 15:36
 * Description:
 */
@Controller
public class ItemController {

    @Autowired
    private ItemService itemService;

    /*
     * 分页查询商品列表
     */
    @RequestMapping("/item/list")
    @ResponseBody
    public EasyUIDataGridResult list(Integer page, Integer rows){
        //调用itemService,获取数据返回
        return itemService.getItemList(page,rows);
    }

    /*
     * 新增商品
     */
    @RequestMapping(value="/item/save",method = RequestMethod.POST)
    @ResponseBody
    public E3Result save(TbItem item, String desc){
        return itemService.save(item,desc);
    }


    /*
     * 加载商品描述
     */
    @RequestMapping("/item/desc/{id}")
    @ResponseBody
    public E3Result loadDesc(@PathVariable Long id){
        return itemService.findDescByItemid(id);
    }

    /*
     * 加载商品规格
     */
    @RequestMapping("/item/query/{id}")
    @ResponseBody
    public E3Result loadParam(@PathVariable Long id){
        return itemService.findParamItemByItemid(id);
    }

    /*
     * 修改商品
     */
    @RequestMapping("/item/update")
    @ResponseBody
    public E3Result edit(TbItem item,String desc){
        return itemService.edit(item,desc);
    }

    /*
     * 商品删除
     */
    @RequestMapping("/item/delete")
    @ResponseBody
    public E3Result delete(String[] ids){
        return itemService.updateStatusByItemId(ids,TbItem.ITEM_STATUS_THREE);
    }

    /*
     * 商品上架
     */
    @RequestMapping("/item/reshelf")
    @ResponseBody
    public E3Result reshelf(String[] ids){
        return itemService.updateStatusByItemId(ids,TbItem.ITEM_STATUS_ONE);
    }

    /*
     * 商品下架
     */
    @RequestMapping("/item/instock")
    @ResponseBody
    public E3Result instock(String[] ids){
        return itemService.updateStatusByItemId(ids,TbItem.ITEM_STATUS_TWO);
    }


}

(2)商品业务逻辑层(ItemService、ItemServiceImpl)

package cn.e3mall.service;

import cn.e3mall.common.entity.E3Result;
import cn.e3mall.common.entity.EasyUIDataGridResult;
import cn.e3mall.entity.TbItem;

/**
 * @Auther: Administrator
 * @Date: 2018/5/11 15:40
 * @Description:
 */
public interface ItemService {
    /**
     * ID加载商品
     * @auther: xushuai
     * @date: 2018/5/11 15:40
     * @return: 商品对象
     */
    E3Result load(Long itemId);

    /**
     * 加载全部商品列表
     * @auther: xushuai
     * @date: 2018/5/12 17:14
     */
    EasyUIDataGridResult getItemList(int page, int size);

    /**
     * 新增商品
     * @auther: xushuai
     * @date: 2018/5/16 9:35
     */
    E3Result save(TbItem item, String desc);

    /**
     * itemId加载商品描述
     * @auther: xushuai
     * @date: 2018/5/16 13:26
     */
    E3Result findDescByItemid(Long id);

    /**
     * itemId加载商品规格
     * @auther: xushuai
     * @date: 2018/5/16 13:47
     */
    E3Result findParamItemByItemid(Long id);

    /**
     * 修改指定商品
     * @auther: xushuai
     * @date: 2018/5/16 13:58
     */
    E3Result edit(TbItem item, String desc);

    /**
     * 修改指定商品状态(可以为多个)
     * @auther: xushuai
     * @date: 2018/5/16 23:23
     */
    E3Result updateStatusByItemId(String[] ids,Byte status);

}
package cn.e3mall.service.impl;

import cn.e3mall.common.entity.E3Result;
import cn.e3mall.common.entity.EasyUIDataGridResult;
import cn.e3mall.common.utils.IDUtils;
import cn.e3mall.dao.TbItemDescMapper;
import cn.e3mall.dao.TbItemMapper;
import cn.e3mall.dao.TbItemParamItemMapper;
import cn.e3mall.entity.*;
import cn.e3mall.service.ItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Item控制层
 * Author: xushuai
 * Date: 2018/5/11
 * Time: 15:40
 * Description:
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper itemMapper;

    @Autowired
    private TbItemDescMapper itemDescMapper;

    @Autowired
    private TbItemParamItemMapper itemParamItemMapper;

    @Override
    public E3Result load(Long itemId) {
        return E3Result.ok(itemMapper.selectByPrimaryKey(itemId));
    }

    @Override
    public EasyUIDataGridResult getItemList(int page, int size) {
        //设置分页
        PageHelper.startPage(page, size);

        //创建页面需要的数据格式对象
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        //创建查询条件
        TbItemExample itemExample = new TbItemExample();
        //执行查询
        List<TbItem> tbItems = itemMapper.selectByExample(itemExample);
        //获取分页信息对象
        PageInfo<TbItem> pageInfo = new PageInfo<>(tbItems);

        //设置总页数
        result.setTotal((int) pageInfo.getTotal());
        //设置数据集合(BeanList)
        result.setRows(pageInfo.getList());

        return result;
    }

    @Override
    public E3Result save(TbItem item, String desc) {
        //生成商品ID
        long itemId = IDUtils.genItemId();

        //补全商品的信息(分别为:ID、创建时间、修改时间以及商品状态)
        item.setId(itemId);
        item.setCreated(new Date());
        item.setUpdated(new Date());
        //设置商品状态为正常
        item.setStatus(TbItem.ITEM_STATUS_ONE);
        //保存商品
        itemMapper.insertSelective(item);

        TbItemDesc itemDesc = new TbItemDesc();
        //补全商品描述对象信息
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(new Date());
        itemDesc.setUpdated(new Date());
        //保存商品描述
        itemDescMapper.insert(itemDesc);

        //返回成功信息
        return E3Result.ok();
    }

    @Override
    public E3Result findDescByItemid(Long id) {
        //创建查询条件对象
        TbItemDescExample example = new TbItemDescExample();
        //封装查询条件
        example.createCriteria().andItemIdEqualTo(id);
        //执行查询,返回结果
        List<TbItemDesc> tbItemDescs = itemDescMapper.selectByExampleWithBLOBs(example);
        if(tbItemDescs != null && tbItemDescs.size() > 0){
            return E3Result.ok(tbItemDescs.get(0));
        }
        return null;
    }

    @Override
    public E3Result findParamItemByItemid(Long id) {
        //创建查询条件
        TbItemParamItemExample example = new TbItemParamItemExample();
        //封装查询条件
        example.createCriteria().andItemIdEqualTo(id);
        //执行查询,获取结果
        List<TbItemParamItem> tbItemParamItems = itemParamItemMapper.selectByExampleWithBLOBs(example);
        if(tbItemParamItems != null && tbItemParamItems.size() > 0){
            return E3Result.ok(tbItemParamItems.get(0));
        }
        return null;
    }

    @Override
    public E3Result edit(TbItem item, String desc) {
        //更新
        itemMapper.updateByPrimaryKeySelective(item);
        //返回成功
        return E3Result.ok();
    }

    @Override
    public E3Result updateStatusByItemId(String[] ids,Byte status) {
        //遍历ID数组
        for(String id : ids){
            //ID查询
            TbItem _item = itemMapper.selectByPrimaryKey(Long.valueOf(id));
            //查询出来的商品不为空,且状态不为删除状态
            if(_item != null && _item.getStatus() != status){
                //执行删除操作
                _item.setStatus(status);
                itemMapper.updateByPrimaryKeySelective(_item);
            }
        }
        //返回成功
        return E3Result.ok();
    }
}

(3)商品持久层为mybatis逆向工程生成,无新增代码


三、商品管理需要的其他模块功能

(1)加载商品分类

  商品分类控制层(ItemCatController)
package cn.e3mall.controller;

import cn.e3mall.common.entity.EasyUITreeNodeResult;
import cn.e3mall.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商品分类控制层
 * Author: xushuai
 * Date: 2018/5/12
 * Time: 20:29
 * Description:
 */
@Controller
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;


    @RequestMapping("/item/cat/list")
    @ResponseBody
    public List<EasyUITreeNodeResult> list(@RequestParam(defaultValue = "0") Long id){
        return itemCatService.getItemCatList(id);
    }

}


商品分类业务逻辑层(ItemCatService、ItemCatServiceImpl)

package cn.e3mall.service;

import cn.e3mall.common.entity.EasyUITreeNodeResult;

import java.util.List;

/**
 * 商品分类业务逻辑层
 * @Auther: xushuai
 * @Date: 2018/5/12 20:49
 * @Description:
 */
public interface ItemCatService {

    /**
     * parentId加载商品分类列表,并封装成需要的数据格式
     * @auther: xushuai
     * @date: 2018/5/12 20:50
     * @return: EasyUITree控件需要的数据格式对象
     */
    List<EasyUITreeNodeResult> getItemCatList(Long parentId);
}
package cn.e3mall.service.impl;

import cn.e3mall.common.entity.EasyUITreeNodeResult;
import cn.e3mall.dao.TbItemCatMapper;
import cn.e3mall.entity.TbItemCat;
import cn.e3mall.entity.TbItemCatExample;
import cn.e3mall.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类业务逻辑层实现
 * Author: xushuai
 * Date: 2018/5/12
 * Time: 20:51
 * Description:
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<EasyUITreeNodeResult> getItemCatList(Long parentId) {
        //创建查询条件对象
        TbItemCatExample example = new TbItemCatExample();
        //封装查询条件
        example.createCriteria().andParentIdEqualTo(parentId);
        //执行查询,获得结果集
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(example);

        List<EasyUITreeNodeResult> results = new ArrayList<>();
        //将获得的结果集,封装成需要的数据格式
        //遍历结果集
        for(TbItemCat itemCat : tbItemCats){
            //封装数据到EasyUITreeNodeResult
            EasyUITreeNodeResult nodeResult = new EasyUITreeNodeResult();
            nodeResult.setId(itemCat.getId());
            nodeResult.setState(itemCat.getIsParent()?"closed":"open");
            nodeResult.setText(itemCat.getName());

            //添加到list集合中
            results.add(nodeResult);
        }

        return results;
    }
}



(2)图片上传模块

图片上传控制层(PicController)

package cn.e3mall.controller;

import cn.e3mall.common.utils.FastDFSClient;
import cn.e3mall.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 上传图片控制层
 * Author: xushuai
 * Date: 2018/5/14
 * Time: 20:32
 * Description:
 */
@Controller
public class PicController {

    @Value("${file.server}")
    private String fileServer;

    @RequestMapping(value="/pic/upload",produces = MediaType.TEXT_PLAIN_VALUE + ";charset=utf-8")
    @ResponseBody
    public String fileUpload(MultipartFile uploadFile){
        Map result = new HashMap();
        try {
            //获取完成文件名
            String originalFilename = uploadFile.getOriginalFilename();
            //取出扩展名
            String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

            //创建文件上传工具类对象
            FastDFSClient fastDFSClient = new FastDFSClient("classpath:other/file-client.conf");
            //上传文件
            String filename = fastDFSClient.uploadFile(uploadFile.getBytes(), ext);
            //获取返回的图片所在位置
            String filepath = fileServer + filename;
            //返回成功信息
            result.put("error",0);
            result.put("url",filepath);
            //返回Map集合生成的json字符串
            return JsonUtils.objectToJson(result);
        }catch (Exception e){
            e.printStackTrace();
            //返回失败信息
            result.put("error",1);
            result.put("message","上传失败");
            return JsonUtils.objectToJson(result);
        }
    }
}

图片上传相关配置文件

other/client.conf
tracker_server=192.168.25.133:22122
other/resources.properties
# 图片服务器IP
file.server=http://192.168.25.133/


四、相关配置文件

(1)注册服务(applicationContext-service.xml中新增)

    <dubbo:service interface="cn.e3mall.service.ItemCatService" ref="itemCatServiceImpl" timeout="300000"/>

(2)读取resources配置文件以及引用服务(springmvc.xml中新增)

    <dubbo:reference interface="cn.e3mall.service.ItemCatService" id="itemCatService" />

    <!--读取resource配置文件-->
    <context:property-placeholder location="classpath:other/resource.properties"/>

     <!--配置文件上传解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设定默认编码 -->
        <property name="defaultEncoding" value="UTF-8"></property>
        <!-- 设定文件上传的最大值5MB,5*1024*1024 -->
        <property name="maxUploadSize" value="5242880"></property>
    </bean>


阅读更多
博主设置当前文章不允许评论。

没有更多推荐了,返回首页