MyBatis中insert操作前往主鍵的完成辦法。本站提示廣大學習愛好者:(MyBatis中insert操作前往主鍵的完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是MyBatis中insert操作前往主鍵的完成辦法正文
在應用MyBatis做耐久層時,insert語句默許是不前往記載的主鍵值,而是前往拔出的記載條數;假如營業層須要獲得記載的主鍵時,可以經由過程設置裝備擺設的方法來完成這個功效
針對Sequence主鍵而言,在履行insert sql前必需指定一個主鍵值給要拔出的記載,如Oracle、DB2,可以采取以下設置裝備擺設方法:
<insert id="add" parameterType="vo.Category"> <selectKey resultType="Java.lang.Short" order="BEFORE" keyProperty="id"> SELECT SEQ_TEST.NEXTVAL FROM DUAL </selectKey> insert into category (name_zh, parent_id, show_order, delete_status, description ) values (#{nameZh,jdbcType=VARCHAR}, #{parentId,jdbcType=SMALLINT}, #{showOrder,jdbcType=SMALLINT}, #{deleteStatus,jdbcType=BIT}, #{description,jdbcType=VARCHAR} ) </insert>
針對自增主鍵的表,在拔出時不須要主鍵,而是在拔出進程主動獲得一個自增的主鍵,好比MySQL,可以采取以下兩種設置裝備擺設方法:
<insert id="add" parameterType="vo.Category" useGeneratedKeys="true" keyProperty="id"> insert into category (name_zh, parent_id, show_order, delete_status, description ) values (#{nameZh,jdbcType=VARCHAR}, #{parentId,jdbcType=SMALLINT}, #{showOrder,jdbcType=SMALLINT}, #{deleteStatus,jdbcType=BIT}, #{description,jdbcType=VARCHAR} ) </insert>
或
<insert id="add" parameterType="vo.Category"> <selectKey resultType="java.lang.Short" order="AFTER" keyProperty="id"> SELECT LAST_INSERT_ID() AS id </selectKey> insert into category (name_zh, parent_id, show_order, delete_status, description ) values (#{nameZh,jdbcType=VARCHAR}, #{parentId,jdbcType=SMALLINT}, #{showOrder,jdbcType=SMALLINT}, #{deleteStatus,jdbcType=BIT}, #{description,jdbcType=VARCHAR} ) </insert>
在拔出操作完成以後,參數category的id屬性就曾經被賦值了
假如數據庫表的主鍵不是自增的類型,那末就須要運用層生成主鍵的方法··········這個就不多說了,須要的同伙,可以留言交換··
上面是針對Oracle的寫法,Oracle沒有autoincrement,而是用觸發器完成的 CURRVAL是在觸發器中界說的.
<insert id="insert" parameterClass="ProFeeKindObject"> <![CDATA[ INSERT INTO t_pro_feeKind (KINDID,kindName,kindType,enable) VALUES (seq_t_pro_feekind_id.nextval,#kindName#,#kindType#,#enable#) ]]> <selectKey resultClass="java.lang.Integer" keyProperty="kindId" > SELECT seq_t_pro_feekind_id.CURRVAL AS kindId FROM DUAL </selectKey> </insert> <!-- 上面是針對MySQL的寫法 --> <!-- <selectKey resultClass="int" keyProperty="id" > SELECT @@IDENTITY AS id </selectKey> -->
其他參考代碼:
耐久化某個實體對象(如保留一個對象)時,假如我們不消selectKey,那末我們不會連忙獲得實體對象的Id屬性的,也就是數據表主鍵
Java代碼
Permission permission = new Permission(); permission.set... permmisonDao.createPermission(permission); assertNull(permission); Permission permission = new Permission(); permission.set... permmisonDao.createPermission(permission); assertNull(permission);
selectKey元素與其在父元素中的地位有關
<insert id="addPermission" parameterClass="Permission"> <selectKey resultClass="int" keyProperty="permissionId"> SELECT SEQ_P_PERMISSION.NEXTVAL FROM DUAL </selectKey> INSERT INTO P_PERMISSION ( PERMISSIONID, PERMISSIONINFO, PERMISSIONNAME, PERMISSIONENNAME, URL ) VALUES ( #permissionId#, #permissionInfo#, #permissionName#, #permissionEnName#, #url# ) </insert> <insert id="addPermission" parameterClass="Permission"> <selectKey resultClass="int" keyProperty="permissionId"> SELECT SEQ_P_PERMISSION.NEXTVAL FROM DUAL </selectKey> INSERT INTO P_PERMISSION ( PERMISSIONID, PERMISSIONINFO, PERMISSIONNAME, PERMISSIONENNAME, URL ) VALUES ( #permissionId#, #permissionInfo#, #permissionName#, #permissionEnName#, #url# ) </insert>
Mysql、SQLServer在後
Xml代碼
<insert id="addPermission" parameterClass="Permission"> INSERT INTO P_PERMISSION ( PERMISSIONID, PERMISSIONINFO, PERMISSIONNAME, PERMISSIONENNAME, URL ) VALUES ( #permissionId#, #permissionInfo#, #permissionName#, #permiss ionEnName#, #url# ) <selectKey resultClass="int" keyProperty="permissionId"> SELECT LAST_INSERT_ID() </selectKey> </insert> <insert id="addPermission" parameterClass="Permission"> INSERT INTO P_PERMISSION ( PERMISSIONID, PERMISSIONINFO, PERMISSIONNAME, PERMISSIONENNAME, URL ) VALUES ( #permissionId#, #permissionInfo#, #permissionName#, #permissionEnName#, #url# ) <selectKey resultClass="int" keyProperty="permissionId"> SELECT LAST_INSERT_ID() </selectKey> </insert>
像下面如許書寫,與selectKey的地位接洽得太慎密了,iBatis的sqlMap設置裝備擺設文件的selectKey元素有個type屬性,可以指定pre或許post表現前生成照樣後生成。
關於Oracle,表現為
Xml代碼
<insert id="addPermission" parameterClass="Permission"> <selectKey resultClass="int" keyProperty="permissionId" type="pre"> SELECT SEQ_P_PERMISSION.NEXTVAL FROM DUAL </selectKey> INSERT INTO P_PERMISSION ( PERMISSIONID, PERMISSIONINFO, PERMISSIONNAME, PERMISSIONENNAME, URL ) VALUES ( #permissionId#, #permissionInfo#, #permissionName#, #permissionEnName#, #url# ) </insert> <insert id="addPermission" parameterClass="Permission"> <selectKey resultClass="int" keyProperty="permissionId" type="pre"> SELECT SEQ_P_PERMISSION.NEXTVAL FROM DUAL </selectKey> INSERT INTO P_PERMISSION ( PERMISSIONID, PERMISSIONINFO, PERMISSIONNAME, PERMISSIONENNAME, URL ) VALUES ( #permissionId#, #permissionInfo#, #permissionName#, #permissionEnName#, #url# ) </insert>
Mysql、SQLServer等表現為:
Xml代碼
<insert id="addPermission" parameterClass="Permission"> <selectKey resultClass="int" keyProperty="permissionId" type="post"> SELECT LAST_INSERT_ID() </selectKey> INSERT INTO P_PERMISSION ( PERMISSIONID, PERMISSIONINFO, PERMISSIONNAME, PERMISSIONENNAME, URL ) VALUES ( #permissionId#, #permissionInfo#, #permissionName#, #permissionEnName#, #url# ) </insert>
以上所述是小編給年夜家引見的MyBatis中insert操作前往主鍵的辦法,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!