实战SSM_O2O商铺_07【商铺注册】DAO层-新增与更新商铺

概述

我们在实战SSM_O2O商铺_02数据模型设计及实体类的创建中规划了具体的模块,按照优先级从高到低的顺序,我们应该先开发 店家模块 ,而店家模块就不得不说 商铺 。 商铺是整个系统的基础,所以我们先来开发商铺管理。


增加商铺

这里写图片描述

按照/o2o/src/main/resources/spring/spring-dao.xml中 对 sqlSessionFactory 和MapperScannerConfigurer的配置,我们在对应的目录下,创建接口类和SQL映射文件


ShopDao新增insertShop接口

package com.artisan.o2o.dao;

import com.artisan.o2o.entity.Shop;

public interface ShopDao {
    /**
     * 
     * 
     * @Title: insertShop
     * 
     * @Description: 店铺注册
     * 
     * @param shop
     * 
     * @return: int 受影响的行数 1即为成功 -1(mybtis返回的值)失败
     */
    int insertShop(Shop shop);
}

ShopDao.xml中新增insertShop语句

<?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.artisan.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>   

单元测试

package com.artisan.o2o.dao;

import java.util.Date;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

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

public class ShopDaoTest extends BaseTest {

    private static final Logger logger = LoggerFactory.getLogger(ShopDaoTest.class);

    @Autowired
    ShopDao shopDao;

    @Test
    public void testQueryArea() {

        Shop shop = new Shop();
        PersonInfo personInfo = new PersonInfo();
        Area area = new Area();
        ShopCategory shopCategory = new ShopCategory();

        // 因为tb_shop表中有外键约束,因此务必确保 设置的这几个id在对应的表中存在.
        // 我们提前在tb_person_inf tb_area
        // tb_shop_category分别添加了如下id的数据,以避免插入tb_shop时抛出如下异常
        // com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
        // Cannot add or update a child row: a foreign key constraint fails
        // (`o2o`.`tb_shop`, CONSTRAINT `fk_shop_area` FOREIGN KEY (`area_id`)
        // REFERENCES `tb_area` (`area_id`))
        personInfo.setUserId(1L);
        area.setAreaId(1);
        shopCategory.setShopCategoryId(1L);

        shop.setOwner(personInfo);
        shop.setArea(area);
        shop.setShopCategory(shopCategory);
        shop.setShopName("Artisan");
        shop.setShopDesc("ArtisanDesc");
        shop.setShopAddr("NanJing");
        shop.setPhone("123456");
        shop.setShopImg("/xxx/xxx");
        shop.setPriority(99);
        shop.setCreateTime(new Date());
        shop.setLastEditTime(new Date());
        shop.setEnableStatus(0);
        shop.setAdvice("Waring");

        int effectNum = shopDao.insertShop(shop);

        Assert.assertEquals(effectNum, 1);
        logger.debug("insert  successfully");
    }
}

我们的Shop实体类中,有3个属性

/**
     * 店铺所属店主
     */
    private PersonInfo owner;
    /**
     * 店铺所在区域
     */
    private Area area;
    /**
     * 店铺类别
     */
    private ShopCategory shopCategory;

我们在设计表关系的时候,设置了外键关系,因此务必确保 设置的这几个id在对应的表中存在.

这里我们先手工插入几条数据方便单元测试。

tb_area 做SSM集成验证的时候新增了几条数据如下:

INSERT INTO `tb_area` VALUES ('1', '北京', '帝都', '0', '2018-05-13 21:00:26', '2018-05-14 21:00:33');
INSERT INTO `tb_area` VALUES ('2', '上海', '魔都', '99', '2018-05-13 21:00:36', '2018-05-14 21:00:41');

这里写图片描述


tb_person

INSERT INTO `tb_person_info` VALUES ('1', 'Artisan', 'img', '[email protected]', '1', '0', '2', '2018-05-18 02:12:43', '2018-05-18 02:12:46');

这里写图片描述


tb_shop_category

INSERT INTO `tb_shop_category` VALUES ('1', '咖啡奶茶', '咖啡奶茶desc', '/xxxx/xxxx', '0', '2018-05-18 02:13:56', '2018-05-18 02:13:58', null);

这里写图片描述

运行单元测试,运行OK。

查看数据库中对应的数据:
这里写图片描述

DAO层的新增商铺,OK,接下来哦我们来看下更新商铺


更新商铺

这里用到的MyBatis的set标签 , 用法如下

MyBatis-13MyBatis动态SQL之【where、set、trim】


ShopDao中新增updateShop接口

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

ShopDao.xml中新增updateShop语句

主要是set标签的用法,根据入参,实现动态更新

    <update id="updateShop" parameterType="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>

单元测试

@Test
    public void testUpdateShop() {
        // shop_id 不可更新 personInfo不可更新
        Shop shop = new Shop();

        Area area = new Area();
        ShopCategory shopCategory = new ShopCategory();

        // 模拟更新 shop_id=5的记录 。 因为目前数据库中只有一条shop_id=5的数据
        shop.setShopId(5L);

        // 将area_id更新成2
        area.setAreaId(2);
        // 为了防止因外键约束,导致更新异常,同时也能验证更新方法没有问题
        // 新增一条测试数据将shopCategoryId更新为2
        shopCategory.setShopCategoryId(2L);

        shop.setArea(area);
        shop.setShopCategory(shopCategory);
        shop.setShopName("ArtisanUP");
        shop.setShopDesc("ArtisanDescUP");
        shop.setShopAddr("NanJingUP");
        shop.setPhone("123456UP");
        shop.setShopImg("/xxx/xxx/UP");
        shop.setPriority(66);
        shop.setCreateTime(new Date());
        shop.setLastEditTime(new Date());
        shop.setEnableStatus(1);
        shop.setAdvice("Waring UP");

        int effectNum = shopDao.updateShop(shop);

        Assert.assertEquals(effectNum, 1);
        logger.debug("update  successfully");

    }

tb_shop_category新增数据:

INSERT INTO `tb_shop_category` VALUES ('2', '汉堡薯条', '汉堡薯条desc', '/yyyy/yyyy', '2', '2018-05-18 03:38:17', '2018-05-18 03:38:20', null);

选中该方法,右键运行单元测试,没有错误,查看数据库中的数据变化,是否符合预期。

这里写图片描述

OK, updateshop也没有问题。

猜你喜欢

转载自blog.csdn.net/yangshangwei/article/details/80364804