Recorded dynamic sql common tags:
1.where
Conditions are generally used as a data addition operation in
example:
<select id="selectByRoleId" resultMap="resource">
select * from resource <where> role_id = #{roleId} </where>
2.if
Generally used as a query, modify, or delete some of the data splicing conditions.
Analyzing the condition test field
example:
<select id="findCountByContditon" resultType="int" parameterType="com.example.demo.po.User">
select coalesce(count(id),0) from user <where> <if test="name != null and name != ''"> name like #{name} </if> <if test="createTime != null"> and create_time < #{createTime} </if> </where> </select>
The above sql there is a problem, that name if conditions are not met, creatTime condition is satisfied, then the sql error. In general, when determining if there are multiple, where 1 = 1 is added before if under conditions where.
<select id="findCountByContditon" resultType="int" parameterType="com.example.demo.po.User">
select coalesce(count(id),0) from user <where> 1=1 <if test="name != null and name != ''"> and name like #{name} </if> <if test="createTime != null"> and create_time < #{createTime} </if> </where> </select>
3.foreach
Generally used to add bulk data
field generally collection list, array, map type, the value of the parameters passed
separator delimiter field value, i.e. what divides
alias for each item field is the loop element
each subscript index field value at the first cycle
open field value prefix value
close suffix value field value
example:
<insert id="saveAll" parameterType="java.util.List">
insert into user(name,role_id,password) values <foreach collection="users" separator="," item="user" index="index" > ( #{user.name}, #{user.roleId}, #{user.password}) </foreach> </insert>
4.set
Generally used in the modified data, if often appear together with
<update id="updateByCondition">
update user
<set> <if test="name != null and name != ''"> name = #{name}, </if> <if test="password != null and password != ''"> password = #{password}, </if> <if test="createTime != null"> create_time = @{createTime} </if> </set> where id = #{id} </update>
5.include
Generally it is used as the constant field extraction
<sql id="user_columns">
id, name,role_id,password,create_time,update_time
</sql>
<select id="selectById" resultMap="user">
select <include refid="user_columns"/> from user where id = #{id} </select>
6.trim
prefix value field prefix is added
suffix added suffix value field
prefixOverrides field delete prefix value
suffixOverrides field delete suffix value
<update id="updateByCondition">
update user set
<trim suffixOverrides=","> <if test="name != null and name != ''"> name = #{name}, </if> <if test="password != null and password != ''"> password = #{password}, </if> <if test="createTime != null"> create_time = @{createTime}, </if> </trim> where id = #{id} </update>
If the password is empty, the other is not empty. sql becomes
update user set name = #{name}, create_time = #{createTime} where id = #{id }
Source: http://www.1994july.club/seo/