文章目录
-
- 1. 新增店铺信息
-
- 1. Shop实体类
- 2. ShopDao接口
- 3. ShopDao.xml配置文件
- 4. 向数据库中插入相应信息
- 5. ShopDaoTest测试类
- 2. 更新店铺信息
-
- 1. ShopDao接口
- 2. ShopDao.xml
- 3. ShopDaoTest
1. 新增店铺信息
1. Shop实体类
@Data
public class Shop {
private Long shopId;
private String shopName;
private String shopDesc;
private String shopAddr;
private String phone;
private String shopImg;
private Integer priority;
private Date createTime;
private Date lastEditTime;
private Integer enableStatus;
private String advice;
private Area area;
private PersonInfo owner;
private ShopCategory shopCategory;
}
2. ShopDao接口
import com.imooc.o2o.entity.Shop;
/**
* dao层跟数据库进行交互
*/
public interface ShopDao {
/**
* 新增店铺
*/
int insertShop(Shop shop);
}
3. 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">
<!--nameSpace:ShopDao接口的实现类-->
<mapper namespace="com.imooc.o2o.dao.ShopDao">
<!--useGeneratedKeys="true":获取数据库的自增主键值-->
<!--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>
</mapper>
4. 向数据库中插入相应信息
由于Shop实体类和这三个实体类有关,因此需要向这三个实体类中加入一些信息:
INSERT INTO tb_area(area_id,area_name,priority,create_time,last_edit_time)
VALUES(1,'东苑',1,NULL,NULL);
INSERT INTO tb_area(area_id,area_name,priority,create_time,last_edit_time)
VALUES(2,'西苑',2,NULL,NULL);
INSERT INTO tb_person_info (NAME,profile_img,email,gender,enable_status,user_type,create_time,last_edit_time)
VALUES ('张三','zhangsan.jpg','zhangsan@163.com','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,'奶茶店','南京','tnanjing.jpg',1,NULL,NULL,NULL);
5. ShopDaoTest测试类
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;
public class ShopDaoTest extends BaseTest {
@Autowired
private ShopDao shopDao;
@Test
public void testInsertShop(){
Shop shop = new Shop();
PersonInfo personInfo = new PersonInfo();
Area area = new Area();
ShopCategory shopCategory = new ShopCategory();
//因为tb_shop表中有外键约束,因此务必确保设置的这几个id在对应的表中存在.
//如果这三个值在数据库中不存在将会报异常
personInfo.setUserId(2L);
shopCategory.setShopCategoryId(1L);
area.setAreaId(1);
shop.setOwner(personInfo);
shop.setArea(area);
shop.setShopCategory(shopCategory);
shop.setShopName("奶茶店");
shop.setShopDesc("珍珠奶茶");
shop.setShopAddr("合肥");
shop.setPhone("76889087");
shop.setShopImg("zhenzhunaicha.jpg");
shop.setPriority(2);
shop.setCreateTime(new Date());
shop.setLastEditTime(new Date());
shop.setEnableStatus(0);
shop.setAdvice("Waring");
int effectNum = shopDao.insertShop(shop);
}
}
由此可见,Dao层实现了与数据库的交互,在数据库中增加了一条店铺信息。
2. 更新店铺信息
1. ShopDao接口
在ShopDao接口中增加:
/**
* 更新店铺信息
*/
int updateShop(Shop shop);
2. ShopDao.xml
在ShopDao.xml中增加:
<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>
3. ShopDaoTest
@Test
public void testUpdateShop(){
Shop shop = new Shop();
shop.setShopId(38L);
shop.setShopDesc("测试描述4");
shop.setShopAddr("测试地4");
shop.setShopImg("test4");
int i = shopDao.updateShop(shop);
System.out.println(i);
}