使用Mybatis更新時(shí)候只更新變更部分的方法
Mybatis更新時(shí)候只更新變更部分
在更新數(shù)據(jù)庫(kù)的某條記錄的時(shí)候,通過(guò)我們只需要更新我們?cè)O(shè)置的字段就可以了,但是如果基于ORM映射更新,當(dāng)參數(shù)傳入的為一個(gè)Bean的時(shí)候,這個(gè)時(shí)候會(huì)將Bean的全部字段都更新一次。
有一個(gè)場(chǎng)景的如在登陸時(shí)候,如果用戶登陸成功以后只想更新用戶登陸的ip跟時(shí)間,對(duì)于這一類場(chǎng)景可以用mybatis的SqlProvider方法來(lái)只更新我們?cè)O(shè)置的字段,
具體可以參考以下代碼
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更新字段的使用
多個(gè)mapper方法,更新單字段
說(shuō)實(shí)話不太推薦,因?yàn)槿绻?0個(gè)字段要更新,難道寫(xiě)10個(gè)方法。
但是實(shí)際中很多人都這么寫(xiě)。
通用mapper方法,java代碼控制字段
特點(diǎn)是一個(gè)mapper方法包含所有字段,不為空的就update。
但是需要控制入?yún)ⅲ话阌?中方式:
new 一個(gè)對(duì)象然后set id和要改的字段
如果字段多比較費(fèi)勁,需要一個(gè)一個(gè)set。
查詢出對(duì)象,然后set要改的字段
這2種方式差不多,就是代碼看起來(lái)不一樣。
特別注意,定位字段不要加if
要更新的字段加if沒(méi)有什么問(wèn)題
但是定位條件不要加if,因?yàn)槿f(wàn)一忘記傳遞了,變成沒(méi)有where條件,那么條數(shù)不可控了。搞不好把全表更新了,可就萬(wàn)劫不復(fù)了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot大文件上傳、分片上傳、斷點(diǎn)續(xù)傳、秒傳的實(shí)現(xiàn)
本文主要介紹了springboot大文件上傳、分片上傳、斷點(diǎn)續(xù)傳、秒傳的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07mybatis plus saveOrUpdate實(shí)現(xiàn)有重復(fù)數(shù)據(jù)就更新,否則新增方式
這篇文章主要介紹了mybatis plus saveOrUpdate實(shí)現(xiàn)有重復(fù)數(shù)據(jù)就更新,否則新增方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java之Springcloud Gateway內(nèi)置路由案例講解
這篇文章主要介紹了Java之Springcloud Gateway內(nèi)置路由案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08