校园商铺项目_4.店铺注册功能模块

1.DAO层之新增店铺

首先在dao层添加ShopDao接口

public interface ShopDao {
	
	/**
	 * 新增店铺
	 * @param shop
	 * @return
	 */
	int insertShop(Shop shop);
}

在src/main/resources/mapper下添加ShopDao.xml

<?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.yorisk.o2o.dao.ShopDao">
	<insert id="insertShop" useGeneratedKeys="true"
		keyColumn="shop_id" keyProperty="shopId">
		INSERT INTO
		tb_shop(owner_id,area_id,shop_category_id,shop_name,shop_desc,shop_addr,
		phone,shop_img,priority,create_time,last_edit_time,enable_status,advice)
		VALUES
		(#{owner.userId},#{area.areaId},#{shopCategory.shopCategoryId},#{shopName},
		#{shopDesc},#{shopAddr},#{phone},#{shopImg},#{priority},#{createTime},
		#{lastEditTime},#{enableStatus},#{advice})
	</insert>
</mapper>

在src/test/java下的dao包中添加ShopDaoTest

public class ShopDaoTest extends BaseTest{
	@Autowired
	private ShopDao shopDao;
	@Test
	public void testInsertShop() {
		Shop shop = new Shop();
		PersonInfo owner = new PersonInfo();
		Area area = new Area();
		ShopCategory shopCategory = new ShopCategory();
		owner.setUserId(1L);
		area.setAreaId(2);
		shopCategory.setShopCategoryId(1L);
		shop.setOwner(owner);
		shop.setArea(area);
		shop.setShopCategory(shopCategory);
		shop.setShopName("测试店铺");
		shop.setShopDesc("test");
		shop.setShopAddr("test");
		shop.setPhone("test");
		shop.setShopImg("test");
		shop.setCreateTime(new Date());
		shop.setEnableStatus(1);
		shop.setAdvice("审核中");
		int effectedNum = shopDao.insertShop(shop);
		assertEquals(1,effectedNum);
	}
}

运行junit,会发现下图

添加成功。

2.DAO层之更新店铺

首先在dao层的ShopDao接口里添加更新店铺的方法

/**
	 * 更新店铺
	 * @param shop
	 * @return
	 */
	int updateShop(Shop shop);

在对应的mapper文件ShopDap.xml添加

<update id="updateShop" parameterType="com.yorisk.o2o.dao.ShopDao">
		update tb_shop
		<set>
			<if test="shopName!=null">shop_name=#{shopName},</if>
			<if test="shopDesc!=null">shop_desc=#{shopDesc},</if>
			<if test="shopAddr!=null">shop_addr=#{shopAddr},</if>
			<if test="phone!=null">phone=#{phone},</if>
			<if test="shopImg!=null">shop_img=#{shopImg},</if>
			<if test="priority!=null">priority=#{priority},</if>
			<if test="lastEditTime!=null">last_edit_time=#{lastEditTime},</if>
			<if test="enableStatus!=null">enable_status=#{enableStatus},</if>
			<if test="advice!=null">advice=#{advice},</if>
			<if test="area!=null">area_id=#{area.areaId},</if>
			<if test="shopCategory!=null">shop_category_id=#{shopCategory.shopCategoryId}</if>
		</set>
		where shop_id=#{shopId}
</update>

其中使用了动态更新。

在ShopDaoTest中添加测试更新的方法

@Test
	public void testUpdateShop() {
		Shop shop = new Shop();
		shop.setShopId(1L);
		shop.setShopDesc("测试描述");
		shop.setShopAddr("测试地址");
		shop.setLastEditTime(new Date());
		int effectedNum = shopDao.updateShop(shop);
		assertEquals(1,effectedNum);
	}

为了不执行前面的添加店铺方法,可以在前面加上@Ignore注解

更新成功。

3.Thumbnailator图片处理封装Util

导入依赖

<dependency>
			<groupId>net.coobird</groupId>
			<artifactId>thumbnailator</artifactId>
			<version>0.4.8</version>
		</dependency>

如果之前导过了就不需要再导了。

创建util包,创建ImageUtil

​
public class ImageUtil {
	private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
	
	public static void main(String[] args) throws IOException {
		
		Thumbnails.of(new File("E:/o2o/littleYellowbaby.jpg")).size(200, 200)
				.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + "/watermark.jpg")), 0.5f)
				.outputQuality(0.8f).toFile("E:/o2o/littleYellowbabynew.jpg");
	}
}

​

创建PathUtil类

public class PathUtil {
	
	private static String seperator = System.getProperty("file.separator");
	
	public static String getImgBasePath() {
		String os = System.getProperty("os.name");
		String basePath = "";
		if(os.toLowerCase().startsWith("win")) {
			basePath = "E:/o2o/image/";
		}else {
			basePath = "/home/xiangzai/image/";
		}
		basePath = basePath.replace("/", seperator);
		return basePath;
	}
	
	public static String getShopImagePath(long shopId) {
		String imagePath = "/upload/item/shop/"+shopId+"/";
		return imagePath.replace("/", seperator);
	}
}

运行imageUtil,可以发现在E:/o2o/下多了加了水印的图片

4.DTO之ShopExecution的实现

在DTO下创建ShopExecution,需要自己添加对应的set,get方法

public class ShopExecution {
	// 结果状态
	private int state;
	// 状态标识
	private String stateInfo;
	// 店铺数量
	private int count;
	// 操作的shop(增删改店铺的时候用到)
	private Shop shop;
	// shop列表(查询店铺列表的时候使用)
	private List<Shop> shopList;

	public ShopExecution() {

	}

	// 店铺操作失败的时候使用的构造器
	public ShopExecution(ShopStateEnum stateEnum) {
		this.state = stateEnum.getState();
		this.stateInfo = stateEnum.getStateInfo();
	}

	// 店铺操作成功的时候使用的构造器
	public ShopExecution(ShopStateEnum stateEnum, Shop shop) {
		this.state = stateEnum.getState();
		this.stateInfo = stateEnum.getStateInfo();
		this.shop = shop;
	}

	// 店铺操作成功的时候使用的构造器
	public ShopExecution(ShopStateEnum stateEnum, List<Shop> shopList) {
		this.state = stateEnum.getState();
		this.stateInfo = stateEnum.getStateInfo();
		this.shopList = shopList;
	}

在enums包下创建ShopStateEnum

public enum ShopStateEnum {
	CHECK(0, "审核中"), OFFLINE(-1, "非法店铺"), SUCCESS(1, "操作成功"), PASS(2, "通过认证"), INNER_ERROR(-1001, "内部系统错误"),
	NULL_SHOPID(-1002, "ShopId为空");

	private int state;
	private String stateInfo;

	private ShopStateEnum(int state, String stateInfo) {
		this.state = state;
		this.stateInfo = stateInfo;
	}
	
	
	public int getState() {
		return state;
	}

	public String getStateInfo() {
		return stateInfo;
	}

	/**
	 * 依据传入的state返回响应的enum值
	 * @param state
	 * @return
	 */
	public static ShopStateEnum stateOf(int state) {
		for(ShopStateEnum stateEnum : values()) {
			if(stateEnum.getState() == state) {
				return stateEnum;
			}
		}
		return null;
	}
}

由于我们不希望别人来修改我们的状态码和内容,所以对我们的构造函数进行私有化,并且不提供set方法

5.店铺注册之service层的实现

猜你喜欢

转载自blog.csdn.net/u013780676/article/details/81220304