Mybatis資料庫操作的返回值
阿新 • • 發佈:2019-02-15
insert,返回值是:新插入行的主鍵(primary key);需要包含<selectKey>語句,才會返回主鍵,否則返回值為null。
update/delete,返回值是:更新或刪除的行數;無需指明resultClass;但如果有約束異常而刪除失敗,只能去捕捉異常。
queryForObject,返回的是:一個例項物件或null;需要包含<select>語句,並且指明resultMap;
queryForList,返回的是:例項物件的列表;需要包含<select>語句,並且指明resultMap;
我的配置檔案如下(desktop_common_sqlMap.xml):
- <typeAliasalias="UnlockTagInfo"type="com.desktop.common.bean.UnlockTagInfo"/>
- <resultMapclass="UnlockTagInfo"id="UnlockTagInfoResult">
- <resultcolumn="id"property="id"jdbcType="INTEGER"/>
- <resultcolumn="name"property="name"jdbcType="VARCHAR"/>
-
<resultcolumn="description"
- <resultcolumn="priority"property="priority"jdbcType="INTEGER"/>
- </resultMap>
- <insertid="insertUnlockTagInfo"parameterClass="map">
- <selectKeyresultClass="int"keyProperty="id">
- select
-
nextval('desktop_unlock_tag_id_seq') as id
- </selectKey>
- insert into
- desktop_unlock_tag(id,name,description,priority)
- values(#id:INTEGER#,#name:VARCHAR#,#description:VARCHAR#,#priority:INTEGER#)
- </insert>
- <updateid="updateUnlockTagInfo"parameterClass="map">
- update
- desktop_unlock_tag
- set modify_time=now(),priority=#priority:INTEGER#,
- name=#name:VARCHAR#,description=#description:VARCHAR#
- where
- id=#id:INTEGER#
- </update>
- <deleteid="deleteUnlockTagInfo"parameterClass="int">
- delete from
- desktop_unlock_tag
- where id=#value:INTEGER#
- </delete>
- <selectid="countUnlockTagInfo"resultClass="int">
- select count(*)
- from
- desktop_unlock_tag
- </select>
- <sqlid="selectUnlockTagInfo">
- select
- id,name,description,priority
- from
- desktop_unlock_tag
- </sql>
- <selectid="findUnlockTagInfoById"parameterClass="int"
- resultMap="UnlockTagInfoResult">
- <includerefid="selectUnlockTagInfo"/>
- where id=#id:INTEGER#
- </select>
- <selectid="listUnlockTagInfo"parameterClass="map"
- resultMap="UnlockTagInfoResult">
- <includerefid="selectUnlockTagInfo"/>
- order by
- modify_time desc limit #size:INTEGER#
- offset #start:INTEGER#
- </select>