【校园商铺 4章】:店铺注册--Dao层的实现

持久层不用实现类,mybatis动态代理接口
1. ShopDao.java文件:

package com.imooc.o2o.dao;
import com.imooc.o2o.entity.Shop;
public interface ShopDao {
    /**1. 新增店铺*/
    //返回1,表示成功;-1,则失败。
    int insertShop(Shop shop);
    /**2.更新店铺信息*/
    int updateShop(Shop shop);
}

2. 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.imooc.o2o.dao.ShopDao">
<!--使用useGeneratedKeys获取自增主键值
        keyColumn="shop_id"数据表的主键
        keyProperty="shopId"实体类中的属性
    -->
    <!---->
    <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>
    <!--更新-->
    <update id="updateShop" parameterType="com.imooc.o2o.entity.Shop">
        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>
</mapper>

3. 数据库插入数据
因为Shop类中有这三个变量,因此需要先对数据库中的这三个表添加信息

insert into tb_person_info (name,profile_img,email,gender,enable_status,user_type,create_time,last_edit_time) 
values ('测试','test','test','1',1,2,null,null);

insert into tb_shop_category (shop_category_id,shop_category_name,shop_category_desc,shop_category_img,priority,create_time,last_edit_time,parent_id) 
values (1,'珍珠奶茶','珍珠奶茶','test',1,null,null,null);

4. ShopDaoTest.java测试

package com.imooc.o2o.dao;

import com.imooc.o2o.BaseTest;
import com.imooc.o2o.entity.Area;
import com.imooc.o2o.entity.PersonInfo;
import com.imooc.o2o.entity.Shop;
import com.imooc.o2o.entity.ShopCategory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import static org.junit.Assert.assertEquals;
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.setLastEditTime(new Date());
        shop.setEnableStatus(1);
        shop.setAdvice("审核中");
        int effectedNum=shopDao.insertShop(shop);
        assertEquals(1,effectedNum);
    }
    //更新
    @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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值