6-5 店铺列表展示之Dao层的实现、分页及@param参数的使用

目录

 

1、编写ShopDao接口

2、编写mapper实现

补充:Mybatis之@param修饰参数

1 由一个问题引入

1.1 问题描述

1.2 问题分析

1.3 举例【单个参数时建议不写@Param】

2 @Param解析

2.1 引入目的

2.2 举例

3 @Param和parameterType关系

3、测试


1、编写ShopDao接口

public interface ShopDao {
	/**
	 * 分页查询店铺,可输入条件:店铺名(模糊)、店铺状态、店铺类别、区域ID、owner
	 * 有多个@Param的原因:这个方法参数有多个,必须要指定param的唯一标识
	 */
	/**
	 * 
	 * @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);
}

2、编写mapper实现

<select id="queryShopList" resultMap="shopMap">
	<!-- 这里不用parameterType是因为Dao接口有三个参数了 -->
	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.enable_status,
		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(*)
		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>

 

rowIndex和 pageSize是mybatis规定的分页规则,分别表示从第几行开始取数据、要返回多少行数据


补充:Mybatis之@param修饰参数

转自http://www.heartthinkdo.com/?p=699

本文概览:介绍了@param的引入的目的和应用举例。

1 由一个问题引入

1.1 问题描述

当在Dao对象中定义了如下接口

1

int insert(HourRoomEntity hourRoomEntity);

对应的mapper.xml的设置如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<insert id="insert" parameterType="com.qunar.scm.ct.model.hourRoom.HourRoomEntity" useGeneratedKeys="true" keyProperty="id">

        insert into hour_room(

        version,

        pkg_serial,

        attribute,

        create_time

        )

        values(

        #{version},

        #{pkgSerial},

        #{attribute},

        now()

        )

</insert>

上述没有任何问题,但是当在DAO中加入这个@Param时,如DAO

1

int insert(@Param("hourRoomEntity") HourRoomEntity hourRoomEntity);

此时就会报错

Error rg.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘version’ not found. Available parameters are param1, hourRoomEntity

1.2 问题分析

1. 当使用@Param时,就会将@Parm的所有参数装到一个map中,必须写成如下格式

1

2

3

#{hourRoomEntity.version},

#{hourRoomEntity.pkgSerial},

#{hourRoomEntity.attribute},

所以,对于使用@Param修饰一个对象类型,需要使用如下格式

1

#{对象.成员}

2. 当不使用@Param时,就当成一个对象来处理,只需要写成

1

2

3

#{version},

#{pkgSerial},

#{attribute},

1.3 举例【单个参数时建议不写@Param】

1. 对于dao中定义如下两个函数:有@Parm和无@Param两种情况:

1

2

int insert(MerchantAdditionalEntity entity);

int updateById(@Param("entity") MerchantAdditionalEntity entity);

2. 对应mapper.xml

(1)有@Param

1

2

3

4

5

6

7

8

9

10

11

<update id="updateById">

        UPDATE crm_mdc_additional

        SET

        <if test="entity.orgId != null">

              org_id = #{entity.orgId},

        </if>

        <if test="entity.publicTime != null">

              public_time = #{entity.publicTime},

        </if>

         ......................

</update>

(2)无@Param

1

2

3

4

5

6

7

8

9

10

11

12

<insert id="insert">

        INSERT INTO crm_mdc_additional

        (

            serial_number,

            org_id

        )

        VALUES

        (

            #{serialNumber},

            #{orgId}

        )

    </insert>

2 @Param解析

2.1 引入目的

引入的目的就是为了在Dao接口中支持多个参数。

2.2 举例

1. 问题描述

当接口中的参数有多个时,如

1

void update(type1 param1,type2 param2);

此时应该写为

1

void update(@Param("param1")type1 param1,@Param("param2")type2 param2);

2. 分析如下:

如下内容主要意思就是可以通过@Param来实现多参数,这正是@Param的引入的意义

You can pass multiple parameters to a mapper method. If you do, they will be named by their position in the parameter list by default, for example: #{1}, #{2} etc. If you wish to change the name of the parameters (multiple only), then you can use the @Param(“paramName”) annotation on the parameter.

3. 实例如下

1

2

3

4

5

public interface CustomItemMapper

    Integer insert(@Param("item")CustomItem item,

                   @Param("extra") String someparam);

对应的mapper

1

2

3

4

5

6

7

<insert id="insert" useGeneratedKeys="false" parameterType="map" keyProperty="id">

    insert into CustomItem

(id, column2, column3, column4, column5, column6)

        values

(#{item.id}, #{item.field1}, #{item.field2}, #{item.field3}, #{item.field4}, #{extra})

  </insert>

3 @Param和parameterType关系

二者的关系如下

  • 如果使用了@Param,就不需要在mapper.xml中设置parameterType属性了
  • 如果不使用@Param,就需要在mapper.xml中设置parameterType属性了。

所以进行如下总结

  • 如果在dao接口代码中,函数中参数只有一个,那么此时就在mapper.xml中使用parameterType,不使用@param了;
  • 如果在dao接口中代码中,函数中参数有多个参数时,此时就使用@param,在mapper.xml中不再需要parameterType了。

3、测试

public class ShopDaoTest extends BaseTest{
	@Autowired
	private ShopDao shopDao;
	@Test
	public void testQueryShopListAndCount() {
		Shop shopCondition = new Shop();
		PersonInfo owner = new PersonInfo();
		owner.setUserId(1L);
		shopCondition.setOwner(owner);
		List<Shop> shopList = shopDao.queryShopList(shopCondition, 0, 3);//从第0条数据开始查找3行
		int count = shopDao.queryShopCount(shopCondition);
		System.out.println("店铺列表的大小:"+shopList.size());
		System.out.println("店铺总数:"+count);
		
		ShopCategory sc = new ShopCategory();
		sc.setShopCategoryId(2L);
		shopCondition.setShopCategory(sc);
		shopList = shopDao.queryShopList(shopCondition, 0, 3);//从第0条数据开始查找3行
		count = shopDao.queryShopCount(shopCondition);
		System.out.println("xin店铺列表的大小:"+shopList.size());
		System.out.println("xin店铺总数:"+count);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_40703303/article/details/89528257