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的用法。

总结

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

相关文章

  • 解决Mybatis-plus自定义TypeHandler查询映射结果一直为null问题

    解决Mybatis-plus自定义TypeHandler查询映射结果一直为null问题

    这篇文章主要介绍了解决Mybatis-plus自定义TypeHandler查询映射结果一直为null问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • Java新手启航之JDK 21版本安装开启编程之路

    Java新手启航之JDK 21版本安装开启编程之路

    Java是一门面向对象的编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,这篇文章主要介绍了Java新手启航之JDK 21版本安装开启编程之路的相关资料,需要的朋友可以参考下
    2025-12-12
  • Java中的分布式事务Seata详解

    Java中的分布式事务Seata详解

    这篇文章主要介绍了Java中的分布式事务Seata详解,Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务,Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案,需要的朋友可以参考下
    2023-08-08
  • JavaWeb实现用户登录与注册功能

    JavaWeb实现用户登录与注册功能

    这篇文章主要为大家详细介绍了JavaWeb实现用户登录与注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Mybatis反向工程出现BigDecimal类型问题及解决

    Mybatis反向工程出现BigDecimal类型问题及解决

    这篇文章主要介绍了Mybatis反向工程出现BigDecimal类型问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-09-09
  • Springboot日期转换器实现代码及示例

    Springboot日期转换器实现代码及示例

    这篇文章主要介绍了Springboot日期转换器实现代码及示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • Maven依赖管理之parent与dependencyManagement深入分析

    Maven依赖管理之parent与dependencyManagement深入分析

    首先我们来说说parent标签,其实这个不难解释,就是父的意思,pom也有继承的。比方说我现在有A,B,C,A是B,C的父级。现在就是有一个情况B,C其实有很多jar都是共同的,其实是可以放在父项目里面,这样,让B,C都继承A就方便管理了
    2022-10-10
  • java实现文件夹解压和压缩

    java实现文件夹解压和压缩

    这篇文章主要为大家详细介绍了java实现文件夹解压和压缩,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • MyBatis中的JdbcType映射使用详解

    MyBatis中的JdbcType映射使用详解

    这篇文章主要介绍了MyBatis中的JdbcType映射使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • Java中把一个文件夹下的所有文件复制到另一个文件夹的完整实现方案

    Java中把一个文件夹下的所有文件复制到另一个文件夹的完整实现方案

    这篇文章主要介绍了如何使用Java原生API实现文件夹复制,支持多级目录、空文件夹和文件覆盖等场景,并提供了基于File和FileChannel的实现方案,此外,还介绍了使用Java 7的Files工具类进行简化实现,需要的朋友可以参考下
    2026-01-01

最新评论