mybatis_常用标签

时间:2022-04-24
本文章向大家介绍mybatis_常用标签,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、<where></where>标签的作用

  • 可以动态的添加where关键字
  • 可以自动去掉第一个拼接条件的and关键字
 
 
 
 1      <where> 2              <if test="username!=null and username!=''"> 3                 and username like '%${username}%' 4              </if> 5              <if test="gender!=null and gender!=''"> 6                  and gender='${gender}' 7              </if> 8          </where
 

2、<if></if>标签的作用

  • 根据传递过来的查询条件动态拼接sql语句
  • 【注意:通常在使用if标签标签判断非空时,记得一定要进行非空的判断】

3、<sql></sql>标签的作用

  • 将公共的查询条件进行封装
 1   <!-- 使用sql标签将查询条件封装,随意拼接-->
 2     <sql id="user_where">
 3         <!-- 
 4             where标签的作用:
 5             1、可以动态的添加where关键字
 6             2、可以自动去掉第一个拼接条件的and关键字
 7          -->
 8         <where>
 9             <if test="username!=null and username!=''">
10                 and username like '%${username}%'
11             </if>
12             <if test="gender!=null and gender!=''">
13                 and gender='${gender}'
14             </if>
15         </where>
16     </sql>

4、<include></include>标签的作用

  • 引入sql标签封装的公共查询条件
1   <!-- 根据条件判断是否为空,来拼接条件查询结果 -->
2     <select id="findByUsernameAndGender" parameterType="com.itheima.mybatis.pojo.User" resultType="com.itheima.mybatis.pojo.User">
3         select * from user 
4         <!-- 引入封装查询条件的SQL标签 -->
5         <include refid="user_where"></include>
6     </select>