Mapper文件常用标签

1.sql语句标签    <sql></sql><include></include>
<sql id="someFied">
   name,password,age,sex,phone
</sql>

<select id="selectData">
   select 
   <include refid="someFied">
   from  xxx 
   where id=#{id}
</select>


2.if标签  if判断
<select id="selectData" resultType="Product">
   select * from product
   <if test="name!=null">
       where name like concat('%',#{name},'%')
   </if>
</select>


3.<when> <otherwise>标签
Mybatis里面没有else标签,但是可以使用when otherwise标签来达到这样的效果
<select id="listProduct" resultType="Product">
              
   SELECT * FROM product_
             
   <where>
                
      <choose>
                  
         <when test="name != null">
and name like concat('%',#{name},'%'</when>
         <when test="price !=null and price != 0">
 and price > #{price}</when>           <otherwise>
and id >1</otherwise>
                
      </choose>
             
   </where>
        
</select>

4.where标签  where标签里面成立就执行,如果不成立就可以忽略wherer标签
<select id="selectData" resultType="Product">
   select * from product
   <where>
       <if test="name!=null">
           and name like concat('%',#{name},'%')
       </if>
       <if test="price!=null and price!=0">
           and price>#{price}
       </if>
   </where>
</select>

5.set标签  与where标签类似,成立时执行,不成立时可以忽略
         update的sql语句中会碰到多个字段,可以使用set标签
<update id="updataData" parameterType="Product">
   update prduct
   <set>
       <if test="name!=null">name=#{name},</if>
       <if test="price!=null">price=#{price}</if>
   </set>
   where id=#{id}
</update>

6.foreach标签,通常用于in这样的语法里
接收一个List集合时使用foreach标签循环遍历
<select if ="selectData" resultType="Product">
   select * from produce
   where id in
      <foreach item="item" index="index" collection="list">
           open="(" separator="," close=")">
           #{item}
       </foreach>
</select>

7.bind标签,就像是再做一次字符串拼接,网上也有说绑定,差不多意思
<select id="listProduct" resultType="Product">
            
    <bind name="likename" value="'%' + name + '%'" />
            
     select * from   product_  where name like #{likename}
      
</select>


 

猜你喜欢

转载自blog.csdn.net/zZoOoOJaVa/article/details/85318399