tk mybatis update各種類型使用及說明
更新時間:2026年04月24日 09:58:19 作者:自找苦吃,自得其樂
文章總結了Java中使用MyBatis進行數(shù)據(jù)更新的方法,包括根據(jù)example對象更新所有屬性或部分屬性,以及根據(jù)主鍵更新所有屬性或部分屬性的API使用和生成的SQL語句
1.updateByExample
根據(jù)給定的example更新所有屬性,包括主鍵id,所有屬性都需要有值
package com.bsx.test;
public class Test {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}Example example = new Example(Test.class);
example.createCriteria().andEqualTo("name", "test");
Test record = new Test();
record.setName("hello");
mapper.updateByExample(record, example);解析后sql:
UPDATE o2o_video_file SET id = ?,name = ? WHERE ( name = ? )
2.updateByExampleSelective
根據(jù)給定的example更新有值的屬性
Example example = new Example(Test.class);
example.createCriteria().andEqualTo("name", "test");
Test record = new Test();
record.setName("hello");
mapper.updateByExampleSelective(record, example);解析后sql:
UPDATE o2o_video_file SET name = ? WHERE ( name = ? )
3.updateByPrimaryKey
根據(jù)主鍵更新所有屬性
Test record = new Test();
record.setId(123);
record.setName("hello");
mapper.updateByPrimaryKey(record, example);解析后sql:
UPDATE o2o_video_file SET id=?, name = ? WHERE ( id = ? )
4.updateByPrimaryKeySelective
根據(jù)主鍵更新有值屬性
Test record = new Test();
record.setId(123);
record.setName("hello");
mapper.updateByPrimaryKey(record, example);解析后sql:
UPDATE o2o_video_file SET name = ? WHERE ( id = ? )
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring+SpringMVC+MyBatis深入學習及搭建(三)之MyBatis全局配置文件解析
這篇文章主要介紹了Spring+SpringMVC+MyBatis深入學習及搭建(三)之MyBatis全局配置文件解析,需要的朋友可以參考下2017-05-05
Spring?BeanFactory?與?FactoryBean?的區(qū)別詳情
這篇文章主要介紹了Spring?BeanFactory?與?FactoryBean?的區(qū)別詳情,BeanFactory?和?FactoryBean?的區(qū)別卻是一個很重要的知識點,在本文中將結合源碼進行分析講解,需要的小伙伴可以參考一下2022-05-05
詳解Spring Data JPA中Repository的接口查詢方法
repository代理有兩種方式從方法名中派生出特定存儲查詢:通過直接從方法名派生查詢和通過使用一個手動定義的查詢。本文將通過示例詳細講解Spring Data JPA中Repository的接口查詢方法,需要的可以參考一下2022-04-04
解決java.lang.IllegalStateException:Duplicate key異常問題
java.lang.IllegalStateException:Duplicatekey異常在將List轉(zhuǎn)換為Map時出現(xiàn),解決方法是使用toMap()的重載方法,如果已經(jīng)存在則不再修改,直接使用上一個數(shù)據(jù)2025-03-03

