• 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.  
  • 相关阅读:
    寒假学习进度七
    寒假学习进度六
    寒假学习进度五
    mysql 数据库第一天
    HTML 标签&总结
    事件对象
    js的事件流的概念
    jquery 的位置信息
    小米导航 案例
    jquery 的文档操作
  • 原文地址:https://www.cnblogs.com/JimShi/p/14250538.html
Copyright © 2020-2023  润新知