静态代码块优于普通代码块,普通代码块优于构造代码块。
每次new对象的时候都会执行构造代码块和普通代码块,而静态代码块仅仅在加载类的时候执行并且仅仅执行一次。
ProductDetailVo:
package com.mmall.vo; import java.math.BigDecimal; /** * Created by geely */ public class ProductDetailVo { private Integer id; private Integer categoryId; private String name; private String subtitle; private String mainImage; private String subImages; private String detail; private BigDecimal price; private Integer stock; private Integer status; private String createTime; private String updateTime; private String imageHost; private Integer parentCategoryId; 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 String getSubImages() { return subImages; } public void setSubImages(String subImages) { this.subImages = subImages; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public Integer getStock() { return stock; } public void setStock(Integer stock) { this.stock = stock; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public String getUpdateTime() { return updateTime; } public void setUpdateTime(String updateTime) { this.updateTime = updateTime; } public String getImageHost() { return imageHost; } public void setImageHost(String imageHost) { this.imageHost = imageHost; } public Integer getParentCategoryId() { return parentCategoryId; } public void setParentCategoryId(Integer parentCategoryId) { this.parentCategoryId = parentCategoryId; } }
DateTimeUtil:
package com.mmall.util; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.util.Date; /** * Created by geely */ public class DateTimeUtil { //joda-time //str->Date //Date->str public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static Date strToDate(String dateTimeStr,String formatStr){ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr); DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr); return dateTime.toDate(); } public static String dateToStr(Date date,String formatStr){ if(date == null){ return StringUtils.EMPTY; } DateTime dateTime = new DateTime(date); return dateTime.toString(formatStr); } public static Date strToDate(String dateTimeStr){ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT); DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr); return dateTime.toDate(); } public static String dateToStr(Date date){ if(date == null){ return StringUtils.EMPTY; } DateTime dateTime = new DateTime(date); return dateTime.toString(STANDARD_FORMAT); } public static void main(String[] args) { System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss")); System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11","yyyy-MM-dd HH:mm:ss")); } }
ProductServiceImpl:
package com.mmall.service.impl; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.google.common.collect.Lists; import com.mmall.common.Const; import com.mmall.common.ResponseCode; import com.mmall.common.ServerResponse; import com.mmall.dao.CategoryMapper; import com.mmall.dao.ProductMapper; import com.mmall.pojo.Category; import com.mmall.pojo.Product; import com.mmall.service.ICategoryService; import com.mmall.service.IProductService; import com.mmall.util.DateTimeUtil; import com.mmall.util.PropertiesUtil; import com.mmall.vo.ProductDetailVo; import com.mmall.vo.ProductListVo; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; /** * Created by geely */ @Service("iProductService") public class ProductServiceImpl implements IProductService { @Autowired private ProductMapper productMapper; @Autowired private CategoryMapper categoryMapper; @Autowired private ICategoryService iCategoryService; public ServerResponse saveOrUpdateProduct(Product product){ if(product != null) { if(StringUtils.isNotBlank(product.getSubImages())){ String[] subImageArray = product.getSubImages().split(","); if(subImageArray.length > 0){ product.setMainImage(subImageArray[0]); } } if(product.getId() != null){ int rowCount = productMapper.updateByPrimaryKey(product); if(rowCount > 0){ return ServerResponse.createBySuccess("更新产品成功"); } return ServerResponse.createBySuccess("更新产品失败"); }else{ int rowCount = productMapper.insert(product); if(rowCount > 0){ return ServerResponse.createBySuccess("新增产品成功"); } return ServerResponse.createBySuccess("新增产品失败"); } } return ServerResponse.createByErrorMessage("新增或更新产品参数不正确"); } public ServerResponse<String> setSaleStatus(Integer productId,Integer status){ if(productId == null || status == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc()); } Product product = new Product(); product.setId(productId); product.setStatus(status); int rowCount = productMapper.updateByPrimaryKeySelective(product); if(rowCount > 0){ return ServerResponse.createBySuccess("修改产品销售状态成功"); } return ServerResponse.createByErrorMessage("修改产品销售状态失败"); } public ServerResponse<ProductDetailVo> manageProductDetail(Integer productId){ if(productId == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc()); } Product product = productMapper.selectByPrimaryKey(productId); if(product == null){ return ServerResponse.createByErrorMessage("产品已下架或者删除"); } ProductDetailVo productDetailVo = assembleProductDetailVo(product); return ServerResponse.createBySuccess(productDetailVo); } private ProductDetailVo assembleProductDetailVo(Product product){ ProductDetailVo productDetailVo = new ProductDetailVo(); productDetailVo.setId(product.getId()); productDetailVo.setSubtitle(product.getSubtitle()); productDetailVo.setPrice(product.getPrice()); productDetailVo.setMainImage(product.getMainImage()); productDetailVo.setSubImages(product.getSubImages()); productDetailVo.setCategoryId(product.getCategoryId()); productDetailVo.setDetail(product.getDetail()); productDetailVo.setName(product.getName()); productDetailVo.setStatus(product.getStatus()); productDetailVo.setStock(product.getStock()); productDetailVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://img.happymmall.com/")); Category category = categoryMapper.selectByPrimaryKey(product.getCategoryId()); if(category == null){ productDetailVo.setParentCategoryId(0);//默认根节点 }else{ productDetailVo.setParentCategoryId(category.getParentId()); } productDetailVo.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime())); productDetailVo.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime())); return productDetailVo; } public ServerResponse<PageInfo> getProductList(int pageNum,int pageSize){ //startPage--start //填充自己的sql查询逻辑 //pageHelper-收尾 PageHelper.startPage(pageNum,pageSize); List<Product> productList = productMapper.selectList(); List<ProductListVo> productListVoList = Lists.newArrayList(); for(Product productItem : productList){ ProductListVo productListVo = assembleProductListVo(productItem); productListVoList.add(productListVo); } 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.setName(product.getName()); productListVo.setCategoryId(product.getCategoryId()); productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://img.happymmall.com/")); productListVo.setMainImage(product.getMainImage()); productListVo.setPrice(product.getPrice()); productListVo.setSubtitle(product.getSubtitle()); productListVo.setStatus(product.getStatus()); return productListVo; } public ServerResponse<PageInfo> searchProduct(String productName,Integer productId,int pageNum,int pageSize){ PageHelper.startPage(pageNum,pageSize); if(StringUtils.isNotBlank(productName)){ productName = new StringBuilder().append("%").append(productName).append("%").toString(); } List<Product> productList = productMapper.selectByNameAndProductId(productName,productId); List<ProductListVo> productListVoList = Lists.newArrayList(); for(Product productItem : productList){ ProductListVo productListVo = assembleProductListVo(productItem); productListVoList.add(productListVo); } PageInfo pageResult = new PageInfo(productList); pageResult.setList(productListVoList); return ServerResponse.createBySuccess(pageResult); } public ServerResponse<ProductDetailVo> getProductDetail(Integer productId){ if(productId == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc()); } Product product = productMapper.selectByPrimaryKey(productId); if(product == null){ return ServerResponse.createByErrorMessage("产品已下架或者删除"); } if(product.getStatus() != Const.ProductStatusEnum.ON_SALE.getCode()){ return ServerResponse.createByErrorMessage("产品已下架或者删除"); } ProductDetailVo productDetailVo = assembleProductDetailVo(product); return ServerResponse.createBySuccess(productDetailVo); } public ServerResponse<PageInfo> getProductByKeywordCategory(String keyword,Integer categoryId,int pageNum,int pageSize,String orderBy){ if(StringUtils.isBlank(keyword) && categoryId == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc()); } List<Integer> categoryIdList = new ArrayList<Integer>(); if(categoryId != null){ Category category = categoryMapper.selectByPrimaryKey(categoryId); if(category == null && StringUtils.isBlank(keyword)){ //没有该分类,并且还没有关键字,这个时候返回一个空的结果集,不报错 PageHelper.startPage(pageNum,pageSize); List<ProductListVo> productListVoList = Lists.newArrayList(); PageInfo pageInfo = new PageInfo(productListVoList); return ServerResponse.createBySuccess(pageInfo); } categoryIdList = iCategoryService.selectCategoryAndChildrenById(category.getId()).getData(); } if(StringUtils.isNotBlank(keyword)){ keyword = new StringBuilder().append("%").append(keyword).append("%").toString(); } PageHelper.startPage(pageNum,pageSize); //排序处理 if(StringUtils.isNotBlank(orderBy)){ if(Const.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){ String[] orderByArray = orderBy.split("_"); PageHelper.orderBy(orderByArray[0]+" "+orderByArray[1]); } } List<Product> productList = productMapper.selectByNameAndCategoryIds(StringUtils.isBlank(keyword)?null:keyword,categoryIdList.size()==0?null:categoryIdList); List<ProductListVo> productListVoList = Lists.newArrayList(); for(Product product : productList){ ProductListVo productListVo = assembleProductListVo(product); productListVoList.add(productListVo); } PageInfo pageInfo = new PageInfo(productList); pageInfo.setList(productListVoList); return ServerResponse.createBySuccess(pageInfo); } }
ProductMapper:
package com.mmall.dao; import com.mmall.pojo.Product; import org.apache.ibatis.annotations.Param; import java.util.List; public interface ProductMapper { int deleteByPrimaryKey(Integer id); int insert(Product record); int insertSelective(Product record); Product selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Product record); int updateByPrimaryKey(Product record); List<Product> selectList(); List<Product> selectByNameAndProductId(@Param("productName") String productName, @Param("productId") Integer productId); List<Product> selectByNameAndCategoryIds(@Param("productName") String productName, @Param("categoryIdList") List<Integer> categoryIdList); }
ProductMapper:
<?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.mmall.dao.ProductMapper" > <resultMap id="BaseResultMap" type="com.mmall.pojo.Product" > <constructor > <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="category_id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="name" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="subtitle" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="main_image" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="sub_images" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="detail" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="price" jdbcType="DECIMAL" javaType="java.math.BigDecimal" /> <arg column="stock" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="status" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" /> <arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" /> </constructor> </resultMap> <sql id="Base_Column_List" > id, category_id, name, subtitle, main_image, sub_images, detail, price, stock, status, create_time, update_time </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from mmall_product where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from mmall_product where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.mmall.pojo.Product" > insert into mmall_product (id, category_id, name, subtitle, main_image, sub_images, detail, price, stock, status, create_time, update_time ) values (#{id,jdbcType=INTEGER}, #{categoryId,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{subtitle,jdbcType=VARCHAR}, #{mainImage,jdbcType=VARCHAR}, #{subImages,jdbcType=VARCHAR}, #{detail,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, #{stock,jdbcType=INTEGER}, #{status,jdbcType=INTEGER}, now(), now() ) </insert> <insert id="insertSelective" parameterType="com.mmall.pojo.Product" > insert into mmall_product <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="categoryId != null" > category_id, </if> <if test="name != null" > name, </if> <if test="subtitle != null" > subtitle, </if> <if test="mainImage != null" > main_image, </if> <if test="subImages != null" > sub_images, </if> <if test="detail != null" > detail, </if> <if test="price != null" > price, </if> <if test="stock != null" > stock, </if> <if test="status != null" > status, </if> <if test="createTime != null" > create_time, </if> <if test="updateTime != null" > update_time, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="categoryId != null" > #{categoryId,jdbcType=INTEGER}, </if> <if test="name != null" > #{name,jdbcType=VARCHAR}, </if> <if test="subtitle != null" > #{subtitle,jdbcType=VARCHAR}, </if> <if test="mainImage != null" > #{mainImage,jdbcType=VARCHAR}, </if> <if test="subImages != null" > #{subImages,jdbcType=VARCHAR}, </if> <if test="detail != null" > #{detail,jdbcType=VARCHAR}, </if> <if test="price != null" > #{price,jdbcType=DECIMAL}, </if> <if test="stock != null" > #{stock,jdbcType=INTEGER}, </if> <if test="status != null" > #{status,jdbcType=INTEGER}, </if> <if test="createTime != null" > now(), </if> <if test="updateTime != null" > now(), </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.mmall.pojo.Product" > update mmall_product <set > <if test="categoryId != null" > category_id = #{categoryId,jdbcType=INTEGER}, </if> <if test="name != null" > name = #{name,jdbcType=VARCHAR}, </if> <if test="subtitle != null" > subtitle = #{subtitle,jdbcType=VARCHAR}, </if> <if test="mainImage != null" > main_image = #{mainImage,jdbcType=VARCHAR}, </if> <if test="subImages != null" > sub_images = #{subImages,jdbcType=VARCHAR}, </if> <if test="detail != null" > detail = #{detail,jdbcType=VARCHAR}, </if> <if test="price != null" > price = #{price,jdbcType=DECIMAL}, </if> <if test="stock != null" > stock = #{stock,jdbcType=INTEGER}, </if> <if test="status != null" > status = #{status,jdbcType=INTEGER}, </if> <if test="createTime != null" > create_time = #{createTime,jdbcType=TIMESTAMP}, </if> <if test="updateTime != null" > update_time = now(), </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.mmall.pojo.Product" > update mmall_product set category_id = #{categoryId,jdbcType=INTEGER}, name = #{name,jdbcType=VARCHAR}, subtitle = #{subtitle,jdbcType=VARCHAR}, main_image = #{mainImage,jdbcType=VARCHAR}, sub_images = #{subImages,jdbcType=VARCHAR}, detail = #{detail,jdbcType=VARCHAR}, price = #{price,jdbcType=DECIMAL}, stock = #{stock,jdbcType=INTEGER}, status = #{status,jdbcType=INTEGER}, create_time = #{createTime,jdbcType=TIMESTAMP}, update_time = now() where id = #{id,jdbcType=INTEGER} </update> <select id="selectList" resultMap="BaseResultMap" > SELECT <include refid="Base_Column_List"/> from mmall_product ORDER BY id asc </select> <select id="selectByNameAndProductId" resultMap="BaseResultMap" parameterType="map"> SELECT <include refid="Base_Column_List"/> from mmall_product <where> <if test="productName != null"> and name like #{productName} </if> <if test="productId != null"> and id = #{productId} </if> </where> </select> <select id="selectByNameAndCategoryIds" resultMap="BaseResultMap" parameterType="map"> SELECT <include refid="Base_Column_List"></include> from mmall_product where status = 1 <if test="productName != null"> and name like #{productName} </if> <if test="categoryIdList != null" > and category_id in <foreach item="item" index="index" open="(" separator="," close=")" collection="categoryIdList"> #{item} </foreach> </if> </select> </mapper>
FTPUtil:
package com.mmall.util; import org.apache.commons.net.ftp.FTPClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.List; /** * Created by geely */ public class FTPUtil { private static final Logger logger = LoggerFactory.getLogger(FTPUtil.class); private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip"); private static String ftpUser = PropertiesUtil.getProperty("ftp.user"); private static String ftpPass = PropertiesUtil.getProperty("ftp.pass"); public FTPUtil(String ip,int port,String user,String pwd){ this.ip = ip; this.port = port; this.user = user; this.pwd = pwd; } public static boolean uploadFile(List<File> fileList) throws IOException { FTPUtil ftpUtil = new FTPUtil(ftpIp,21,ftpUser,ftpPass); logger.info("开始连接ftp服务器"); boolean result = ftpUtil.uploadFile("img",fileList); logger.info("开始连接ftp服务器,结束上传,上传结果:{}"); return result; } private boolean uploadFile(String remotePath,List<File> fileList) throws IOException { boolean uploaded = true; FileInputStream fis = null; //连接FTP服务器 if(connectServer(this.ip,this.port,this.user,this.pwd)){ try { ftpClient.changeWorkingDirectory(remotePath); ftpClient.setBufferSize(1024); ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); for(File fileItem : fileList){ fis = new FileInputStream(fileItem); ftpClient.storeFile(fileItem.getName(),fis); } } catch (IOException e) { logger.error("上传文件异常",e); uploaded = false; e.printStackTrace(); } finally { fis.close(); ftpClient.disconnect(); } } return uploaded; } private boolean connectServer(String ip,int port,String user,String pwd){ boolean isSuccess = false; ftpClient = new FTPClient(); try { ftpClient.connect(ip); isSuccess = ftpClient.login(user,pwd); } catch (IOException e) { logger.error("连接FTP服务器异常",e); } return isSuccess; } private String ip; private int port; private String user; private String pwd; private FTPClient ftpClient; public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public FTPClient getFtpClient() { return ftpClient; } public void setFtpClient(FTPClient ftpClient) { this.ftpClient = ftpClient; } }
IProductService:
package com.mmall.service; import com.github.pagehelper.PageInfo; import com.mmall.common.ServerResponse; import com.mmall.pojo.Product; import com.mmall.vo.ProductDetailVo; /** * Created by geely */ public interface IProductService { ServerResponse saveOrUpdateProduct(Product product); ServerResponse<String> setSaleStatus(Integer productId, Integer status); ServerResponse<ProductDetailVo> manageProductDetail(Integer productId); ServerResponse<PageInfo> getProductList(int pageNum, int pageSize); ServerResponse<PageInfo> searchProduct(String productName, Integer productId, int pageNum, int pageSize); ServerResponse<ProductDetailVo> getProductDetail(Integer productId); ServerResponse<PageInfo> getProductByKeywordCategory(String keyword, Integer categoryId, int pageNum, int pageSize, String orderBy); }
ProductManageController:
package com.mmall.controller.backend; import com.google.common.collect.Maps; import com.mmall.common.Const; import com.mmall.common.ResponseCode; import com.mmall.common.ServerResponse; import com.mmall.pojo.Product; import com.mmall.pojo.User; import com.mmall.service.IFileService; import com.mmall.service.IProductService; import com.mmall.service.IUserService; import com.mmall.util.PropertiesUtil; import org.apache.commons.lang3.StringUtils; 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 org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.util.Map; /** * Created by geely */ @Controller @RequestMapping("/manage/product") public class ProductManageController { @Autowired private IUserService iUserService; @Autowired private IProductService iProductService; @Autowired private IFileService iFileService; @RequestMapping("save.do") @ResponseBody public ServerResponse productSave(HttpSession session, Product product){ 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.saveOrUpdateProduct(product); }else{ return ServerResponse.createByErrorMessage("无权限操作"); } } @RequestMapping("set_sale_status.do") @ResponseBody public ServerResponse setSaleStatus(HttpSession session, Integer productId,Integer status){ 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.setSaleStatus(productId,status); }else{ return ServerResponse.createByErrorMessage("无权限操作"); } } @RequestMapping("detail.do") @ResponseBody public ServerResponse getDetail(HttpSession session, Integer productId){ 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.manageProductDetail(productId); }else{ return ServerResponse.createByErrorMessage("无权限操作"); } } @RequestMapping("list.do") @ResponseBody 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("无权限操作"); } } @RequestMapping("search.do") @ResponseBody 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.searchProduct(productName,productId,pageNum,pageSize); }else{ return ServerResponse.createByErrorMessage("无权限操作"); } } @RequestMapping("upload.do") @ResponseBody public ServerResponse upload(HttpSession session,@RequestParam(value = "upload_file",required = false) MultipartFile file,HttpServletRequest request){ User user = (User)session.getAttribute(Const.CURRENT_USER); if(user == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录管理员"); } if(iUserService.checkAdminRole(user).isSuccess()){ String path = request.getSession().getServletContext().getRealPath("upload"); String targetFileName = iFileService.upload(file,path); String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName; Map fileMap = Maps.newHashMap(); fileMap.put("uri",targetFileName); fileMap.put("url",url); return ServerResponse.createBySuccess(fileMap); }else{ return ServerResponse.createByErrorMessage("无权限操作"); } } @RequestMapping("richtext_img_upload.do") @ResponseBody public Map richtextImgUpload(HttpSession session, @RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response){ Map resultMap = Maps.newHashMap(); User user = (User)session.getAttribute(Const.CURRENT_USER); if(user == null){ resultMap.put("success",false); resultMap.put("msg","请登录管理员"); return resultMap; } //富文本中对于返回值有自己的要求,我们使用是simditor所以按照simditor的要求进行返回 // { // "success": true/false, // "msg": "error message", # optional // "file_path": "[real file path]" // } if(iUserService.checkAdminRole(user).isSuccess()){ String path = request.getSession().getServletContext().getRealPath("upload"); String targetFileName = iFileService.upload(file,path); if(StringUtils.isBlank(targetFileName)){ resultMap.put("success",false); resultMap.put("msg","上传失败"); return resultMap; } String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName; resultMap.put("success",true); resultMap.put("msg","上传成功"); resultMap.put("file_path",url); response.addHeader("Access-Control-Allow-Headers","X-File-Name"); return resultMap; }else{ resultMap.put("success",false); resultMap.put("msg","无权限操作"); return resultMap; } } }
ProductListVo:
package com.mmall.vo; import java.math.BigDecimal; /** * Created by geely */ 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; } }