温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis的原理和使用

发布时间:2021-06-29 14:56:38 来源:亿速云 阅读:115 作者:chen 栏目:大数据

本篇内容主要讲解“MyBatis的原理和使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MyBatis的原理和使用”吧!

依赖:jar

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.2.7</version>
</dependency>

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.2.2</version>
</dependency>

1.批量插入效率最高的一种 返回主键ID:

<insert id="insertBatch" useGeneratedKeys="true" 
keyProperty="releaseDetailsId" keyColumn="RELEASE_DETAILS_ID" 
parameterType="java.util.List" >
    INSERT INTO t_user
            (id, name, del_flag)
    VALUES
    <foreach collection ="list" item="user" separator =",">
         (#{user.id}, #{user.name}, #{user.delFlag})
    </foreach >
</insert>

特殊符号处理:

其实就是xml特殊符号,转义的方式。 

&lt; < 
&gt; > 
&lt;&gt; <> 
&amp; & 
&apos; ’ 
&quot; ”


比如: 

select (case when (UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(ur.offline_time)-5*60*1000)&gt;0 then '1' else '0' end) as offline_flag from ……

使用<![CDATA[ sql语句]]>符号进行说明,将此类符号不进行解析 。 
比如: 

<isEqual property="offline_flag" compareValue="0"> 
and <![CDATA[((UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(ur.offline_time)-5*60*1000)<=0 or u.record_id=0)]]> 
</isEqual>

如果是参数字段,可以用ibatis的语法。 

<isEqual> 相等。 
<isNotEqual> 不等。 
<isGreaterThan> 大于 
<isGreaterEqual> 大于等于 
<isLessThan> 小于 
<isLessEqual> 小于等于


比如: 
 

<isNotEmpty prepend="AND" property="username"> 
u.username like '%$username$%' 
</isNotEmpty> 
<isNotEmpty prepend="AND" property="location"> 
concat(u.country,u.province,u.city) like '%$location$%' 
</isNotEmpty> 
<isEqual property="offline_flag" compareValue="1"> 
and (UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(ur.offline_time)-5*60*1000)&gt;0 
</isEqual> 
<isEqual property="offline_flag" compareValue="0"> 
and <![CDATA[((UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(ur.offline_time)-5*60*1000)<=0 or u.record_id=0)]]> 
</isEqual> 
<!-- sort --> 
<isEqual property="sort_onlinetime" compareValue="asc"> 
order by u.online_time asc 
</isEqual> 
<isEqual property="sort_onlinetime" compareValue="desc"> 
order by u.online_time desc 
</isEqual> 
<isEqual property="sort_registtime" compareValue="asc"> 
order by u.register_time asc 
</isEqual> 
<isEqual property="sort_registtime" compareValue="desc"> 
order by u.register_time desc 
</isEqual> 
<isEqual property="sort_appversion" compareValue="asc"> 
order by u.app_version asc 
</isEqual> 
<isEqual property="sort_appversion" compareValue="desc"> 
order by u.app_version desc 
</isEqual>

到此,相信大家对“MyBatis的原理和使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI