mybatis动态新增(insert)和修改(update)方式

 更新时间:2024年05月18日 10:15:39   作者:六六大欢  
这篇文章主要介绍了mybatis动态新增(insert)和修改(update)方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

mybatis动态新增(insert)和修改(update)

动态操作这里使用到了标签

trim标记是一个格式化的标记,主要用于拼接sql的条件语句(前缀或后缀的添加或忽略),可以完成set或者是where标记的功能。

标签的四个主要的属性:

  • prefix:前缀覆盖并增加其内容
  • suffix:后缀覆盖并增加其内容
  • prefixOverrides:前缀判断的条件
  • suffixOverrides:后缀判断的条件

新增

<insert id="saveDynamicCow" useGeneratedKeys="true" keyProperty="intCowId">
        insert into cowtest
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="intPastureId != null and '' != intPastureId">
                intPastureId,
            </if>
            <if test="varCowCode != null and '' != varCowCode">
                varCowCode,
            </if>
            <if test="cSex != null and '' != cSex">
                cSex,
            </if>
            <if test="addSource != null and '' != addSource">
                addSource,
            </if>
            <if test="sireClass != null and '' != sireClass">
                sireClass,
            </if>
            <if test="dateLeave != null and '' != dateLeave">
                dateLeave,
            </if>
            <if test="intLeaveClass != null and '' != intLeaveClass">
                intLeaveClass,
            </if>
            <if test="intReason != null and '' != intReason">
                intReason,
            </if>
            <if test="intCurBar != null and '' != intCurBar">
                intCurBar,
            </if>
            <if test="intCurBarName != null and '' != intCurBarName">
                intCurBarName,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="intPastureId != null and '' != intPastureId">
                #{intPastureId},
            </if>
            <if test="varCowCode != null and '' != varCowCode">
                #{varCowCode},
            </if>
            <if test="cSex != null and '' != cSex">
                #{cSex},
            </if>
            <if test="addSource != null and '' != addSource">
                #{addSource},
            </if>
            <if test="sireClass != null and '' != sireClass">
                #{sireClass},
            </if>
            <if test="dateLeave != null and '' != dateLeave">
                #{dateLeave},
            </if>
            <if test="intLeaveClass != null and '' != intLeaveClass">
                #{intLeaveClass},
            </if>
            <if test="intReason != null and '' != intReason">
                #{intReason},
            </if>
            <if test="intCurBar != null and '' != intCurBar">
                #{intCurBar},
            </if>
            <if test="intCurBarName != null and '' != intCurBarName">
                #{intCurBarName},
            </if>
        </trim>
   </insert>

这里会忽略最后的逗号“,”

修改

<update id="updateDynamicCow">
        update cowtest
        <trim prefix="SET" suffixOverrides=",">
            <if test="dateBirthDate != null and '' != dateBirthDate">
                dateBirthDate= #{dateBirthDate},
            </if>
            <if test="decBirWeight != null and '' != decBirWeight">
                decBirWeight= #{decBirWeight},
            </if>
            <if test="decQuotiety != null and '' != decQuotiety">
                decQuotiety= #{decQuotiety},
            </if>
            <if test="intCurBar != null and '' != intCurBar">
                intCurBar= #{intCurBar},
            </if>
            <if test="intCurBarName != null and '' != intCurBarName">
                intCurBarName= #{intCurBarName},
            </if>
            <if test="intCurFetal != null and '' != intCurFetal">
                intCurFetal= #{intCurFetal},
            </if>
            <if test="intBreed != null and '' != intBreed">
                intBreed= #{intBreed},
            </if>
            <if test="cSex != null and '' != cSex">
                cSex= #{cSex},
            </if>
        </trim>
        where varCowCode= #{varCowCode}
    </update>

此外

trim标签还可以在where语句中省略前缀and,当然我们也可以使用 where 1=1 后面再跟上判断语句

mybatis判断用insert还是update

在实际开发中会遇到这种情况,就是一条数据需要判断是新增还是更新,正常的开发思路是先去查询这条数据的Id是否已经存在于数据库,存在就是update,否则为insert,mybatis也是基于这样的思想实现的,下面就举个例子看一下。

具体实现

比如,前台将一条教师的信息保存到教师的实体bean中,然后需要将这条信息保存到数据库中,这时需要判断一下教师信息是要update还是insert。

教师信息实体bean如下:Teacher.java

public class Teacher {

    private int teacherId;//教师Id

    private String teacherName;//教师名

    private int count;//mybatis判断Id是否存在

    public int getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(int teacherId) {
        this.teacherId = teacherId;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

}

可以看到在实体bean中除了正常的教师信息外多了一count,它就是mybatis用来判断teacherId是否存在,如果存在就会将存在的个数保存到count中,当然一般Id都是主键,所有count也就一般都是1。

下边看一下mybatis的映射文件。

<insert id="AddTeacher" parameterType="com.mycompany.entity.Teacher">
        <selectKey keyProperty="count" resultType="int" order="BEFORE">
            select count(*) from Teacher where teacher_id = #{teacherId}
        </selectKey>
        <if test="count > 0">
            update event
            <set>
               <if test="teacherName!= null" >  
                    teacher_name= #{teacherName},
               </if>
            </set>
            <where>
                teacher_id = #{teacherId}
            </where>
        </if>
        <if test="count==0">
            insert into teacher(teacher_id,teacher_name) values (#{teacherId},#{teacherName})
        </if>
</insert>

可以看到mybatis的实现思路也是先查询Id是否存在,在根据count判断是insert还是update。

说明

1.实现原理是selectKey做第一次查询,然后根据结果进行判断,所以这里的order="BEFORE"是必须的,也是因BEFORE,所以没法通过<bind>标签来临时存储中间的值,只能在入参中增加属性来存放。

2.就上面这个例子而言,就要求实体类中包含count属性(可以是别的名字)。否则selectKey的结果没法保存,如果入参是个Map类型,就没有这个限制。

3.这种方式只是利用了selectKey会多执行一次查询来实现的,但是如果你同时还需要通过selectKey获取序列或者自增的id,就会麻烦很多(oracle麻烦,其他支持自增的还是很容易),例如我在上一篇中利用selectKey 获取主键Id。

4.建议单独查看学习一下selectKey的用法。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详解Java的MyBatis框架中的事务处理

    详解Java的MyBatis框架中的事务处理

    利用MyBatis框架的配置管理比直接使用JDBC API编写事务控制要来得更加轻松,这里我们就来详解Java的MyBatis框架中的事务处理,尤其是和Spring框架集成后更加exciting
    2016-06-06
  • 比较java中Future与FutureTask之间的关系

    比较java中Future与FutureTask之间的关系

    在本篇文章里我们给大家分享了java中Future与FutureTask之间的关系的内容,有需要的朋友们可以跟着学习下。
    2018-10-10
  • spring boot教程之建立第一个HelloWorld

    spring boot教程之建立第一个HelloWorld

    这篇文章主要介绍了spring boot教程之建立第一个HelloWorld的相关资料,需要的朋友可以参考下
    2022-08-08
  • SpringBoot对Druid配置SQL监控功能失效问题及解决方法

    SpringBoot对Druid配置SQL监控功能失效问题及解决方法

    这篇文章主要介绍了SpringBoot对Druid配置SQL监控功能失效问题的解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05
  • java实现简易局域网聊天功能

    java实现简易局域网聊天功能

    这篇文章主要为大家详细介绍了java实现简易局域网聊天功能,使用UDP模式编写一个聊天程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Java 使用openoffice进行word转换为pdf的方法步骤

    Java 使用openoffice进行word转换为pdf的方法步骤

    这篇文章主要介绍了Java 使用openoffice进行word转换为pdf的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Springboot Websocket Stomp 消息订阅推送

    Springboot Websocket Stomp 消息订阅推送

    本文主要介绍了Springboot Websocket Stomp 消息订阅推送,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-07-07
  • springboot接收前端参数的四种方式图文详解

    springboot接收前端参数的四种方式图文详解

    Spring Boot可以通过多种方式接收前端传递的数据,下面这篇文章主要给大家介绍了关于springboot接收前端参数的四种方式,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • Java FtpClient 实现文件上传服务

    Java FtpClient 实现文件上传服务

    本文主要对Java FtpClient实现简单的图片上传到服务器的方法进行介绍,并且展示的小demo中,对配置过程中主要碰到的问题:关于文件权限的问题也进行了说明,下面跟着小编一起来看下吧
    2016-12-12
  • 浅谈导入JavaWeb 项目出现的问题

    浅谈导入JavaWeb 项目出现的问题

    这篇文章主要介绍了导入JavaWeb 项目出现的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03

最新评论