Mybatis动态SQL语句篇

版权声明:转载请说明去处,文章仅供学习参考 https://blog.csdn.net/qq_38487155/article/details/82866603

MyBatis常用OGNL表达式

  • e1 or e2
  • e1 and e2
  • e1 == e2,e1 eq e2
  • e1 != e2,e1 neq e2
  • e1 lt e2:小于
  • e1 lte e2:小于等于,其他gt(大于),gte(大于等于)
  • e1 in e2
  • e1 not in e2
  • e1 + e2,e1 * e2,e1/e2,e1 - e2,e1%e2
  • !e,not e:非,求反
  • e.method(args)调用对象方法
  • e.property对象属性值
  • e1[ e2 ]按索引取值,List,数组和Map
  • @class@method(args)调用类的静态方法
  • @class@field调用类的静态字段值

一、 动态拼接SQL语句:根据传入的参数值进行判断拼接出正确的SQL语句。

                                        使用OGNL表达式,类似EL表达式。使用如下标签,类似JSTL标签

                                        以下介绍的标签基本都在操作标签内(<select>、<insert>、<delete>、<update>)

1、<where>:相当于给sql语句后面自动添加一个 where 1=1
           
            
2、<if>:if语句
                  test:对属性的条件,填写OGNL表达式,

3、<trim>: 对拼接后的sql语句再进行最后的修改
                 prefix:给拼串后的整个字符串加一个前缀 
                 prefixOverrides:前缀覆盖: 去掉整个字符串前面多余的字符
                 suffix:suffix给拼串后的整个字符串加一个后缀 
                 suffixOverrides:去掉整个字符串后面多余的字符

4、<choose>:相当于带break的choose语句,只能执行第一个匹配条件的语句
             <when>:相当于switch语句
                        test:属性值的判断,ONGL表达式
             <otherwise>:相当于default语句

    <select id="getEmpByCondition" resultType="mybatis.bean.Employee">
        select* from tb1_employee
        
        <where>
	            <if test="id!=null">
	                and id=#{id}
	            </if>
	            <if test="lastName!=null">
	                and last_name=#{lastName}
	            </if>
	            <if test="email!=null">
	                and email=#{email}
	            </if>
	            <if test="gender!=null">
	                and gender=#{gender}
	            </if> 
        </where>
	    
	    
	     	<trim prefix="where" prefixOverrides="and">
	     	    <if test="id!=null">
	                and id=#{id}
	            </if>
	            <if test="lastName!=null">
	                and last_name=#{lastName}
	            </if>
	            <if test="email!=null">
	                and email=#{email}
	            </if>
	            <if test="gender!=null">
	                and gender=#{gender}
	            </if> 
	     	</trim>
	     
		 <where>
		     <choose>
		         <when test="id!=null">and id=#{id}</when>
		         <when test="lastName!=null">and last_name=#{lastName}</when>
		         <when test="email!=null"> and email=#{email}</when>
		         <otherwise>
		              and gender=#{gender}
		         </otherwise>
		     </choose>
	     </where>
    </select>

5、<set>:相当于给sql语句后面自动添加一个 set,同时去掉set条件后的逗号( , ),在更新数据表数据时使用。

    <update id="updateByCondition">
        update tb1_employee 
        <set>
            <if test="id!=null">
	            id=#{id}
            </if>
            <if test="lastName!=null">
                last_name=#{lastName}
            </if>
            <if test="email!=null">
                email="[email protected]"
            </if>
            <if test="gender!=null">
                gender=#{gender}
            </if> 
        </set>
        where email=#{email};
    </update>

6、<foreach>:遍历传入的List列表参数或Map集合参数

                 collection:指定要遍历的集合参数名

                 item:正在遍历的元素名

                 separator:每个元素之间的分隔符

                 open:在遍历出来的结果前添加前缀

                 close:在遍历出来的结果后添加后缀

                 index:索引。遍历list的时候是index就是索引,item就是当前值

                 #{元素名(即item)}:取出元素

  使用<foreach>根据传入的List拼接sql查询语句

    <select id="getEmpsByList" resultType="mybatis.bean.Employee">
         select* from tb1_employee where id in
        <foreach collection="list" item="idItem" separator="," open="(" close=")">
            #{idItem}
        </foreach>
    </select>

使用<foreach>进行批量插入的俩种方法:

第一种:MySQL支持values(...),values(...),...来插入多条数据

	<insert id="insertBatchOne">
	    insert into tb1_employee values
	    <foreach collection="list" item="employee" separator=",">
	        (
	        	#{employee.id},
	        	#{employee.lastName},
	        	#{employee.gender},
	        	#{employee.email},
	        	#{employee.dept.id}
	        )
	    </foreach>
	</insert>    

 第二种:拼写多条insert语句,需要开启在全局配置文件中修改

             database=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true

	<insert id="insertBatchTwo">
	    <foreach collection="list" item="employee" separator=";">
	        insert into tb1_employee values(#{employee.id},#{employee.lastName},
            #{employee.gender}, #{employee.email},#{employee.dept.id})
	    </foreach>
	</insert>    

7、<bind>:存储常用ONGL表达式

                 name:引用时别名

                 value :ONGL表达式

                 #{name}:使用ONGL表达式

	<select id="getByLastName" resultType="mybatis.bean.Employee">
	    <bind name="similarLastName" value="'%'+lastName+'%'"/>
	    select* from tb1_employee where last_name like #{similarLastName}
	</select>

8、<sql>:存储sql语句的数据库常用列名

                 id:引用时别名

     <include>:根据refid取出<sql>标签存储的列名

                 refid:取出refid与id相同的列名

	<sql id="insertColumn">
	    last_name,gender
	</sql>
	<insert id="insertEmployee">
	    insert into tb1_employee(
			<include refid="insertColumn">
			</include>
	    ) values
        (
        	#{lastName},
        	#{gender}
        )
	</insert>

二、内置参数

         _parameter:代表整个参数
             单个参数:_parameter就是这个参数
             多个参数:参数会被封装为一个map;_parameter就是代表这个map
         
         _databaseId:如果配置了databaseIdProvider标签。
             _databaseId就是代表当前数据库的别名

使用_parameter得要进行非空判断,即以下格式:

        <if test="_parameter!=null">
            where id = #{_parameter.id}
        </if>

猜你喜欢

转载自blog.csdn.net/qq_38487155/article/details/82866603