entitymanagerupdate数据未更新null

entitymanagerupdate数据未更新null,第1张

sql语句 update字段null不能用is null

update更新数据时null字段是否更新进数据库总结

情况一:不需要更新进数据库

情况二:需要更新进数据库 (推荐方法3)

sql语句 update字段null不能用is null

update字段为 null 值时,

要用 

set column = null,

而不是

set column is null

✖ UPDATE tableA set 字段a is null WHERE字段b = 条件 

例:

mysql>UPDATE t SET col3 is null WHERE col1 = "a"

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near "is null WHERE col1 = "a"" at line 1

〇 UPDATE tableA set 字段a = null WHERE字段b = 条件

例:

mysql>UPDATE t SET col3 = null WHERE col1 = "a"

Query OK, 1 row affected (0.03 sec)

update更新数据时null字段是否更新进数据库总结

情况一:不需要更新进数据库

方法1:直接用sql语句方式,需要更新哪个字段就set xxclounm 即可

方法2:使用mybatis带的方法 updateById(Entry entry),传入entry对象只设置需要更新字段即可。

解析:mybatis-plus在update时对null字段有三种处理策略分别是:

IGNORED:0 忽略

NOT_NULL:1 非 NULL,默认策略

NOT_EMPTY:2 非空

默认策略是忽略null字段,所以只需要将entry中

你是否想达到下列目的:

budgetType 不为null,更新 BUDGET_TYPE_ 字段;

budgetType 为努力时,不更新  BUDGET_TYPE_ 字段;

如果是可以如下做:

<update id="update">

    UPDATE PRO_BUDGET_F

       SET 

           <if test="budgetType != null"> BUDGET_TYPE_ = #{budgetType}</if>

           <if test="budgetType == null"> BUDGET_TYPE_ = BUDGET_TYPE_</if>

     WHERE ID_ = #{id}

</update>

即改变下思路,当budgetType 为null,不更新值(这里BUDGET_TYPE_ = BUDGET_TYPE_

只是把值重新设置一次)

注:上面的方法只是针对你Mybatis部分做出的修改,如果budgetType 为null,将执行一次无用的SQL语句,浪费系统资源,最好的办法是,到调用Mybatis时(一般在DAO)判断budgetType是否为空,为空则不执行数据库 *** 作;

Mybatis只执行SQL *** 作,Dao(或者Service)附带业务检查,判断是否需要执行

Java mysql mybatis批量更新数据库,采用以下写法即可执行,但是数据库连接必须配置:&allowMultiQueries=true

例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true

<update id="batchUpdate" parameterType="java.util.List">

<foreach collection="list" item="item" index="index" open="" close="" separator="">

update test

<set>

test=${item.test}+1

</set>

where id = ${item.id}

</foreach>

</update>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10002901.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存