15、【 商品管理模块开发】——后台获取商品详情功能开发及PropertiesUtil配置工具,DateTimeUtil时间处理工具开发...
1、后臺獲取商品詳情接口:
在上一篇文章所新建的ProudctManageController類中新建下面方法:
*Controller:
*Service:
//查詢商品的詳細信息ServerResponse<ProductDetailVo> manageProductDetail(Integer productId);*ServiceImpl:
//查詢商品的詳細信息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);}其中用到了selectByPrimaryKey和assembleProductDetailVo這兩個方法,其中selectByPrimaryKey是使用Mybaties逆向工程生產(chǎn)的方法,根據(jù)productId來查詢Product的信息。
下面介紹下assembleProductDetailVo這個方法
這個方法是自己封裝的,用戶返回給前端一個商品詳情類,那么為什么要用到這個封裝類呢?因為我們返回給前端的不僅僅是一個數(shù)據(jù)表里面的信息,我們返回的多個表綜合的信息。所以使用的是多表查詢,我們自己封裝了一個ProductDetailVo類,來定義我們需要返回給前端定義的字段。
新建ProductDetailVo類
ProductDetailVo內(nèi)容如下:
package com.mmall.vo;import java.math.BigDecimal;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;} }assembleProductDetailVo:封裝的方法
//對返回的商品詳細信息進行封裝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);//默認根節(jié)點}else{productDetailVo.setParentCategoryId(category.getParentId());}productDetailVo.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));productDetailVo.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime()));return productDetailVo;}這一行代碼設(shè)置的是對應(yīng)的圖片服務(wù)器:下面會對這個作介紹:
productDetailVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://img.happymmall.com/"));selectByPrimaryKey方法使用的是逆向工程自動生成的方法。
2、PropertiesUtil工具的開發(fā):
1、在util包下新建PropertiesUtil類
PropertiesUtil: package com.mmall.util;import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.io.IOException; import java.io.InputStreamReader; import java.util.Properties;/*** Created by geely*/ public class PropertiesUtil {private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);private static Properties props;static {String fileName = "mmall.properties";props = new Properties();try {props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));} catch (IOException e) {logger.error("配置文件讀取異常",e);}}public static String getProperty(String key){String value = props.getProperty(key.trim());if(StringUtils.isBlank(value)){return null;}return value.trim();}public static String getProperty(String key,String defaultValue){String value = props.getProperty(key.trim());if(StringUtils.isBlank(value)){value = defaultValue;}return value.trim();}} String fileName = "mmall.properties";
這行代碼所指的就是下文中新建的mmal.properties配置文件 。
2、resouces目錄下新建mmal.properties配置文件
image.pngmmal.properties:
//對應(yīng)的ftp文件服務(wù)器的相關(guān)配置 ftp.server.ip=127.0.0.1 ftp.user=admin ftp.pass=admin ftp.server.http.prefix=http://image.imooc.com/alipay.callback.url=http://erjq8u.natappfree.cc/order/alipay_callback.dopassword.salt = geelysdafaqj23ou89ZXcj@#$@#$#@KJdjklj;D../dSF.,3、DateTimeUtil時間處理工具
1、新建DateTimeUtil工具類
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;public class DateTimeUtil {//joda-time//str->date//date->strpublic 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);}/**** @param dateTimeStr 具體時間* @param STANDARD_FORMAT 時間格式* @return*/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("2016-02-09 11:45:28","yyyy-MM-dd HH:mm:ss"));}*/ }4、接口測試
后臺獲取商品詳情接口測試
image.png總結(jié)
以上是生活随笔為你收集整理的15、【 商品管理模块开发】——后台获取商品详情功能开发及PropertiesUtil配置工具,DateTimeUtil时间处理工具开发...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 判断是否大于指定版本号
- 下一篇: Mysql-高性能索引