Mybatis傳多個參數查詢

一,用Mapjava

Daoapp

public interface TopicUpdateDao {

	Topic selectByPrimaryKey(Integer id);

	List<Topic> selectNow(Map<String, Object> map);

}

Mappercode

<select id="selectNow" resultMap="BaseResultMap" parameterType="java.util.Map">
		select
		<include refid="Base_Column_List" />
		from topic_update
		WHERE
		uptime = (SELECT MAX(uptime) FROM topic_update)
		ORDER BY rank
		LIMIT
		#{begin,jdbcType=INTEGER},#{size,jdbcType=INTEGER};
	</select>

 

        以前一直是用Map,忽然以爲不太方便,我寫的mapper,別人在service層調用我寫的dao時不知道須要哪些參數,就查了一下有沒有其餘方法。xml

2、用@Param註解,這方法這樣在調用dao層就知道須要哪些參數,很直觀。class

Daodate

public interface TopicUpdateDao {

	Topic selectByPrimaryKey(Integer id);

	List<Topic> selectNow(@Param("begin") Integer begin, @Param("size") Integer size);

}

Mapper.xmlList

<select id="selectNow" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from topic_update
		WHERE
		uptime = (SELECT MAX(uptime) FROM topic_update)
		ORDER BY rank
		LIMIT
		#{begin,jdbcType=INTEGER},#{size,jdbcType=INTEGER};
	</select>

3、佔位,dao層和上面同樣多個參數,mapper用#{x}表示第幾個參數,第一個是0,類推。select

<select id="selectNow" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from topic_update
		WHERE
		uptime = (SELECT MAX(uptime) FROM topic_update)
		ORDER BY rank
		LIMIT
		#{0},#{1};
	</select>

這個方法不太直觀。jdbc

相關文章
相關標籤/搜索