Java--mapper.xml中常用SQL標簽


https://blog.csdn.net/MinggeQingchun/article/details/110957384

 

1、查詢語句

  1.  
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
  2.  
    selec...
  3.  
    </select>

2、插入語句

  1.  
    < insert id="insert" parameterType="pojo.OrderTable" >
  2.  
    insert into ordertable(...)
  3.  
    values(...)
  4.  
    </ insert> 

3、刪除語句

  1.  
    < delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
  2.  
    delete from ordertable  
  3.  
    where order_id =  #{orderId,jdbcType=VARCHAR}  
  4.  
    </ delete>  

4、修改語句

  1.  
    <update id= "updateByPrimaryKey" parameterType="pojo.OrderTable" >  
  2.  
      update ordertable 
  3.  
    set cid = #{cid,jdbcType=VARCHAR},  
  4.  
    address =  #{address,jdbcType=VARCHAR},  
  5.  
    create_date =  #{createDate,jdbcType=TIMESTAMP},  
  6.  
    orderitem_id =  #{orderitemId,jdbcType=VARCHAR}  
  7.  
    where order_id = #{orderId,jdbcType=VARCHAR}  
  8.  
    </update> 

需要配置的屬性

  1. id="xxxx"    表示此段SQL執行語句的唯一標識,也是接口的方法名稱【必須一致才能找到】
  2. parameterType="xxxx"    表示SQL語句中需要傳入的參數,類型要與對應的接口方法的類型一致
  3. resultMap="xxx"    定義出參,調用已定義的映射管理器的id的值
  4. resultType="xxxx"    定義出參,匹配普通Java類型或自定義的pojo       【出參類型若不指定,將為語句類型默認類型,如語句返回值為int】

if標簽

用法:

  1.  
    < select id="listProduct" resultType="Product">
  2.  
    select * from product_
  3.  
    < if test="name!=null">
  4.  
    where name like concat('%',#{name},'%')
  5.  
    </if>
  6.  
    </select>

where標簽

作用:
標簽會進行自動判斷:
如果任何條件都不成立,那么就在sql語句里就不會出現where關鍵字(重點)
如果有任何條件成立,會自動去掉多出來的 and 或者 or。(就不需要我們追加1=1之類的入侵性代碼了)
用法:

  1.  
    < select id="listProduct" resultType="Product">
  2.  
    select * from product_
  3.  
    < where>
  4.  
    < if test="name!=null">
  5.  
    and name like concat('%',#{name},'%')
  6.  
    </if>
  7.  
    <if test="price!=null and price!=0">
  8.  
    and price > #{price}
  9.  
    </ if>
  10.  
    </ where>
  11.  
    </ select>

set標簽

作用是:
與where標簽類似的,在update語句里也會碰到多個字段相關的問題。 在這種情況下,就可以使用set標簽。
其效果與where標簽類似,有數據的時候才進行設置。
用法:

  1.  
    <update id="updateProduct" parameterType="Product" >
  2.  
    update product_
  3.  
    <set>
  4.  
    <if test="name != null">name=#{name},</if>
  5.  
    <if test="price != null">price=#{price}</if>
  6.  
    </set>
  7.  
    where id=#{id}
  8.  
    </update>

trim標簽

作用是:
trim 用來定制想要的功能,比如where標簽就可以用
用法:

  1.  
    <select id="listProduct" resultType="Product">
  2.  
    select *from product_
  3.  
    <trim prefix="WHERE" prefixOverrides="AND |OR ">
  4.  
    <if test="name!=null">
  5.  
    and name like concat('%',#{name},'%')
  6.  
    </if>
  7.  
    <if test="price!=null and price!=0">
  8.  
    and price > #{price}
  9.  
    </if>
  10.  
    </trim>
  11.  
    </select>
  12.  
     
  13.  
    <update id="updateProduct" parameterType="Product" >
  14.  
    update product_
  15.  
    <trim prefix="SET" suffixOverrides=",">
  16.  
    <if test="name != null">name=#{name},</if>
  17.  
    <if test="price != null">price=#{price}</if>
  18.  
    </trim>
  19.  
    where id=#{id}
  20.  
    </update>
  • trim 用來定制想要的功能,比如where標簽就可以用
  1.  
    <trim prefix="WHERE" prefixOverrides="AND |OR ">
  2.  
    ...
  3.  
    </trim>

來替換

set標簽就可以用

  1.  
    <trim prefix="SET" suffixOverrides=",">
  2.  
    ...
  3.  
    </trim>

來替換
運行set標簽中的代碼,其效果是一樣的。

choose when otherwise 標簽

作用是:
有任何任何條件符合,就進行條件查詢,
否則就只使用id>1這個條件(即前面的標簽都不符合條件)。
也就相當於if···········else

Mybatis里面沒有else標簽,但是可以使用when otherwise標簽來達到這樣的效果。

用法:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE mapper
  3.  
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5.  
    <mapper namespace="com.myjava.pojo">
  6.  
    <select id="listProduct" resultType="Product">
  7.  
    SELECT * FROM product_
  8.  
    <where>
  9.  
    <choose>
  10.  
    <when test="name != null">
  11.  
    and name like concat('%',#{name},'%')
  12.  
    </when>
  13.  
    <when test="price !=null and price != 0">
  14.  
    and price > #{price}
  15.  
    </when>
  16.  
    <otherwise>
  17.  
    and id >1
  18.  
    </otherwise>
  19.  
    </choose>
  20.  
    </where>
  21.  
    </select>
  22.  
    </mapper>
  •  

foreach標簽

作用是:
foreach標簽通常用於in 這樣的語法里。

用法(接收一個List集合參數):

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE mapper
  3.  
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5.  
    <mapper namespace="com.myjava.pojo">
  6.  
    <select id="listProduct" resultType="Product">
  7.  
    SELECT * FROM product_
  8.  
    WHERE ID in
  9.  
    <foreach item="item" index="index" collection="list"
  10.  
    open="(" separator="," close=")">
  11.  
    #{item}
  12.  
    </foreach>
  13.  
    </select>
  14.  
    </mapper>
  •  

bind標簽

bind標簽就像是再做一次字符串拼接,網上也有說叫綁定,差不多意思,只是方便后續的使用。

用法:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE mapper
  3.  
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5.  
    <mapper namespace="com.myjava.pojo">
  6.  
     
  7.  
    <select id="listProduct" resultType="Product">
  8.  
    <bind name="likename" value="'%' + name + '%'" />
  9.  
    select * from product_ where name like #{likename}
  10.  
    </select>
  11.  
     
  12.  
    </mapper>
  •  

sql片段標簽

作用是:
通過該標簽可定義能復用的sql語句片段,在執行sql語句標簽中直接引用即可。
這樣既可以提高編碼效率,還能有效簡化代碼,提高可讀性。
用法:

    1.  
      <!--定義sql片段-->
    2.  
      <sql id="orderAndItem">
    3.  
      o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
    4.  
      </sql>
    5.  
      <select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
    6.  
      select
    7.  
      <!--引用sql片段-->
    8.  
      <include refid="orderAndItem" />
    9.  
      from ordertable o
    10.  
      join orderitem i on o.orderitem_id = i.orderitem_id
    11.  
      where o.order_id = #{orderId}
    12.  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM