欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyBatis常用标签大全

程序员文章站 2024-03-11 16:49:01
_parameter _parameter 表示当前传入的参数,如果查询的时候传入的参数只有一个,则使用 _parameter e getbyid(intege...

_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常用标签大全,希望对大家有所帮助