使用Mybatis更新時候只更新變更部分的方法
Mybatis更新時候只更新變更部分
在更新數(shù)據(jù)庫的某條記錄的時候,通過我們只需要更新我們設置的字段就可以了,但是如果基于ORM映射更新,當參數(shù)傳入的為一個Bean的時候,這個時候會將Bean的全部字段都更新一次。
有一個場景的如在登陸時候,如果用戶登陸成功以后只想更新用戶登陸的ip跟時間,對于這一類場景可以用mybatis的SqlProvider方法來只更新我們設置的字段,
具體可以參考以下代碼
Dao:
package org.**.dao;? import java.sql.Timestamp;? import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.UpdateProvider; import org.**.beans.User; import org.**.sql.provider.UserSqlProvider; import org.springframework.stereotype.Repository; ? @Repository("userDao") public interface UserDao { ? ? /** ? ? ?* login method ? ? ?* @param username ? ? ?* @param password ? ? ?* @return ? ? ?*/ ? ? @Select("select * from j_user where username=#{username} and password=#{password}") ? ? @Results({ ? ? ? ? @Result(id=true, property="userId", column="user_id", javaType=Integer.class), ? ? ? ? @Result(property="username", column="username", javaType=String.class), ? ? ? ? @Result(property="password", column="password", javaType=String.class), ? ? ? ? @Result(property="email", column="email", javaType=String.class), ? ? ? ? @Result(property="registerTime", column="register_time", javaType=Timestamp.class), ? ? ? ? @Result(property="registerIp", column="register_ip", javaType=String.class), ? ? ? ? @Result(property="lastLoginTime", column="last_login_time", javaType=Timestamp.class), ? ? ? ? @Result(property="lastLoginIp", column="last_login_ip", javaType=String.class), ? ? ? ? @Result(property="loginCount", column="login_count", javaType=Integer.class), ? ? ? ? @Result(property="errorTime", column="error_time", javaType=Timestamp.class), ? ? ? ? @Result(property="errorCount", column="error_count", javaType=Integer.class), ? ? ? ? @Result(property="errorIp", column="error_ip", javaType=String.class) ? ? }) ? ? public User getUserByUsernameAndPassword(@Param("username") String username, @Param("password") String password); ? ?? ? ? @UpdateProvider(type = UserSqlProvider.class, method = "update") ? ? public int updateLoginInfo(User user); }
SqlProvider:
package org.**.sql.provider;? import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.jdbc.SQL; import org.**.beans.User;? ? public class UserSqlProvider {?? ? ?? ?public String update(final User user) { ?? ??? ?return new SQL(){ ?? ??? ??? ?{ ?? ??? ??? ??? ?UPDATE("jo_user"); ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getUsername())) { ?? ??? ??? ??? ??? ?SET("username = #{username}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getEmail())) { ?? ??? ??? ??? ??? ?SET("email = #{email}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getPassword())) { ?? ??? ??? ??? ??? ?SET("password = #{password}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (user.getRegisterTime() != null) { ?? ??? ??? ??? ??? ?SET("register_time = #{registerTime}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getRegisterIp())) { ?? ??? ??? ??? ??? ?SET("register_ip = #{registerIp}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (user.getLastLoginTime() != null ) { ?? ??? ??? ??? ??? ?SET("last_login_time = #{lastLoginTime}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getLastLoginIp())) { ?? ??? ??? ??? ??? ?SET("last_login_ip = #{lastLoginIp}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getLoginCount().toString())) { ?? ??? ??? ??? ??? ?SET("login_count = #{loginCount}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getResetKey())) { ?? ??? ??? ??? ??? ?SET("reset_key = #{resetKey}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getResetPwd())) { ?? ??? ??? ??? ??? ?SET("reset_pwd = #{resetPwd}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (user.getErrorTime() != null) { ?? ??? ??? ??? ??? ?SET("error_time = #{errorTime}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getErrorCount().toString())) { ?? ??? ??? ??? ??? ?SET("error_count = #{errorCount}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getErrorIp())) { ?? ??? ??? ??? ??? ?SET("error_ip = #{errorIp}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?if (StringUtils.isNotBlank(user.getActivationCode())) { ?? ??? ??? ??? ??? ?SET("activation_code = #{activationCode}"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ?WHERE("user_id = #{userId}"); ?? ??? ??? ?} ?? ??? ?}.toString(); ?? ?}? }
Mybatis update更新字段的使用
多個mapper方法,更新單字段
說實話不太推薦,因為如果有10個字段要更新,難道寫10個方法。
但是實際中很多人都這么寫。
通用mapper方法,java代碼控制字段
特點是一個mapper方法包含所有字段,不為空的就update。
但是需要控制入?yún)?,一般?中方式:
new 一個對象然后set id和要改的字段
如果字段多比較費勁,需要一個一個set。
查詢出對象,然后set要改的字段
這2種方式差不多,就是代碼看起來不一樣。
特別注意,定位字段不要加if
要更新的字段加if沒有什么問題
但是定位條件不要加if,因為萬一忘記傳遞了,變成沒有where條件,那么條數(shù)不可控了。搞不好把全表更新了,可就萬劫不復了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot大文件上傳、分片上傳、斷點續(xù)傳、秒傳的實現(xiàn)
本文主要介紹了springboot大文件上傳、分片上傳、斷點續(xù)傳、秒傳的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07mybatis plus saveOrUpdate實現(xiàn)有重復數(shù)據(jù)就更新,否則新增方式
這篇文章主要介紹了mybatis plus saveOrUpdate實現(xiàn)有重復數(shù)據(jù)就更新,否則新增方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Java之Springcloud Gateway內(nèi)置路由案例講解
這篇文章主要介紹了Java之Springcloud Gateway內(nèi)置路由案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08