<trim prefix=““ suffix=““ suffixOverrides=““ prefixOverrides=““>
</trim>
prefix:在trim标签内sql语句加上前缀
suffix:在trim标签内sql语句加上后缀
suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","
prefixOverrides:指定去除多余的前缀内容
<insert id="方法名" parameterType="" useGeneratedKeys="" keyProperty="" >
insert into 表名
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="a != null" >
a ,
</if>
...........
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="a != null" >
#{a},
</if>
............
</trim>
<selectKey resultType="" order="" keyProperty="">
......
</selectKey>
</insert>
假设没有指定
suffixOverrides=","
执行的sql语句也许是这样的:
insert into user (id,name,location,) values(1,"tom","beijing",);
显然是错误的
指定之后语句就会变成
insert into user (id,name,location) values(1,"tom","beijing");
这样就将“,”去掉了
<include refid="Base_Column_List" />
<sql id="Base_Column_List" >
name,location
</sql>
<select id="方法名" resultMap="" parameterType="" >
select
<include refid="Base_Column_List" />
from 表名
where id = #{id,jdbcType=BIGINT}
</select>
Base_Column_List就是提前定义好的字段
resultMap="BaseResultMap"
<sql>标签
用来定义表中出现的所有字段,方便其他地方调用
<sql id="Base_Column_List">
id,name,location,age.......
<sql>
<include>标签
引用公用的代码,例如上面的sql标签
<select id="" resultMap="" parameterType="" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=BIGINT}
</select>
<where>标签
如果某一个条件符合,会动态的拼接sql where条件
如果第二个条件为空,会智能的去除and连接符
<select id="" parameterType="" resultType="">
select count(*) from user
<where>
<if test="name != null">
name=#{name}
</if>
<if test="age != null">
and age=#{age}
</if>
</where>
</select>
<if>标签
用来动态判断属性
<if test a != null>
a = #{a}
<if>
<foreach>标签
常用来批量操作时拼接条件,其中item表示循环体的别名,collection可以为list、Map、array
<insert id="" parameterType="">
insert into user (
name,location,age)
values
<foreach collection="list" item="conf" index="index" separator=",">
(
#{conf.name,jdbcType=VARCHAR},
#{conf.location,jdbcType=VARCHAR},
#{conf.age,jdbcType=INTEGER}
)
</foreach>
</insert>
useGeneratedKeys=""
keyProperty=""
这两个不是很理解,还在整理中