
如图所示,我的junit测试是绿色的,但是我检查数据库里面,并没有插入新的test1那些数据,控制台也没有老师视频里面的东西输出。不知道我是哪里出了问题,请老师指教。
package com.imooc.o2o.service.impl;
import java.io.File;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.imooc.o2o.dao.ShopDao;
import com.imooc.o2o.dto.ImageHolder;
import com.imooc.o2o.dto.ShopExecution;
import com.imooc.o2o.entity.Shop;
import com.imooc.o2o.enums.ShopStateEnum;
import com.imooc.o2o.exceptions.ShopOperationException;
import com.imooc.o2o.service.ShopService;
import com.imooc.o2o.util.ImageUtil;
//import com.imooc.o2o.util.PageCalculator;
import com.imooc.o2o.util.PathUtil;
@Service
public class ShopServiceImpl implements ShopService {
@Autowired
private ShopDao shopDao;
@Override
@Transactional
// ImageHolder thumbnail
public ShopExecution addShop(Shop shop, File shopImg) {// throws ShopOperationException {
// 空值判断
if (shop == null) {
return new ShopExecution(ShopStateEnum.NULL_SHOP);
}
try {
// 给店铺信息赋初始值
shop.setEnableStatus(0);
shop.setCreateTime(new Date());
shop.setLastEditTime(new Date());
// 添加店铺信息
int effectedNum = shopDao.insertShop(shop);
if (effectedNum <= 0) {
throw new ShopOperationException("店铺创建失败");
//throw new RuntimeException("店铺创建失败");
} else {
// if (thumbnail.getImage() != null) {
if (shopImg != null) {
// 存储图片
try {
addShopImg(shop, shopImg);//thumbnail
} catch (Exception e) {
throw new ShopOperationException("addShopImg error:" + e.getMessage());
//throw new RuntimeException("add shop img 失败");
}
// 更新店铺的图片地址
effectedNum = shopDao.updateShop(shop);
if (effectedNum <= 0) {
throw new ShopOperationException("更新图片地址失败");
//throw new RuntimeException("更新 shop img 失败");
}
}
}
} catch (Exception e) {
throw new ShopOperationException("addShop error:" + e.getMessage());
//throw new RuntimeException("addShop error:" + e.getMessage());
}
return new ShopExecution(ShopStateEnum.CHECK, shop);
}
private void addShopImg(Shop shop, File shopImg) {// ImageHolder thumbnail
// 获取shop图片目录的相对值路径
String dest = PathUtil.getShopImagePath(shop.getShopId());
String shopImgAddr = ImageUtil.generateThumbnail(shopImg, dest);
shop.setShopImg(shopImgAddr);
}
}
下面是imageUtil:
package com.imooc.o2o.util;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
public class ImageUtil {
private static String basePath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
private static final SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
private static final Random r=new Random();
private static Logger logger=LoggerFactory.getLogger(ImageUtil.class);
/**
* 将CommonsMultipartFile转换成file
* @param cFile
* @return
*/
public static File transferCommonsMultipartFileToFile(CommonsMultipartFile cFile){
File newFile=new File(cFile.getOriginalFilename());
try {
cFile.transferTo(newFile);
} catch (IllegalStateException e) {
logger.error(e.toString());
e.printStackTrace();
} catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return newFile;
}
/**
* 处理缩略图,并返回目标新生成的图片的相对值路径
* @param thumbnail
* @param targetAddr
* @return
*/
public static String generateThumbnail(File thumbnail,String targetAddr){//CommonsMultipartFile
String realFileName=getRandomFileName();//给予随机文件名称
String extension=getFileExtension(thumbnail);//扩展名
makeDirPath(targetAddr);//创建文件夹
String relativeAddr=targetAddr+realFileName+extension;//相对路径
logger.error("currrent relativeAddr is:"+relativeAddr);
File dest=new File(PathUtil.getImgBasePath()+relativeAddr);//新生成的文件的路径
logger.debug("current complete addr is:"+PathUtil.getImgBasePath()+relativeAddr);
try {
Thumbnails.of(thumbnail).size(200, 200)
.watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/watermark.jpg")),0.25f)
.outputQuality(0.8f).toFile(dest);
}catch(IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return relativeAddr;
}
/**
* 创建目标路径锁设计到的目录
* @param targetAddr
*/
private static void makeDirPath(String targetAddr) {
String realFileParentPath=PathUtil.getImgBasePath()+targetAddr;
File dirPath=new File(realFileParentPath);
if(!dirPath.exists()) {
dirPath.mkdirs();
}
}
/**
* 获取输入文件名的扩展名
* @param cFile
* @return
*/
private static String getFileExtension(File cFile) {//CommonsMultipartFile
String originalFileName=cFile.getName();
return originalFileName.substring(originalFileName.lastIndexOf("."));
}
/**
* 生成随机文件名,当前年月日小时分钟秒钟+5位随机数
* @return
*/
private static String getRandomFileName() {
//获取随机5位数
int rannum=r.nextInt(89999)+10000;
String nowTimeStr= sDateFormat.format(new Date());
return nowTimeStr+rannum;
}
public static void main(String[] args) throws IOException {
Thumbnails.of(new File("C:/Users/cmr/Desktop/hhh/sikaha.jpg"))
.size(200, 200).watermark(Positions.BOTTOM_RIGHT,
ImageIO.read(new File(basePath+"/watermark.jpg")),0.25f)
.outputQuality(0.8f).toFile("C:/Users/cmr/Desktop/hhh/sikaha.jpg");
}
}
SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需
了解课程