实战SSM_O2O商铺_07【商铺注册】DAO层-新增与更新商铺
文章目錄
- 概述
- 增加商鋪
- ShopDao新增insertShop接口
- ShopDao.xml中新增insertShop語句
- 單元測試
- 更新商鋪
- ShopDao中新增updateShop接口
- ShopDao.xml中新增updateShop語句
- 單元測試
- Github地址
概述
我們在實戰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);@AutowiredShopDao shopDao;@Testpublic 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', 'test@artisan.com', '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>單元測試
@Testpublic 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更新成2area.setAreaId(2);// 為了防止因外鍵約束,導致更新異常,同時也能驗證更新方法沒有問題// 新增一條測試數據將shopCategoryId更新為2shopCategory.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也沒有問題。
Github地址
代碼地址: https://github.com/yangshangwei/o2o
總結
以上是生活随笔為你收集整理的实战SSM_O2O商铺_07【商铺注册】DAO层-新增与更新商铺的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战SSM_O2O商铺_06logbac
- 下一篇: 实战SSM_O2O商铺_08【商铺注册】