Mybatis数据库操作的返回值

来源:互联网 发布:sql replace into用法 编辑:程序博客网 时间:2024/06/06 02:14

insert,返回值是:新插入行的主键(primary key);需要包含<selectKey>语句,才会返回主键,否则返回值为null。
update/delete,返回值是:更新或删除的行数;无需指明resultClass;但如果有约束异常而删除失败,只能去捕捉异常。
queryForObject,返回的是:一个实例对象或null;需要包含<select>语句,并且指明resultMap;
queryForList,返回的是:实例对象的列表;需要包含<select>语句,并且指明resultMap;

我的配置文件如下(desktop_common_sqlMap.xml):

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <typeAlias alias="UnlockTagInfo" type="com.desktop.common.bean.UnlockTagInfo" />  
  2. <resultMap class="UnlockTagInfo" id="UnlockTagInfoResult">  
  3.     <result column="id" property="id" jdbcType="INTEGER" />  
  4.     <result column="name" property="name" jdbcType="VARCHAR" />  
  5.     <result column="description" property="description" jdbcType="VARCHAR" />  
  6.     <result column="priority" property="priority" jdbcType="INTEGER" />  
  7. </resultMap>  
  8. <insert id="insertUnlockTagInfo" parameterClass="map">  
  9.     <selectKey resultClass="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. <update id="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. <delete id="deleteUnlockTagInfo" parameterClass="int">  
  26.     delete from  
  27.     desktop_unlock_tag  
  28.     where id=#value:INTEGER#  
  29. </delete>  
  30. <select id="countUnlockTagInfo" resultClass="int">  
  31.     select count(*)  
  32.     from  
  33.     desktop_unlock_tag  
  34. </select>  
  35. <sql id="selectUnlockTagInfo">  
  36.     select  
  37.     id,name,description,priority  
  38.     from  
  39.     desktop_unlock_tag  
  40. </sql>  
  41. <select id="findUnlockTagInfoById" parameterClass="int"  
  42.     resultMap="UnlockTagInfoResult">  
  43.     <include refid="selectUnlockTagInfo" />  
  44.     where id=#id:INTEGER#  
  45. </select>  
  46. <select id="listUnlockTagInfo" parameterClass="map"  
  47.     resultMap="UnlockTagInfoResult">  
  48.     <include refid="selectUnlockTagInfo" />  
  49.     order by  
  50.     modify_time desc limit #size:INTEGER#  
  51.     offset #start:INTEGER#  
  52. </select>  
0 0
原创粉丝点击