mybatis动态sql

来源:互联网 发布:淘宝刷到单被骗咋追回 编辑:程序博客网 时间:2024/06/06 12:22

mybatis可以根据动态传入的条件进行sql拼接,并且可以避免一些sql异常的发生,具体如下:

1.selectKey标签:

selectKey用来执行insert时返回insert数据的主键,用法如下:

<insert id= "insertProductDetail"          parameterType="com.xy.domain.productDetailDTO">       <!-- keyProperty表示对应DTO的属性, -->       <!-- ORACLE需要先取sequence -->       <selectKey keyProperty="id" resultType= "int" order ="BEFORE">           SELECT key_sequence. nextval() from dual      </selectKey >      <!-- mysql需要插入后返回 -->      <!--      <selectKey keyProperty="id" resultType=" int" order="AFTER"">           SELECT last_insert_id() from dual      </selectKey>       -->      insert into product_detail( id, product_name)  VALUES ( #id#, #productName#)</insert>

2.if和where标签:

<resultMap type="com.xy.domain.productDetailDTO"                              id="productDetailDTO">       <id column="ID"   property= "id" />       <result column="PRODUCT_NAME" property= "productName"/>       <result column="PRODUCT_PRICE" property= "productPrice"/>       <result column="PRODUCT_TYPE" property= "productType"/>       <result column="PRODUCT_PIC" property= "productPic"/></resultMap><select id= "getProductDetailByCondition"          parameterType="com.xy.domain.productSearchDTO"          resultMap="productDetailDTO" >      select *            from product_detail t       <!--      1=1防止两个条件同时为空造成 sql错误      可更换成where标签            where 1=1      <if test="productName!=null">            and productName like '%${productName}%'      </if>      <if test="productType!=null">            and productType = ${productType}      </if>      -->       <!-- 前面条件为空,会自动剔除or或and -->       <where>             <if test="productName!=null" >                  productName like '%${productName}%'             </if>             <if test="productType!=null" >                  and productType = ${productType}             </if>              </where></select>

3.set标签:

<update id= "updateProductDetail"             parameterType="com.xy.domain.productSearchDTO" >      update productDetail             <!-- 直接使用set语句,若其中一个参数为null,则 sql会报错            set productName = ${productName},                  productType = ${productType},                  updateTime = sysdate            where                  productId = ${productId}            -->             <!--            set标签和if标签会自动剔除为null参数的条件和,             -->             <set>                   <if test="productName!=null" >                        productName = ${productName},                   </if>                   <if test="productType" >                        productType = ${productType},                   </if>                  updateTime = sysdate             </set>             where                  productId = ${productId}</update>

4.trim标签:

trim标签可以根据标签内定义的表达式,动态剔除不必要的sql,可实现where或者set标签的作用,例如:

<select id= "getProductDetailByCondition"               parameterType="com.xy.domain.productSearchDTO"               resultMap="productDetailDTO" >            select *                  from product_detail t             <!--            使用where标签            <where>                  <if test="productName!=null">                        productName like '%${productName}%'                  </if>                  <if test="productType!=null">                        and productType = ${productType}                  </if>                   </where>             -->             <!--                    使用trim标签剔除:                    1.prefix:在sql前拼接                    2.prefixOverrides:动态剔除sql内的内容                    3.             -->             <trim prefix="where" prefixOverrides= "and|or">                   <if test="productName!=null" >                        productName like '%${productName}%'                   </if>                   <if test="productType!=null" >                        and productType = ${productType}                   </if>                    </trim></select>

5.choose标签( when, otherwise ):

类似于java中的switch,choose等同于switch,when等用于case,otherwise等同于default。
当多个条件一起时,满足一个则不在添加下面的条件。
if是and的关系,choose是or的关系。
<select id= "getProductDetailByCondition"                parameterType="com.xy.domain.productSearchDTO"                resultMap="productDetailDTO" >      select *            from product_detail t       <where>             <choose>                   <when test="productName!=null" >                        t.productName = ${productName}                   </when>                   <when test="productType!=null" >                        t.productType = ${productType}                   </when>                   <otherwise>                        t.createTime = sysdate                   </otherwise>             </choose>       </where></select>

6.foreach标签:

foreach对于动态sql非常重要,可以做迭代sql拼接。如:

对于list(in):

<select id= "getProductDetailByIds" resultMap= "productDetailDTO">      select *            from productDetail t             <where>                  t.productId in                   <foreach collection="list" item="idList" open="(" separator="," close=")">                        #{idList }                   </foreach>                            </where></select>

对于array(or):

<select id= "getProductDetailByIds" resultMap= "productDetailDTO">      select *            from productDetail t             <where>                   <foreach collection="array" item="ids" open="(" separator="or" close=")">                       id = #{ids}                   </foreach>                            </where></select>

7.include标签:

include标签可以动态引入sql文件,可以将很多sql公用的东西提到一个<sql></sql>的标签里,然后通过include引入。提高了编码的效率和代码的复用性。例如:

<select id= "queryProductDetail" parameterType="com.xy.domain.productSearchDTO" resultMap="productDetailDTO" >      select *            from product_detail             <where>                   <include refid="query_common" ></include>             </where></select><sql id= "query_common">       <if test="productName!=null" >            productName like '%${productName}%'       </if>       <if test="productType!=null" >            and productType = ${productType}       </if></sql>

0 0
原创粉丝点击