我们根据实体类的不同取值,使用不同的 sql 语句来进行查询,所以需要动态的合成 sql 语句。
一、关键字标签
<trim>: 格式化标签。
<where>: 代表 sql 条件关键字 where。
<set>: 代表 sql update 更新字段关键字 set。
<!--动态Sql: trim 标签-->
<select id="dynamicSqlTrim" resultType="com.lks.domain.User">
select * from users
<trim prefix="where" suffix="order by age" prefixOverrides="and | or"
suffixOverrides=",">
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="county != null and county != ''">
AND county = #{county}
</if>
</trim>
</select>
trim 标签的属性含义如下:
标签用于格式化,它的属性:
- prefix:前缀。
- suffix:后缀。
- prefixOverrides:去掉第一个 and 或者是 or。
- suffixOverrides:去掉最后一个逗号,也可以是其他的标记。
二、条件查询
<if> 和 <choose>、<when>、<otherwise>(类似 java switch)。
<select id="findByCondition" parameterType="user" resultType="user">
select * from User
<where>
<if test="id!=0">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
<choose>
<when test="id!=0">
and id=#{id}
</when>
<when test="username!=null">
and username=#{username}
</when>
<otherwise>
and id=1
</otherwise>
</choose>
</where>
</select>
三、循环执行 sql 的拼接操作
例: SELECT * FROM USER WHERE id IN (1,2,5);
<select id="findByIds" parameterType="list" resultType="user">
select * from User
<where>
<foreach collection="list" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
foreach 标签的属性含义如下:
标签用于遍历集合,它的属性:
- collection:代表要遍历的集合元素,注意编写时不要写#{} 。
- open:代表语句的开始部分。
- close:代表结束部分。
- item:代表遍历集合的每个元素,生成的变量名。
- sperator: 代表分隔符。
四、sql 片段抽取
sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。
<!--抽取sql片段简化编写-->
<sql id="selectUser">select * from User</sql>
<select id="findById" parameterType="int" resultType="user">
<include refid="selectUser"></include> where id=#{id}
</select>
<select id="findByIds" parameterType="list" resultType="user">
<include refid="selectUser"></include>
<where>
<foreach collection="array" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
五、在注解中使用动态 sql
在接口注解中要使用动态 sql,则需要使用 <script> 标签。
@Update({"<script>",
"update Author",
" <set>",
" <if test='username != null'>username=#{username},</if>",
" <if test='password != null'>password=#{password},</if>",
" <if test='email != null'>email=#{email},</if>",
" <if test='bio != null'>bio=#{bio}</if>",
" </set>",
"where id=#{id}",
"</script>"})
void updateAuthorValues(Author author);
六、insert 插入语句返回 主键值
特别针对主键是数值,而且是主键自动增长的数据表,使用到标签 <selectKey>。
<insert id="insertUser" parameterType="com.entity.user">
insert into test (name) values (#{name})
<selectKey keyProperty="id" resultType="java.lang.Integer">
select LAST_INSERT_ID() as id
</selectKey>
</insert>
foreach 标签的属性含义如下:
标签用于设置生成主键,它的属性:
- keyProperty:selectKey 语句结果应该被设置的目标属性。
- resultType:结果的类型。
- order:这可以被设置为 before 或 after(mysql 默认),表示 selectKey 标签内的语句是否先执行。
- statementType:statement(mysql 默认)、prepared 和 callable,分别代表 StatementImpl、 PreparedStatement 和 CallableStatement 类型。
文章内容输出来源:拉勾教育Java高薪训练营;
参考其他博文:
https://blog.youkuaiyun.com/isea533/article/details/21153791
https://www.cnblogs.com/chenziyu/p/9454765.html
mybatis 官网文档:
https://mybatis.org/mybatis-3/zh/dynamic-sql.html

本文详细总结了MyBatis中的动态SQL操作,包括<trim>、<where>、<set>标签用于SQL格式化,<if>、<choose>、<when>、<otherwise>进行条件查询,<foreach>实现SQL拼接,<include>实现SQL片段复用,以及在注解中使用动态SQL和<insert>中利用<selectKey>获取主键值。
2万+

被折叠的 条评论
为什么被折叠?



