Dao层之新增店铺和更新店铺

这篇博客详细介绍了如何在Dao层实现店铺的新增和更新功能,包括ShopDao.java的编写,ShopDao.xml配置以及ShopDaoTest.java的测试用例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Dao层之新增店铺

  1. ShopDao.java
package com.lzx.o2o.dao;

import com.lzx.o2o.entity.Shop;

public interface ShopDao {
	/**
	 * 新增店铺信息
	 * 
	 * @param shop
	 * @return
	 */
	public int insertShop(Shop shop);
	
}
  1. 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.lzx.o2o.dao.ShopDao">
	<insert id="insertShop" useGeneratedKeys="true"
		keyColumn="shop_id" keyProperty="shopId"
		parameterType="com.lzx.o2o.entity.Shop">
		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>
  1. ShopDaoTest.java
package com.lzx.o2o.dao;

import static org.junit.Assert.assertEquals;

import java.util.Date;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.lzx.o2o.BaseTest;
import com.lzx.o2o.entity.Area;
import com.lzx.o2o.entity.PersonInfo;
import com.lzx.o2o.entity.Shop;
import com.lzx.o2o.entity.ShopCategory;

public class ShopDaoTest extends BaseTest {
	@Autowired
	private ShopDao shopDao;

	@Test
	public void testInsertShop() {
		Shop shop = new Shop();
		PersonInfo owner = new PersonInfo();
		owner.setUserId(1L);
		Area area = new Area();
		area.setAreaId(2);
		ShopCategory shopCategory = new ShopCategory();
		shopCategory.setShopCategoryId(10L);

		shop.setOwner(owner);
		shop.setArea(area);
		shop.setShopCategory(shopCategory);
		shop.setShopName("test");
		shop.setShopDesc("test");
		shop.setShopAddr("test");
		shop.setPhone("test");
		shop.setShopImg("test");
		shop.setPriority(100);
		shop.setEnableStatus(0);
		shop.setCreateTime(new Date());
		shop.setLastEditTime(new Date());
		shop.setAdvice("test");

		int affectNum = shopDao.insertShop(shop);
		assertEquals(1, affectNum);
	}

	
}

二、Dao层之更新店铺

  1. ShopDao.java
	/**
	 * 更新店铺信息
	 * 
	 * @param shop
	 * @return
	 */
	public int updateShop(Shop shop);
  1. ShopDao.xml
	<update id="updateShop" parameterType="com.lzx.o2o.dao.ShopDao">
		UPDATE tb_shop
		<set>
			<if test="area != null">area_id=#{area.areaId},</if>
			<if test="shopCategory != null">shop_category_id=#{shopCategory.shopCategoryId},</if>
			<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>
		</set>
		WHERE shop_id=#{shopId}
	</update>
  1. ShopDaoTest.java
	@Test
	public void testUpdateShop() {
		Shop shop = new Shop();
		shop.setShopId(37L);
		Area area = new Area();
		area.setAreaId(3);
		ShopCategory shopCategory = new ShopCategory();
		shopCategory.setShopCategoryId(12L);

		shop.setArea(area);
		shop.setShopCategory(shopCategory);
		shop.setShopName("test2");
		shop.setShopDesc("test2");
		shop.setShopAddr("test2");
		shop.setPhone("test2");
		shop.setShopImg("test2");
		shop.setPriority(100);
		shop.setEnableStatus(0);
		shop.setLastEditTime(new Date());
		shop.setAdvice("test2");

		int affectNum = shopDao.updateShop(shop);
		assertEquals(1, affectNum);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值