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

1. ShopDao.java文件:

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

    /**
     * 更新店铺信息
     */
    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
        <!--动态SQL-->
        <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文件:

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 i = shopDao.insertShop(shop);
        System.out.println(i);
    }
    
    @Test
    public void testUpdateShop(){
     
        Shop shop = new Shop();
        shop.setShopId(1L);
        shop.setShopDesc("测试描述");
        shop.setShopAddr("测试地址");
        int i = shopDao.updateShop(shop);
        System.out.println(i);
    }
}

你可能感兴趣的:(SSM校园商铺项目)