/selectKey> insert id="insertUser" >selectKey keyColumn="id" resultType="long" keyProperty="userId" order="BEFORE">SELECT USE。有關(guān)mybatis知識點解析 mybatis調(diào)用存儲過程( 六 )。" />

亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

有關(guān)mybatis知識點解析 mybatis調(diào)用存儲過程( 六 )


<selectKey keyColumn="id" resultType="long" keyProperty="id" order="BEFORE"></selectKey> <insert id="insertUser" ><selectKey keyColumn="id" resultType="long" keyProperty="userId" order="BEFORE">SELECT USER_ID.nextval as id from dual </selectKey> insert into user( user_id,user_name, user_password, create_time) values(#{userId},#{userName}, #{userPassword} , #{createTime, jdbcType= TIMESTAMP})</insert>此時會將Oracle生成的主鍵值賦予userId變量 。這個userId 就是USER對象的屬性,這樣就可以將生成的主鍵值返回了 。如果僅僅是在insert語句中使用但是不返回,此時keyProperty=“任意自定義變量名”,resultType 可以不寫 。
Oracle 數(shù)據(jù)庫中的值要設(shè)置為 BEFORE ,這是因為 Oracle中需要先從序列獲取值,然后將值作為主鍵插入到數(shù)據(jù)庫中 。
擴展如果Mysql 使用selectKey的方式獲取主鍵,需要注意下面兩點:
order : AFTER獲取遞增主鍵值 :SELECT LAST_INSERT_ID()
當實體類中的屬性名和表中的字段名不一樣 ,怎么辦第1種: 通過在查詢的SQL語句中定義字段名的別名,讓字段名的別名和實體類的屬性名一致 。
<select id="getOrder" parameterType="int" resultType="com.jourwon.pojo.Order">select order_id id, order_no orderno ,order_price price form orders where order_id=#{id};</select>第2種: 通過<resultMap>來映射字段名和實體類屬性名的一一對應(yīng)的關(guān)系 。
<select id="getOrder" parameterType="int" resultMap="orderResultMap">select * from orders where order_id=#{id}</select><resultMap type="com.jourwon.pojo.Order" id="orderResultMap"><!–用id屬性來映射主鍵字段–><id property="id" column="order_id"><!–用result屬性來映射非主鍵字段,property為實體類屬性名,column為數(shù)據(jù)庫表中的屬性–><result property ="orderno" column ="order_no"/><result property="price" column="order_price" /></reslutMap>Mapper 編寫有哪幾種方式?第一種:接口實現(xiàn)類繼承 SqlSessionDaoSupport:使用此種方法需要編寫mapper 接口,mapper 接口實現(xiàn)類、mapper.xml 文件 。
(1)在 sqlMapConfig.xml 中配置 mapper.xml 的位置
<mappers><mapper resource="mapper.xml 文件的地址" /><mapper resource="mapper.xml 文件的地址" /></mappers>(2)定義 mapper 接口
(3)實現(xiàn)類集成 SqlSessionDaoSupport
mapper 方法中可以 this.getSqlSession()進行數(shù)據(jù)增刪改查 。
(4)spring 配置
<bean id=" " class="mapper 接口的實現(xiàn)"><property name="sqlSessionFactory"ref="sqlSessionFactory"></property></bean>第二種:使用 org.mybatis.spring.mapper.MapperFactoryBean:
(1)在 sqlMapConfig.xml 中配置 mapper.xml 的位置,如果 mapper.xml 和mappre 接口的名稱相同且在同一個目錄,這里可以不用配置 。
<mappers><mapper resource="mapper.xml 文件的地址" /><mapper resource="mapper.xml 文件的地址" /></mappers>(2)定義 mapper 接口:
(3)mapper.xml 中的 namespace 為 mapper 接口的地址
(4)mapper 接口中的方法名和 mapper.xml 中的定義的 statement 的 id 保持一致
(5)Spring 中定義
<bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="https://www.520longzhigu.com/diannao/mapper 接口地址" /><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>第三種:使用 mapper 掃描器:
(1)mapper.xml 文件編寫:
mapper.xml 中的 namespace 為 mapper 接口的地址;
mapper 接口中的方法名和 mapper.xml 中的定義的 statement 的 id 保持一致;
如果將 mapper.xml 和 mapper 接口的名稱保持一致則不用在 sqlMapConfig.xml中進行配置 。
(2)定義 mapper 接口:
注意 mapper.xml 的文件名和 mapper 的接口名稱保持一致,且放在同一個目錄


以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!

「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助: