insert,返回值是:新插入行的主鍵(primary key);需要包含<selectKey>語句,纔會返回主鍵,否則返回值爲null。
update/delete,返回值是:更新或刪除的行數;無需指明resultClass;但如果有約束異常而刪除失敗,只能去捕捉異常。
queryForObject,返回的是:一個實例對象或null;需要包含<select>語句,並且指明resultMap;
queryForList,返回的是:實例對象的列表;需要包含<select>語句,並且指明resultMap;
我的配置文件如下(desktop_common_sqlMap.xml):
- <typeAlias alias="UnlockTagInfo" type="com.desktop.common.bean.UnlockTagInfo" />
- <resultMap class="UnlockTagInfo" id="UnlockTagInfoResult">
- <result column="id" property="id" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- <result column="description" property="description" jdbcType="VARCHAR" />
- <result column="priority" property="priority" jdbcType="INTEGER" />
- </resultMap>
- <insert id="insertUnlockTagInfo" parameterClass="map">
- <selectKey resultClass="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>
- <update id="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>
- <delete id="deleteUnlockTagInfo" parameterClass="int">
- delete from
- desktop_unlock_tag
- where id=#value:INTEGER#
- </delete>
- <select id="countUnlockTagInfo" resultClass="int">
- select count(*)
- from
- desktop_unlock_tag
- </select>
- <sql id="selectUnlockTagInfo">
- select
- id,name,description,priority
- from
- desktop_unlock_tag
- </sql>
- <select id="findUnlockTagInfoById" parameterClass="int"
- resultMap="UnlockTagInfoResult">
- <include refid="selectUnlockTagInfo" />
- where id=#id:INTEGER#
- </select>
- <select id="listUnlockTagInfo" parameterClass="map"
- resultMap="UnlockTagInfoResult">
- <include refid="selectUnlockTagInfo" />
- order by
- modify_time desc limit #size:INTEGER#
- offset #start:INTEGER#
- </select>