校园商铺-6店铺编辑列表和列表功能-5店铺列表展示之Dao层的实现

创建2个方法,一个返回列表,一个返回总数

1.Dao层

新增queryShopList方法,用于查询店铺列表;queryShopCount方法,用于返回总数

package com.csj2018.o2o.dao;
import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.csj2018.o2o.entity.Shop;
public interface ShopDao {
	/**
	 * 分页查询店铺,可输入的条件有店铺名(模糊)、店铺状态、店铺类别、区域ID、owner
	 * @param shopCondition
	 * @param rowIndex
	 * @param pageSize
	 * @return
	 */
	List<Shop> queryShopList(@Param("shopCondition") Shop shopCondition,@Param("rowIndex")int rowIndex,@Param("pageSize")int pageSize);
	/**
	 * 返回queryShopList记录总数
	 * @param shopCondition
	 * @return 
	 */
	int queryShopCount(@Param("shopCondition") Shop shopCondition);
	/**
	 * 通过shop id查询店铺
	 * @param shopId
	 * @return shop
	 */
	Shop queryByShopId(long shopId);
	/**
	 * 新增店铺
	 * @param shop
	 * @return 返回影响的行数;-1插入失败
	 */
	int insertShop(Shop shop);
	/**
	 * 更新店铺信息
	 * @param shop
	 * @return 返回影响的行数
	 */
	int updateShop(Shop shop);
}

2. mapper文件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" >
 <!-- useGeneratedKeys="true",一旦数据插入成功,使用JDBC的getGeneratedKeys获取数据库自增主键值 -->
 <mapper namespace="com.csj2018.o2o.dao.ShopDao">
    <!-- 定义resultMapper,类型为Shop,id命名为shopMap -->
	<resultMap type="com.csj2018.o2o.entity.Shop" id="shopMap">
		<id column="shop_id" property="shopId" /><!-- 定义主键id,column为数据表的主键,property为对应实体类的成员变量的名字 -->
		<result column="shop_name" property="shopName" /><!-- 返回字段,column为数据表的字段,property为对应实体类的成员变量的名字 -->
		<result column="shop_desc" property="shopDesc" />
		<result column="shop_addr" property="shopAddr" />
		<result column="phone" property="phone" />
		<result column="shop_img" property="shopImg" />
		<result column="priority" property="priority" />
		<result column="create_time" property="createTime" />
		<result column="last_edit_time" property="lastEditTime" />
		<result column="enable_status" property="enableStatus" />
		<result column="advice" property="advice" />
        <!-- property:实体类的成员变量,column:通过那个字段关联 javaType:返回类型 -->
		<association property="area" column="area_id" javaType="com.csj2018.o2o.entity.Area">
			<id column="area_id" property="areaId" />
			<result column="area_name" property="areaName" />
		</association>
		<association property="shopCategory" column="shop_category_id" javaType="com.csj2018.o2o.entity.ShopCategory">
			<id column="shop_category_id" property="shopCategoryId" />
			<result column="shop_category_name" property="shopCategoryName" />
		</association>
		<association property="owner" column="user_id" javaType="com.csj2018.o2o.entity.PersonInfo">
			<id column="user_id" property="userId" />
			<result column="name" property="name" />
		</association>
	</resultMap>
        <!-- 以下为新增代码 -->
	<select id="queryShopList" resultMap="shopMap">
	select 
		s.shop_id,
		s.shop_name,
		s.shop_desc,
		s.shop_addr,
		s.phone,
		s.shop_img,
        s.priority,
        s.enable_status,
		s.create_time,
		s.last_edit_time,
		s.advice,
		a.area_id,
		a.area_name,
		sc.shop_category_id,
		sc.shop_category_name
	from 
		tb_shop s,
		tb_area a,
		tb_shop_category sc
		<where>
			<if test="shopCondition.shopCategory != null and shopCondition.shopCategory.shopCategoryId != null">
				and s.shop_category_id = #{shopCondition.shopCategory.shopCategoryId}
			</if>
			<if test="shopCondition.area != null and shopCondition.area.areaId != null">
				and s.area_id = #{shopCondition.area.areaId}
			</if>
			<if test="shopCondition.shopName != null">
				and s.shop_name like '%${shopCondition.shopName}%'
			</if>
			<if test="shopCondition.enableStatus != null">
				and s.enable_status = #{shopCondition.enableStatus}
			</if>
			<if test="shopCondition.owner != null and shopCondition.owner.userId != null">
				and s.owner_id = #{shopCondition.owner.userId}
			</if>
			and s.area_id = a.area_id and s.shop_category_id = sc.shop_category_id
		</where>
		order by s.priority desc
		limit #{rowIndex},#{pageSize}
	</select>
       
	<select id="queryShopCount" resultType="int">
		select
		count(1)
		from 
		tb_shop s,
		tb_area a,
		tb_shop_category sc
		<where>
			<if test="shopCondition.shopCategory != null and shopCondition.shopCategory.shopCategoryId != null">
				and s.shop_category_id = #{shopCondition.shopCategory.shopCategoryId}
			</if>
			<if test="shopCondition.area != null and shopCondition.area.areaId != null">
				and s.area_id = #{shopCondition.area.areaId}
			</if>
			<if test="shopCondition.shopName != null">
				and s.shop_name like '%${shopCondition.shopName}%'
			</if>
			<if test="shopCondition.enableStatus != null">
				and s.enable_status = #{shopCondition.enableStatus}
			</if>
			<if test="shopCondition.owner != null and shopCondition.owner.userId != null">
				and s.owner_id = #{shopCondition.owner.userId}
			</if>
			and s.area_id = a.area_id and s.shop_category_id = sc.shop_category_id
		</where>
	</select>
<!--以上为新增代码-->
	<select id="queryByShopId" resultMap="shopMap" parameterType="Long">
	select 
		s.shop_id,
		s.shop_name,
		s.shop_desc,
		s.shop_addr,
		s.phone,
		s.shop_img,
		s.priority,
		s.create_time,
		s.last_edit_time,
		s.advice,
		a.area_id,
		a.area_name,
		sc.shop_category_id,
		sc.shop_category_name
	from 
		tb_shop s,
		tb_area a,
		tb_shop_category sc
	where
		s.area_id = a.area_id 
		and
		s.shop_category_id = sc.shop_category_id
		and 
		s.shop_id = #{shopId}
	</select>
	<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.csj2018.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>
</mapper>

3.Dao层测试

package com.csj2018.o2o.dao;

import static org.junit.Assert.assertEquals;

import java.util.Date;
import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

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

public class ShopDaoTest extends BaseTest{
	@Autowired
	private ShopDao shopDao;
	
	@Test
//	@Ignore
	public void testQueryShopList() {
		Shop shopCondition = new Shop();
		PersonInfo owner = new PersonInfo();
		owner.setUserId(1L);
		shopCondition.setOwner(owner);
		List<Shop> shopList = shopDao.queryShopList(shopCondition, 0, 5);
		
		
		System.out.println("用户为1的第一页的数量:"+shopList.size());
		int count = shopDao.queryShopCount(shopCondition);
		System.out.println("用户为1的店铺总数:"+count);
		for(Shop shop:shopList) {
			System.out.print(shop.getShopId());
			System.out.println(shop.getEnableStatus());
		}
		//shopCategoryId设置为1
		ShopCategory shopCategory = new ShopCategory();
		shopCategory.setShopCategoryId(1L);
		shopCondition.setShopCategory(shopCategory);
		List<Shop> shopList2 = shopDao.queryShopList(shopCondition, 0, 5);
		System.out.println("用户为1且店铺类别为1时,第一页店铺:"+shopList2.size());
		int count2 = shopDao.queryShopCount(shopCondition);
		System.out.println("用户为1且店铺类别为1的店铺总数"+count2);
	}
	
	
	@Test
	@Ignore
	public void testQueryShopById() {
		long shopId=1;
		Shop shop = shopDao.queryByShopId(shopId);
		System.out.println(shop.getArea().getAreaId());
		System.out.println(shop.getArea().getAreaName());
		
	}
	@Test
	@Ignore
	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("测试de店铺");
		shop.setShopDesc("店铺描述");
		shop.setShopAddr("测试路1号");
		shop.setPhone("123456789");
		shop.setShopImg("shopimg");
		shop.setCreateTime(new Date());                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
		shop.setEnableStatus(1);
		shop.setAdvice("审核中");
		shop.setPriority(3);
		
		try{
			int effectedNum = shopDao.insertShop(shop);
			assertEquals(1,effectedNum);
			System.out.print(effectedNum);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Test
	@Ignore
	public void testUpdateShop() {
		Shop shop = new Shop();
		shop.setShopId(1L);
		shop.setShopDesc("江南皮革厂");
		shop.setShopAddr("皮革厂路1号");
		shop.setPhone("0571-3770571");
		shop.setLastEditTime(new Date());
		
		int effectedNum = shopDao.updateShop(shop);
		assertEquals(1,effectedNum);
		System.out.print(effectedNum);
	}
}
posted on 2020-01-22 20:29  singleSpace  阅读(236)  评论(0编辑  收藏  举报