no.1
无论Interface Mapper里的方法有几个参数
都要加上 @Param("xxx") 这个注解
避免无法映射Mapper.XML的参数
no.2
#{} 解析为一个 JDBC 预编译语句(prepared statement)的参数标记符。
select * from user where name = #{name};
转换为
select * from user where name = ?;
可以看到#{}被解析为一个参数占位符?
${} 仅仅为一个纯碎的 string 替换,在动态 SQL 解析阶段将会进行变量替换
select * from user where name = ${name};
当我们传递参数“sprite”时,转换为
select * from user where name = "sprite";
no.3
like查询的方式
(1)'%${name}%' 不推荐
(2)CONCAT('%',CONCAT(#{name},'%')) 推荐 拼接时%必须加引号
(3)使用bind 强烈推荐
List<Person> selectBykeyWord(@Param("keyword") String keyword);
<select id="selectBykeyWord" parameterType="string" resultType="com.entity.Person">
<bind name="pattern" value="'%' + keyword + '%'" />
select * from person where name LIKE #{pattern}
</select>
no.4
mybatis foreach in 查询超过1000条解决办法
SELECT
*
FROM
${tabName} M
WHERE
M.CODE_ID IN
<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
<if test="(index % 999) == 998"> NULL) OR M.CODE_ID IN(</if>#{id}
</foreach>
explain :
M.CODE_ID IN('......','998',NULL ) OR M.CODE_ID IN('999',..... NULL) OR M.CODE_ID IN('.....')