• SSM_O2O商铺开发——02【商铺注册】 Dao层新增和更新店铺


    Dao层新增和更新店铺

    具体步骤:

      1.1  Dao层 新增ShopDao接口 

    package com.gs.o2o.dao;
    import com.gs.o2o.entity.Shop;
    public interface ShopDao {
    	//新增店铺
    	int insertShop(Shop shop);
    	//更新店铺信息
    	int updateShop(Shop shop);
    }
    

      

    1.2   ShopDao.xml中  对 ShopDao接口 中的 方法 进行实现。

    <?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">
        
     <!-- 
     namespace: 指定mapper去扫描哪个类。  
     useGeneratedKeys="true" :当数据添加成功,jdbc自动获取主键的值传入到实体类中。
     keyColumn:数据库表的主键 ,绑定 keyProperty:实体类中的主键字段。
     <if test="xxx!=null">xx_xx=#{xxx},</if>  xxx代表实体类字段,xx_xx数据库表对应的字段。
      -->
    <mapper namespace="com.gs.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>
    	<update id="updateShop" parameterType="com.gs.o2o.entity.Shop">
    		update tb_shop
    		<set>
    		<!--shop实体类只有这  shop_id, owner_id,  createTime 三个数据库字段不更新。  -->
    			<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>

      

    1.3 由于Shop实体类中

    有3个属性  和   对应的实体类  及  数据库表  相关联,我们在设计表关系的时候,设置了外键关系,因此务必确保 设置的这几个id在对应的表中存在。测试的时候先往  对应的数据库添加一些 数据。

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

      tb_area 表:        

     tb_person_info 表      

    tb_shop_category表    

    1.4  更新店铺shop实体类只有这  shop_id, owner_id,  createTime 三个数据库字段不更新。

    1.5 单元测试    ShopDaoTest

    package com.gs.o2o.dao;
    
    import static org.junit.Assert.assertEquals;
    
    import java.util.Date;
    
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.gs.o2o.BaseTest;
    import com.gs.o2o.entity.Area;
    import com.gs.o2o.entity.PersonInfo;
    import com.gs.o2o.entity.Shop;
    import com.gs.o2o.entity.ShopCategory;
    
    public class ShopDaoTest extends BaseTest {
    	@Autowired
    	private ShopDao shopDao;
    	@Test
    	public void testInsertShop() {
    		Shop shop = new Shop();
    		
    		Area area = new Area();
    		PersonInfo owner = new PersonInfo();
    		ShopCategory shopCategory = new ShopCategory();
    		area.setAreaId(1);
    		owner.setUserId(1L);
    		shopCategory.setShopCategoryId(1L);
    		
    		shop.setArea(area);
    		shop.setOwner(owner);
    		shop.setShopCategory(shopCategory);
    		shop.setShopName("测试的店铺");
    		shop.setShopDesc("test");
    		shop.setShopAddr("上海");
                 shop.setPhone("123456");
                 shop.setShopImg("/xxx/xxx");
                 shop.setPriority(99);
                   shop.setCreateTime(new Date());
                shop.setLastEditTime(new Date());
                shop.setEnableStatus(0);
                   shop.setAdvice("审核中");
            
            int effectNum = shopDao.insertShop(shop);//若添加成功,产生受影响的行数
            assertEquals(effectNum,1);
    	}
    	@Test
    	public void testUpdateShop() {
    		Shop shop = new Shop();
    		shop.setShopId(1L);
    		shop.setShopDesc("在上海开店");
            shop.setPhone("8888888");
            shop.setLastEditTime(new Date());
            int effectNum = shopDao.updateShop(shop);
            assertEquals(effectNum,1);
    	}
    }
    

      

      

      运行结果:tb_shop表新增了一条数据。

  • 相关阅读:
    Linux系统编程@文件操作(一)
    printf 格式化输出符号详细说明(转)
    SUID,SGID,Sticky Bit详解(转)
    GDB调试器
    GCC编译器
    Make和Makefile编写(详见GCC手册)
    嵌入式Linux开发——内容介绍与开发环境的搭建
    Linux驱动设计——字符设备驱动(一)
    用Socket做一个局域网聊天工具(转)
    linux下常用文件传输命令 (转)
  • 原文地址:https://www.cnblogs.com/gshao/p/10391960.html
Copyright © 2020-2023  润新知