1. 程式人生 > >Mybatis/Ibatis,資料庫操作的返回值

Mybatis/Ibatis,資料庫操作的返回值

該問題,我百度了下,根本沒發現什麼有價值的文章;還是看原始碼(詳見最後附錄)中的註釋,最有效了!
insert,返回值是:新插入行的主鍵(primary key);需要包含<selectKey>語句,才會返回主鍵,否則返回值為null。
update/delete,返回值是:更新或刪除的行數;無需指明resultClass;但如果有約束異常而刪除失敗,只能去捕捉異常。
queryForObject,返回的是:一個例項物件或null;需要包含<select>語句,並且指明resultMap;
queryForList,返回的是:例項物件的列表;需要包含<select>語句,並且指明resultMap;

我的配置檔案如下(desktop_common_sqlMap.xml):

  1. <typeAliasalias="UnlockTagInfo"type="com.desktop.common.bean.UnlockTagInfo"/>
  2. <resultMapclass="UnlockTagInfo"id="UnlockTagInfoResult">
  3.     <resultcolumn="id"property="id"jdbcType="INTEGER"/>
  4.     <resultcolumn="name"property="name"
    jdbcType="VARCHAR"/>
  5.     <resultcolumn="description"property="description"jdbcType="VARCHAR"/>
  6.     <resultcolumn="priority"property="priority"jdbcType="INTEGER"/>
  7. </resultMap>
  8. <insertid="insertUnlockTagInfo"parameterClass="map">
  9.     <selectKeyresultClass="int"keyProperty
    ="id">
  10.         select  
  11.         nextval('desktop_unlock_tag_id_seq') as id  
  12.     </selectKey>
  13.     insert into  
  14.     desktop_unlock_tag(id,name,description,priority)  
  15.     values(#id:INTEGER#,#name:VARCHAR#,#description:VARCHAR#,#priority:INTEGER#)  
  16. </insert>
  17. <updateid="updateUnlockTagInfo"parameterClass="map">
  18.     update  
  19.     desktop_unlock_tag  
  20.     set modify_time=now(),priority=#priority:INTEGER#,  
  21.     name=#name:VARCHAR#,description=#description:VARCHAR#  
  22.     where  
  23.     id=#id:INTEGER#  
  24. </update>
  25. <deleteid="deleteUnlockTagInfo"parameterClass="int">
  26.     delete from  
  27.     desktop_unlock_tag  
  28.     where id=#value:INTEGER#  
  29. </delete>
  30. <selectid="countUnlockTagInfo"resultClass="int">
  31.     select count(*)  
  32.     from  
  33.     desktop_unlock_tag  
  34. </select>
  35. <sqlid="selectUnlockTagInfo">
  36.     select  
  37.     id,name,description,priority  
  38.     from  
  39.     desktop_unlock_tag  
  40. </sql>
  41. <selectid="findUnlockTagInfoById"parameterClass="int"
  42.     resultMap="UnlockTagInfoResult">
  43.     <includerefid="selectUnlockTagInfo"/>
  44.     where id=#id:INTEGER#  
  45. </select>
  46. <selectid="listUnlockTagInfo"parameterClass="map"
  47.     resultMap="UnlockTagInfoResult">
  48.     <includerefid="selectUnlockTagInfo"/>
  49.     order by  
  50.     modify_time desc limit #size:INTEGER#  
  51.     offset #start:INTEGER#  
  52. </select>
我的DAO原始碼如下:
  1. publicclass UnlockTagDaoImpl extends SqlMapClientDaoSupport implements
  2.         UnlockTagDao {  
  3.     @Override
  4.     public Integer addItem(String name, String desc, Integer priority) {  
  5.         SqlMapClientTemplate template = this.getSqlMapClientTemplate();  
  6.         Map<String, Object> args = new HashMap<String, Object>();  
  7.         args.put("name", name);  
  8.         args.put("description", desc);  
  9.         args.put("priority", priority);  
  10.         Object key = template.insert("DesktopCommon.insertUnlockTagInfo", args);  
  11.         return (Integer) key;  
  12.     }  
  13.     @Override
  14.     publicboolean updateItem(Integer id, String name, String description,  
  15.             Integer priority) {  
  16.         SqlMapClientTemplate template = this.getSqlMapClientTemplate();  
  17.         Map<String, Object> args = new HashMap<String, Object>();  
  18.         args.put("id", id);  
  19.         args.put("name", name);  
  20.         args.put("description", description);  
  21.         args.put("priority", priority);  
  22.         try {  
  23.             int c = template.update("DesktopCommon.updateUnlockTagInfo", args);  
  24. 相關推薦

    Mybatis/Ibatis資料庫操作返回

    該問題,我百度了下,根本沒發現什麼有價值的文章;還是看原始碼(詳見最後附錄)中的註釋,最有效了! insert,返回值是:新插入行的主鍵(primary key);需要包含<selectKey>語句,才會返回主鍵,否則返回值為null。 updat

    laravel資料庫操作返回 Eloquent

    新增 User::create([]);  成功返回Eloquent物件(包含id,created_at,updated_at和傳入值)   失敗丟擲異常 $user = new User(); $user->phone = '139......'; $us

    ThinkPHP中資料庫操作返回總結

    文章轉自:http://www.baiwar.com/post/thinkphp-database-operations-in-the-return-value.html Thinkphp中的Think\Model類提供了資料庫的基本CURD(Create、Update

    PYTHON自動化Day6-函式多個返回和匿名函式、列表生成式三元運算子os模組sys模組時間模組字典排序資料庫操作加密(md5)

    一.函式多個返回值和匿名函式 #函式返回多個值,用一個變數接收 def say(): num1=1 num2=2 num3=3 return num1,num2,num3 res=say() print(res) #打印出來是元組。 函式如果返回多個值的話,會把返回的

    Mybatis Update操作返回

    後端的資料持久化使用的是 Mybatis ,在做高併發下賬戶增減餘額的時候,打算使用樂觀鎖來解決這個問題。在獲取update操作的返回值時遇到了一個問題,似乎 Mybatis 進行 update 操作得到的 int 返回值並不是影響的行數。這下就尷尬了。 一般而言,我們

    java中使用mybatis呼叫儲存過程拿到返回(單引數返回

    service業務層呼叫dao層 注意:返回值直接從物件裡獲取 不需要拿物件接收再獲取 dao.uspGetUser(userPO);//物件封裝了儲存過程的入參和出參 count = userPO.getCount(); //count 是儲存過程的返回值 dao層介面 pu

    Java 執行資料庫儲存過程並帶返回

    前提是載入資源DataSource private JdbcTemplate jdbcTemplate; Java 呼叫儲存過程: @Override public String oneUniscInfoHisToDm(final Str

    Finally-操作返回

    acc finall line style 返回值 console urn hide images Finally中操作返回值會出現一個問題?值沒有被改變? 1 static int M1() 2 { 3 int res

    C#異步執行帶有返回和參數的方法且獲取返回

    urn 利用 回調方法 ext col list ont mes gate 很多時候需要用到這些小知識點,做做筆記一起成長 下面是需要異步執行的方法 //獲取所有的郵件 private List<EmailModel> GetEmailOnl

    JDBC Update操作返回和Insert操作返回主鍵

    not null rest enc 不同 生成 statement -a AC ret JDBC Update操作返回值 在操作數據庫時,update操作會返回數據庫更新行數,但是在JDBC默認情況下則不會返回數據庫更新行數,這一點有所不同,在實際操作中可能會出現意想不到的

    正則表達式中的matchtestexecsearch的返回

    div gpo post arch 感覺 不知道 淺談 nbsp ear 今天突然被問到了正則表達式,因為長時間不用突然不知道怎麽用了,只知道有這麽個東西。然後去網上查了一下,感覺寫的不少,但解釋的有點模糊,今天我來淺談一下。 1,match的用法  A,在不加全局

    使用 ResponseBodyAdvice 攔截Controller方法預設返回引數統一處理返回/響應體

    使用 @ControllerAdvice & ResponseBodyAdvice 攔截Controller方法預設返回引數,統一處理返回值/響應體 1、Controller程式碼 以下是Controller查詢方法原始碼: /** * controller * * @au

    ListView的getview不執行並且getCount返回大於0

    這個問題網上描述了幾點原因,這裡只講我遇到的情況。 遇到的現象:Listview不顯示Item,手動點一下介面才會顯示。Log列印getCount返回值大於0。 我就說Listview及其Adapter寫個幾百個肯定是有的,應該不會有什麼問題的。然後百思不得解。 最後發現我的資料來自

    Spring整合mybatis資料庫配置com.mysql.jdbc.Driver和com.mysql.cj.jdbc.Driver區別

    1、#mysql-connector-java 5 用法 spring.datasource.driver-calss-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnico

    為何Mybatis查詢無資料時返回不是null

    一、返回為一個List List<TeacherData> teacherData=null; teacherData=teacherService.queryTeacherByNameService(name);#假設資料庫沒有匹配的資料 if

    java多執行緒 demo 包含帶返回的Callable和不帶返回的Runnable

    輸入int,利用callable取得double值,再用runnable列印 import java.util.concurrent.*; public class TestMultiThread { private static ExecutorService pool = nul

    php精闢程式碼欣賞資料庫操作封裝所有資料庫操作

    下面是php各種程式碼庫,個人分享 <?php //******************************************************************* //此處構造一個數據庫操作類,封裝所有資料庫操作 //可以擴充套件便於後臺管理程

    php 除錯微信介面時curl無返回file_get_contents有返回的解決方法

    在公司伺服器調式微信介面,使用curl獲取access_token完全沒有問題, 同樣的程式碼拿回家使用php內建webserver調試出現問題,curl沒有返回值,也沒報錯 原因是php內建webserver驗證了https的問題,把curl加入如下程式碼即可:

    callable介面配合ExecutorService實現多執行緒處理資料並接收返回(2018-08-23)

    /** * @author chenzhen * Created by chenzhen on 2018/8/22. */ @Data public class QuickPullGit implements Callable<ArrayList&l

    採用HttpURLConnection方式呼叫第三方介面介面的返回經過unicode編碼處理

    在介面的呼叫過程中,第三方介面的返回值使用unicode處理了,呼叫過程中一直報錯 改成下面這樣就可以了 public class TestStatic { public static void main(String[] args) throws Exception {