mybatis-xml常用写法

批量插入

<insert id="batchInsert" parameterType="java.util.List">
  insert into table_name (name,age,high,status,create_time,update_time)
  values
     <foreach collection="list" item="e" index="index" separator=",">
       (
        #{e.name},
        #{e.age},
        #{e.high},
        #{e.status},       
        #{e.createTime},
        #{e.updateTime}
       )
    </foreach>
</insert>

 判断String类型字段并模糊查询

<if test="name != null and name != ''">
     AND name LIKE CONCAT('%',#{name},'%')
</if>

判断数值类型字段并精确查询,不需要加!=''

<if test="port != null">
   AND port = #{port}
</if>

单个字段的多值查询

<select id="findByIds" resultMap="xxxResultMap">
    select * from table_name where id in
    <foreach collection="ids" item="id" open="(" close=")" separator=",">
       #{id}
    </foreach>
</select>

 自增主键id

<insert id="insert" keyProperty="id" useGeneratedKeys="true" parameterType="com.entity.ClassXXX">
     insert into table_name(a,b,c,d)
     values(#{a},#{b},#{c},#{d},)
</insert>
原文地址:https://www.cnblogs.com/yb38156/p/14690023.html