服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - 详解MyBatis 常用写法

详解MyBatis 常用写法

2021-06-15 10:24ZT1994 Java教程

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。这篇文章给大家介绍了MyBatis 常用写法,感兴趣的朋友跟随小编一起看看吧

什么是 mybatis ?

mybatis 是一款优秀的持久层框架,它支持定制化 sql、存储过程以及高级映射。mybatis 避免了几乎所有的 jdbc 代码和手动设置参数以及获取结果集。mybatis 可以使用简单的 xml 或注解来配置和映射原生信息,将接口和 java 的 pojos(plain old java objects,普通的 java对象)映射成数据库中的记录。

1、foreach 循环

  foreach 元素的属性主要有 item, idnex, collection, open, separator, close。

1.collection:传入的 list 或 array 或自己封装的 map。
2.item:集合中元素迭代时的别名。
3.idnex:集合中元素迭代是的索引。
4.open:where 后面表示以什么开始,如以‘('开始。
5.separator:表示在每次进行迭代是的分隔符。
6.close:where后面表示以什么结束,如以‘)'结束。

?
1
2
3
4
5
6
7
8
9
10
//mapper中需要传递一个容器
public list<user> querybyidlist(list<integer> useridlist);
 
<select id="querybyidlist" resultmap="baseresultmap" parametertype="map">
  select * from user
  where userid in
  <foreach collection="useridlist" item="id" index="index" open="(" close=")" separator=",">
    #{id}
  </foreach>
</select>

 

2、concat 模糊查询

?
1
2
3
4
5
6
7
8
9
//模糊查询使用concat拼接sql
<select id="querybyname" resultmap="baseresultmap" paramtertype"string">
  select * from user
  <where>
    <if test="name != null">
      name like concat('%', concat(#{name}, '%'))
    </if>
  </where>
</select>

3、if + where 标签

用 if 标签判断参数是否有效来进行条件查询。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<select id="getuserlist" resultmap="baseresultmap" paramtertype="com.demo.user">
  select * from user
  <where>
    <if test="userid !=null and userid!= ''">
      userid= #{userid}
    </if>
    <if test="name !=null and name!= ''">
      and name= #{name}
    </if>
    <if phone="userid !=null and phone!= ''">
      and phone= #{phone}
    </if>
  </where>
</select>

where 动态语句中,where 标签会自动去掉 and 或 or。防止 where and 错误。

4、if + set

使用 set 标签可以动态的配置 set 关键字,使用 if + set 标签,如果某项为 null 则不进行更新。

<update id="updateuser" paramtertype="com.demo.user">
    update user
    <set>
        <if test=" name != null and name != ''">
            name = #{name},
        </if>
        <if test=" phone != null and phone != ''">
            phone = #{phone},
        </if>
    </set>
    where userid = #{userid}
</update>

5、if + trim 代替 where/set 标签

  trim 可以更灵活的去处多余关键字的标签,可以实现 where 和 set 的效果。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<select id="getuserlist" resultmap="baseresultmap" paramtertype="com.demo.user">
  select * from user
  <trim prefix="where" prefixoverrides="and|or">
    <if test="userid !=null and userid!= ''">
      userid= #{userid}
    </if>
    <if test="name !=null and name!= ''">
      and name= #{name}
    </if>
    <if phone="userid !=null and phone!= ''">
      and phone= #{phone}
    </if>
  </trim>
</select>
 
<update id="updateuser" paramtertype="com.demo.user">
  update user
  <trim prefix="set" suffixoverrides=",">
    <if test=" name != null and name != ''">
      name = #{name},
    </if>
    <if test=" phone != null and phone != ''">
      phone = #{phone},
    </if>
  </trim>
  where userid = #{userid}
</update>

5、choose(when, otherwise)标签

  choose 标签是按顺序判断其内部 when 标签中的 test 条件是否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满足,则执行 otherwise 中的 sql。类似 java 中的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<select id="selectcustomerbycustnameandtype" parametertype="map" resultmap="baseresultmap">
  select * from user
  <choose>
    <when test="utype == 'name'">
      where name = #{name}
    </when>
    <when test="utype == 'phone'">
      where phone= #{phone}
    </when>
    <when test="utype == 'email'">
      where email= #{email}
    </when>
    <otherwise>
      where name = #{name}
    </otherwise>
  </choose>
</select>

总结

以上所述是小编给大家介绍的mybatis 常用写法 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/zt19994/archive/2018/11/22/9999696.html

延伸 · 阅读

精彩推荐