专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 MyBatis常用标签

MyBatis常用标签

更新时间:2021-07-06 15:58:48 来源:动力节点 浏览851次

_parameter

_parameter表示当前传入的参数,如果查询的时候传入的参数只有一个,则使用_parameter

E getById(Integer id); 
<select id="getById" parameterType="int" resultMap="BaseResultMap"> 
    SELECT * 
    FROM 
    库名.表名 
    WHERE 
    id = #{_parameter} 
  </select> 

if判断

<select id="getUsers" parameterType="int" resultType="User"> 
    SELECT id, name, phone, email FROM users WHERE 1=1 
    <if test="_parameter != null"> 
      and id > #{_parameter} 
    </if> 
  </select> 

大量重复的字段

<sql id="HHHHH"> 
    id,name 
  </sql> 

引用写法 

<include refid="HHHHH"/> 

foreach

1.item表示集合中每一个元素进行迭代时的别名

2.index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,

3.open表示该语句以什么开始,

4.separator表示在每次进行迭代之间以什么符号作为分隔符,

5.close表示以什么结束,

<update id="updateBatch"> 
    <foreach item="item" index="index" collection="list" open="" 
      close="" separator=";"> 
      <if test="item.statusType.toString() == 'DELETED'"> 
        DELETE FROM 库名.表名 WHERE id = #{item.id} 
      </if> 
      <if test="item.statusType.toString() != 'DELETED'"> 
        UPDATE 库名.表名 
        <set> 
          modifier = #{item.modifier,jdbcType=CHAR}, 
          <if test="item.account != null"> 
            account = #{item.account,jdbcType=VARCHAR}, 
          </if> 
          <if test="item.name != null"> 
            name = #{item.name,jdbcType=VARCHAR}, 
          </if> 
        </set> 
        where 
        库名.表名.id =#{item.id} 
      </if> 
    </foreach> 
  </update> 

set

一定要非空判断

set为空会报错

<update id="updateFlow"> 
    UPDATE ufis.pid 
    SET 
<strong><span style="font-size:24px;"></span></strong><pre name="code" class="html"><if test="<pre name="code" class="html"><pre name="code" class="html">used_flow != null"></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<p></p> 
<pre></pre> 
<pre></pre> 
 used_flow = used_flow + #{flow},<br> 
</if><br> 
 change_flow_time =#{changeFlowTime}WHEREid = #{id}</update> 
<pre></pre> 
<br> 
<br> 
<p></p> 
           </pre></pre> 

以上就是动力节点小编介绍的"MyBatis常用标签",希望对大家有帮助,想了解更多可查看Mybatis基础教程,如有疑问,请在线咨询,有专业老师随时为您服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>