【6】mybatis update標簽


一、update標簽

MyBatis update 標簽用於定義更新語句,執行更新操作。當 MyBatis 執行完一條更新語句后,會返回一個整數,表示受影響的數據庫記錄的行數。

如以下xml文件中的語句

 update 標簽常用屬性

 注意:update 標簽中沒有 resultType 屬性,只有查詢操作才需要對返回結果類型進行相應的指定。

二、傳遞參數

Mybatis提供以下 3 種方式,來實現給映射器傳遞多個參數:

  • 使用 Map 傳遞參數
  • 使用注解傳遞參數
  • 使用 JavaBean 傳遞參數

2.1 使用 Map 傳遞參數

1》復制【mybatis嘗鮮】這節中的項目代碼mybatisDemoA2,生成項目mybatisDemoA5,打開net.biancheng.mapper包下的WebsiteMapper接口類,添加以下方法:

public int updateWebsiteByMap(Map<String, Object> params);

2》打開net.biancheng.mapper包下的WebsiteMapper.xml 中,添加以下配置內容,注意原來內容不要動:

<!--更新語句接收 Map 傳遞的參數-->
<update id="updateWebsiteByMap" parameterType="map">
update website set name = #{name},url= #{url} where id = #{id}
</update>

3》在net.biancheng.test包添加testMap類,代碼如下:

package net.biancheng.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import net.biancheng.mapper.WebsiteMapper;
import net.biancheng.po.Website;

public class testMap {

public static void main(String[] args) {
InputStream config;
try {
// 讀取配置文件
config = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder()
.build(config);
// 創建sqlsession對象
SqlSession ss = ssf.openSession();
// 獲取指定對象
WebsiteMapper websiteMapper = ss.getMapper(WebsiteMapper.class);
System.out.println("更新前:");
// 調用對象方法
Website websitelist0 = websiteMapper.selectAllWebsite().get(0);
System.out.println(websitelist0.show());

// 使用 Map 向 update 標簽傳遞參數
Map<String, Object> params = new HashMap<>();
params.put("id", 3);
params.put("name", "菜鳥果");
params.put("url", "www.cai.net");
int i = websiteMapper.updateWebsiteByMap(params);
if (i > 0) {
System.out.println("通過 Map 傳遞參數,共更新了 " + i + " 條記錄");
}
System.out.println("更新后:");
// 調用對象方法
Website websitelist1 = websiteMapper.selectAllWebsite().get(0);
System.out.println(websitelist1.show());
ss.commit();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}

更新前數據庫記錄如下:

 程序執行結果如下:

 更新后數據庫記錄如下:

 id為3的記錄發生了更新,執行成功.

 2.2 使用注解傳遞參數

可以使用 MyBatis 提供的 @Param 注解給注解器傳遞參數

1》打開net.biancheng.mapper包下的WebsiteMapper接口類,添加以下方法,注意原有代碼不要動:

public int updateWebsiteByParam(@Param("name") String name,
@Param("url") String url, @Param("id") Integer id);

2》在WebsiteMapper.xml 中,添加以下配置內容,注意原來內容不要動:

<!--更新語句接收 @Param 注解傳遞的參數 -->
<update id="updateWebsiteByParam">
update website set name = #{name},url= #{url} where id = #{id}
</update>

3》在net.biancheng.test包添加testPara類,代碼如下:

package net.biancheng.test;

import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import net.biancheng.mapper.WebsiteMapper;
import net.biancheng.po.Website;

public class testPara {

public static void main(String[] args) {
InputStream config;
try {
// 讀取配置文件
config = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder()
.build(config);
// 創建sqlsession對象
SqlSession ss = ssf.openSession();
// 獲取指定對象
WebsiteMapper websiteMapper = ss.getMapper(WebsiteMapper.class);
System.out.println("更新前:");
// 調用第3條記錄
Website websitelist0 = websiteMapper.selectAllWebsite().get(2);
System.out.println(websitelist0.show());
// 使用@Param 注解傳遞參數到更新語句中
String name = "果果鋪";
String url = "www.ggp.net";
Integer id = 5;
int i = websiteMapper.updateWebsiteByParam(name, url, id);
if (i > 0) {
System.out.println("通過para傳遞參數,共更新了 " + i + " 條記錄");
}
System.out.println("更新后:");
// 調用第3條記錄
Website websitelist1 = websiteMapper.selectAllWebsite().get(2);
System.out.println(websitelist1.show());
ss.commit();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}

4》程序執行結果

更新前數據庫記錄如圖:

  

 

 更新后數據庫記錄如圖所示:

 id為5的記錄名稱和url更新成功。

 2.3 使用 JavaBean 傳遞參數

在參數過多的情況下,我們還可以將參數通過 setter 方法封裝到 JavaBean(實體類)對象中傳遞給映射器

方法過程與insert中使用javabean傳遞參數相同,思路是先在接口類中創建方法,然后在WebsiteMapper.xml文件中創建id為剛才方法名稱的sql語句,最后在test類中測試執行,在此不詳細寫了。

以上 3 種方式的區別如下:

  • 使用 Map 傳遞參數會導致業務可讀性的喪失,繼而導致后續擴展和維護的困難,所以在實際應用中我們應該果斷廢棄該方式。
  • 使用 @Param 注解傳遞參數會受到參數個數的影響。當 n≤5 時,它是最佳的傳參方式,因為它更加直觀;當 n>5 時,多個參數將給調用帶來困難。
  • 當參數個數大於 5 個時,建議使用 JavaBean 方式。

本節代碼mybatisDemoA5,下節繼續。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM