详解MyBatis 常用写法

作者:ZT1994 时间:2022-07-14 19:36:14 

什么是 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后面表示以什么结束,如以‘)'结束。


//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 模糊查询


//模糊查询使用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 标签判断参数是否有效来进行条件查询。


<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 的效果。


<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。


<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

标签:mybatis,常用,写法
0
投稿

猜你喜欢

  • MyBatis全局配置文件详解

    2021-09-19 10:13:32
  • SpringBoot注册Filter的两种实现方式

    2023-01-01 10:26:05
  • Java求最小生成树的两种算法详解

    2023-11-10 07:21:24
  • IDEA远程管理docker镜像及容器服务的实现

    2022-01-07 16:26:48
  • 获取Java线程转储的常用方法(推荐)

    2023-05-15 02:30:19
  • mybatis中foreach嵌套if标签方式

    2023-11-20 23:11:05
  • java实现人员信息管理系统

    2023-11-02 05:21:31
  • Java高级面试题小结

    2023-11-23 07:34:00
  • Java设计模式之模板方法模式详解

    2021-08-04 04:32:51
  • springboot调用支付宝第三方接口(沙箱环境)

    2023-11-25 06:12:08
  • 关于Mybatis-Plus Wrapper是否应该出现在Servcie类中

    2023-11-28 22:04:56
  • MyBatis配置的应用与对比jdbc的优势

    2023-08-27 07:03:47
  • WPF中ImageBrush常用方式介绍

    2022-02-26 09:45:30
  • C#使用Task实现异步方法

    2022-09-02 20:26:53
  • Java中的Kotlin 内部类原理

    2021-10-26 23:02:13
  • swing登录注册界面设计

    2023-11-24 17:27:21
  • VSCode 搭建 Arm 远程调试环境的步骤详解

    2023-06-27 08:54:36
  • C#判断ip地址是否可以ping的通

    2021-08-29 10:53:13
  • 基于SpringBoot实现用户身份验证工具

    2022-05-08 18:37:29
  • java实现多线程之定时器任务

    2021-07-10 08:56:14
  • asp之家 软件编程 m.aspxhome.com