日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis实现SaveOrUpdate

發(fā)布時間:2024/1/23 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis实现SaveOrUpdate 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

MyBatis實現(xiàn)SaveOrUpdate

這篇文章主要講如何通過xml方式實現(xiàn)SaveOrUpdate,但是仍然建議在Service中實現(xiàn)。

例子

<insert id="saveOrUpdate" ><selectKey keyProperty="count" resultType="int" order="BEFORE">select count(*) from country where id = #{id}</selectKey><if test="count > 0">update country set countryname = #{countryname},countrycode = #{countrycode} where id = #{id}</if><if test="count==0">insert into country values(#{id},#{countryname},#{countrycode})</if> </insert>

條件限制

根據(jù)不同的判斷邏輯,會有所不同,就上面這個例子而言,就要求實體類中包含count屬性(可以是別的名字)。否則selectKey的結(jié)果沒法保存,如果入?yún)⑹莻€Map類型,就沒有這個限制。

說明

從例子來看除了有個限制外,也沒別的麻煩。

通過selectKey做第一次查詢,然后根據(jù)結(jié)果進行判斷,所以這里的order="BEFORE"是必須的。

也是因為BEFORE,所以沒法通過<bind>標(biāo)簽來臨時存儲中間的值,只能在入?yún)⒅性黾訉傩詠泶娣拧?/p>

測試代碼

//數(shù)據(jù)庫中已經(jīng)存在該ID,但是countryname=China Country country = new Country(); country.setId(35); country.setCountryname("中國"); country.setCountrycode("CN"); //由于存在,這里會update int result = countryMapper.saveOrUpdate(country);//查詢結(jié)果,判斷是否已經(jīng)改變 Country c2 = countryMapper.selectById(35); assertEquals("中國",c2.getCountryname());//id=300的不存在 c2 = countryMapper.selectById(300); assertNull(c2);//將id=300 country.setId(300); //由于id=300不存在,這里會Insert result = countryMapper.saveOrUpdate(country);//查詢結(jié)果 c2 = countryMapper.selectById(300); assertNotNull(c2)

輸出日志

DEBUG ==> Preparing: select count(*) from country where id = ? DEBUG ==> Parameters: 35(Integer) TRACE <== Columns: C1 TRACE <== Row: 1 DEBUG <== Total: 1 DEBUG ==> Preparing: update country set countryname = ?,countrycode = ? where id = ? DEBUG ==> Parameters: 中國(String), CN(String), 35(Integer) DEBUG <== Updates: 1 DEBUG ==> Preparing: select * from country where id = ? DEBUG ==> Parameters: 35(Integer) TRACE <== Columns: ID, COUNTRYNAME, COUNTRYCODE TRACE <== Row: 35, 中國, CN DEBUG <== Total: 1 DEBUG ==> Preparing: select * from country where id = ? DEBUG ==> Parameters: 300(Integer) DEBUG <== Total: 0 DEBUG ==> Preparing: select count(*) from country where id = ? DEBUG ==> Parameters: 300(Integer) TRACE <== Columns: C1 TRACE <== Row: 0 DEBUG <== Total: 1 DEBUG ==> Preparing: insert into country values(?,?,?) DEBUG ==> Parameters: 300(Integer), 中國(String), CN(String) DEBUG <== Updates: 1 DEBUG ==> Preparing: select * from country where id = ? DEBUG ==> Parameters: 300(Integer) TRACE <== Columns: ID, COUNTRYNAME, COUNTRYCODE TRACE <== Row: 300, 中國, CN DEBUG <== Total: 1實際配置 類名 package com.jn.oracle.tz.importExcel.bean;public class ExcelUser {private String username; //用戶名private Integer reportId; //報表idprivate Integer isView; // 是否有查看權(quán)限private Integer isImport; //是否有導(dǎo)入權(quán)限private Integer count ; //一個計數(shù)器public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getReportId() {return reportId;}public void setReportId(Integer reportId) {this.reportId = reportId;}public Integer getIsView() {return isView;}public void setIsView(Integer isView) {this.isView = isView;}public Integer getIsImport() {return isImport;}public void setIsImport(Integer isImport) {this.isImport = isImport;}public Integer getCount() {return count;}public void setCount(Integer count) {this.count = count;} }xml 配置<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.jn.oracle.tz.importExcel.dao.ExcelUserDao"><resultMap id="reportConfigMap" type="com.jn.oracle.tz.importExcel.bean.ExcelUser" ><result property="username" column="USER_NAME" jdbcType="VARCHAR" /><result property="reportId" column="REPORT_ID" jdbcType="INTEGER" /><result property="isView" column="IS_VIEW" jdbcType="INTEGER" /><result property="isImport" column="IS_IMPORT" jdbcType="INTEGER" /></resultMap><insert id="saveOrUpdate" parameterType="com.jn.oracle.tz.importExcel.bean.ExcelUser"><selectKey keyProperty="count" resultType="int" order="BEFORE">select count(*) from TB_TZ_EXCEL_USER where report_id = #{reportId,jdbcType=INTEGER}and user_name = #{username,jdbcType=VARCHAR}</selectKey><if test="count > 0">update TB_TZ_EXCEL_USER set is_view = #{isView,jdbcType=INTEGER},is_import = #{isImport,jdbcType=INTEGER}where user_name = #{username,jdbcType=VARCHAR}and report_id = #{reportId,jdbcType=INTEGER}</if><if test="count == 0">insert into TB_TZ_EXCEL_USER(User_Name,REPORT_ID,IS_VIEW,IS_IMPORT)values( #{username,jdbcType=VARCHAR},#{reportId,jdbcType=INTEGER},#{isView,jdbcType=INTEGER},#{isImport,jdbcType=INTEGER} )</if></insert></mapper>

最后

這種方式只是利用了selectKey會多執(zhí)行一次查詢來實現(xiàn)的,但是如果你同時還需要通過selectKey獲取序列或者自增的id,就會麻煩很多(Oracle麻煩,其他支持自增的還是很容易)。

建議在復(fù)雜情況下,還是選擇在Service中實現(xiàn)更好。

總結(jié)

以上是生活随笔為你收集整理的MyBatis实现SaveOrUpdate的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。