public int getXXXBeanList(@Param("score")int score ,@Param("name")String name);
<selectid="getXXXBeanList"resultType="XXBean"> select * from tableName where score = #{score} and name = #{name} </select>
Map 封装多参数
1 2 3 4 5 6 7 8
map.put("score", 90); map.put("name", "hello");
public List<XXXBean> getXXXBeanList(HashMap map);
<selectid="getXXXBeanList"parameterType="java.util.Map"resultType="XXBean"> select * from tableName where id = #{score} and name = #{name} </select>
Array 集合
1 2 3 4 5 6 7 8
public List<XXXBean> getXXXBeanList(String[] ids);
<selectid="getXXXBeanList"resultType="XXBean"parameterType="java.lang.String"> select * from tableName where id in <foreachitem="item"index="index"collection="array"open="("separator=","close=")"> #{item} </foreach> </select>
List 集合
1 2 3 4 5 6 7 8 9 10 11
public List<XXXBean> getXXXBeanList(List<String> list);
<selectid="getXXXBeanList"resultType="XXBean"> select * from tableName where 1==1 <iftest="list != null and list.size() > 0" > and id in <foreachitem="item"index="index"collection="list"open="("separator=","close=")"> #{item} </foreach> </if> </select>
<updateid="operateBatch"> update student set store=#{score} where id in <foreachcollection="list"index="index"item="item"open="("separator=","close=")"> #{item.id} </foreach> </delete>
select distinct PROC_INST_ID_ from act_hi_taskinst t <where> exists(select TASK_ID_ from act_hi_identitylink where TYPE_ = 'participant' and USER_ID_ = #{username} and t.ID_=TASK_ID_) <iftest="participateTime_begin != null and participateTime_begin != ''"> and t.END_TIME_ >= STR_TO_DATE(#{participateTime_begin},'%Y-%m-%d %H:%i:%s') </if> <iftest="participateTime_end != null and participateTime_end != ''"> and STR_TO_DATE(#{participateTime_end},'%Y-%m-%d %H:%i:%s') >= t.END_TIME_ </if> </where>
注意事项
批处理 rewriteBatchedStatements=true
1 2 3 4
MySQL的JDBC连接的url中要加rewriteBatchedStatements参数,并保证5.1.13以上版本的驱动,才能实现高性能的批量插入。 MySQL JDBC驱动在默认情况下会无视executeBatch()语句,把我们期望批量执行的一组sql语句拆散,一条一条地发给MySQL数据库,批量插入实际上是单条插入,直接造成较低的性能。 只有把rewriteBatchedStatements参数置为true, 驱动才会帮你批量执行SQL 另外这个选项对INSERT/UPDATE/DELETE都有效
Mapper xml文件中 SQL 语句不能以分号结尾「;」
参数名区分大小写,一定注意不要写错
1
select * from SGP_INFORMPERSON where INFORMID = #{informid} and TOPERSON = #{toperson}
in 一个集合中为数字,可以通过 concat 拼接字符串,将数字转换为字符串
1
A.DEPTCODE in (concat('', ${house.deptcode}))
数字判断,不要加单引号
noticeperson.readflag == 1
1 2 3 4
<iftest="noticeperson.readflag != null and noticeperson.readflag != '' and noticeperson.readflag == 1"> and P.TOPERSON = #{noticeperson.toperson,jdbcType=VARCHAR} and P.READFLAG = #{noticeperson.readflag,jdbcType=VARCHAR} </if>