论坛首页 入门技术论坛

ibatis crud返回值问题

浏览 3263 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-07-15  

<insert id="insertBook" parameterClass="book">
insert into BOOKS (bookNum, bookName, author, publisher,price,
state, quality) values ( #bookNum:VARCHAR#, #bookName:VARCHAR#,
#author:VARCHAR#,#publisher:VARCHAR#, #price:FLOAT#,
#state:INTEGER#, #quality:INTEGER#)

<selectKey resultClass="int" keyProperty="bookId" >
SELECT @@IDENTITY AS bookId
</selectKey>

</insert>

 

在执行insert是才会返回插入的id

    public int update(Book book) throws DatabaseException{
        String method = "update";
        int result = 0;
        try {
            result = update("updateBook", book);
            debug("", method, Constant.LOG_UPDATE_SUCCESS);
        } catch (RuntimeException e) {
            error("", method, Constant.LOG_UPDATE_FAIL);
            throw new DatabaseException(DatabaseException.DB_Message, e);
        }
        return result;
    }

///////////////////////////////////////////////

对于删除

<delete id="deleteBook" parameterClass="int" >
        DELETE FROM BOOKS WHERE BookId = #bookId#
 </delete>

直接返回影响记录集

    /**
     *
     * @param bookId
     * @throws DatabaseException
     * @return int >0:delete success  <0: delete fail
     */
    public int deleteBook(int bookId) throws DatabaseException{
        String method = "deleteBook";
        int result = -1;
        try {
            result = (Integer)this.delete("deleteBook", bookId);
            debug("", method, Constant.LOG_DELETE_SUCCESS);
        } catch (RuntimeException e) {
            result = -1;
            error("", method, Constant.LOG_DELETE_FAIL);
            throw new DatabaseException(DatabaseException.DB_Message, e);
        }
        return result;
    }

 

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics