MyBaties参数传递方式:
1. 单个参数
(1) 实体类属性名传入,如下所示:
<select id="selectUrlByConfigId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from spider_url_queue
where config_id = #{config_id,jdbcType=VARCHAR}
and state = '0'
</select>
(2)占位符传入,如下所示:
<select id="selectUrlByConfigId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from spider_url_queue
<!-- 此处是0就表示取第1个参数 -->
where config_id = #{0}
and state = '0'
</select>
2.多个参数传入
(1)使用实体类属性名传入,如下所示:
<!-- 分页查询 -->
<select id="getSpiderListByPage" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM spider_config
ORDER BY
id ASC
LIMIT #{start}, #{end}
</select>
(2)占位符传入,如下所示:
<!-- 分页查询 -->
<select id="getSpiderListByPage" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM spider_config
ORDER BY
id ASC
LIMIT #{0}, #{1}
</select>
(3)传入map参数,如下所示:
参数map为:
Map<String,Object> map = new Map<String,Object>();
map.put("userId",userId);
map.put("userName",userName);
dao接口:
UserManage selectByUserIdAndUserName(Map<String,Object> map);
mapper:
<select id="selectByUserIdAndUserName" resultMap="BaseResultMap" parameterType="java.lang.String" >
select *
from USER_MANAGE
<!-- 注意此处是$符号,不是#号-->
where USER_ID = ${userId} and USER_NAME = ${userName}
</select>
(4)list参数传入,如下所示:
dao为:
void updateUrlState(@Param("UrlList")List<SpiderURLQueue> spiderUrlQueueList, @Param("state")String state);
mapper:
<update id="updateUrlState">
update spider_url_queue
set state = #{state,jdbcType=VARCHAR}
where id in
<!-- foreach进行list集合遍历
collection:是传入的集合参数名称
item:集合中每一个元素进行迭代时的别名
index:用于表示在迭代过程中,每次迭代到的位置
open:该语句以什么开始
close:以什么结束
separator:在每次进行迭代之间以什么符号作为分隔符
-->
<foreach collection="UrlList" item="SpiderUrl" index="index"
open="(" close=")" separator=",">
#{SpiderUrl.id, jdbcType=VARCHAR}
</foreach>
</update>
3.参考文章
[1]https://blog.csdn.net/s592652578/article/details/52871884
[2]https://blog.csdn.net/aya19880214/article/details/41961235
[3]https://blog.csdn.net/s592652578/article/details/52871660