- #和$的区别:
我们一般推荐使用的是#{},不使用${}的原因如下:
- 会引起sql注入,因为${}会直接参与sql编译
- 会影响sql语句的预编译,因为 ${ } 仅仅为一个纯碎的 string 替换,在动态 SQL 解析阶段将会进行变量替换
- sql标签和include标签:
- <select id="findList" parameterType="person" resultType="per">
select id,username,birthday from user
<!-- where标签相当 于where关键字,可以自动去除第一个and -->
<where>
<!-- 引用sql标签片段,如果sql片段和引用处不在一个mapper则需要在前边加上namespace -->
<include refid="query_person_where"></include>
<!-- 下边还有很其它的条件 -->
<!-- <include refid="其它的sql片段"></include> -->
</where>
</select> -
- foreach:
<select id="findUserByIds" parameterType="com.huihui.pojo.UserQueryIds" resultType="user">select * from user<where><if test="ids!=null and ids.size>0"><foreach collection="ids" item="id" open="and id in (" close=")" separator=",">#{id}</foreach></if></where></select>说明:collection:指定输入对象中集合属性名(也就是上面pojo中定义的属性名)item:定义每次遍历后生成的对象的对象名
open:开始遍历时要拼接的字符串
close:结束遍历时要拼接的字符串
separator:定义遍历后产生的每个对象之间的字符串
trim属性:
prefix:前缀覆盖并增加其内容
suffix:后缀覆盖并增加其内容
prefixOverrides:前缀判断的条件
suffixOverrides:后缀判断的条件
<insert id="insertSelective" parameterType="com.suning.ssmp.entity.AbortedRecord" useGeneratedKeys="true" keyProperty="id">
insert into aborted_record
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="taskId != null" >
task_id,
</if>
</trim>
...
</insert>