Hibernate大量操作資料

來源:互聯網
上載者:User

標籤:get   使用   資料   os   類   cti   

批量處理資料

①通過session來進行大量操作

?具體的做法是在處理完一個對象或小批量對象後,立刻調用flush方法清理緩衝,然後再調用clear方法清空緩衝

<!-- 設定JDBC單次批量處理的數目 -->

<property name=”hibernate.jdbc.batch_size”>20</property>

 

如果對象採用”identity” 標示符產生器,則Hibernate無法在JDBC層進行批量插入操作

進行大量操作是建議關閉二級緩衝!!!

<!-- 關閉二級緩衝 -->

<property name=”hibernate.cache.user_second_level_cache”>false</property>

 

 

②使用callablestatement調用預存程序

1.插入資料的預存程序:

Create or replace procedure login_insert(username in varchar,password in varchar,age in number) as

Begin

Insert into login (username,password,age) values (username,password,age);

End;

 

2.更新資料的預存程序:

Create or replace procedure login_Update(a in number) as

Begin

Update lobin set age=a where username=’Tom’;

End;

 

3.刪除資料的預存程序:

Create or replace procedure login_delete(uname in varchar) as

Begin

Delete form login where USERNAME = uname;

End;

 

//調用插入資料的預存程序

Session session = new Configuration().configure().buildSessionFactory().openSessin();

Transaction tran  =session.beginTransaction();

CallableStatement cst = session.connection.prepareCall(“{call login_insert(?,?,?)}”);

cst.setString(1,”aaa”);

cst.setString(2,”22222”);

cst.setLong(3,21);

cst.executeUpdate();

Tran.commit();

Session.close();

 

查詢資料的預存程序:

*Logins表示系統遊標,將查詢結果賦給logins。

Create or replace procedure login_getlist(logins OUT SYS_REFCURSOR)

Begin

Open logins for select * from login;

End;

?在hibernate中如何調用這個帶有輸出參數(返回結果集)的預存程序:

//調用帶有返回結果集的預存程序

Session session = new Configuration().configure().buildSessionFactory().openSession();

Transaction tran = session.beginTransaction();

CallableStatement cst = session.connection().prepareCall(“{call login_getlist(?)}”);

/*調用CallableStatement的registerOutParameter()方法,傳參,”1”代表第一個問號,第二個參數Oracle的資料類型,因為預存程序是用系統遊標類型的輸出參數來返回的結果集,所以在這裡要傳入一個Oracle遊標的類型*/

cst.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);

cst.execute();

ResultSet rs = (ResultSet)cst.getObject(1);//擷取預存程序的輸出參數,也就是系統遊標的輸出參數,要使用強制類型轉換

While(rs.next()){

System.out.print(“使用者名稱:”+rs.getString(“username”));

System.out.print(“密碼:”+rs.getString(“password”));

System.out.print(“年齡:”+rs.getString(“age”));

}

Tran.commit();

Session.close();

使用命名SQL調用預存程序:

<!-- 調用查詢的預存程序, callable=”true”代表我們的聲明的命名支援SQL預存程序,不能省略,省略會報錯-->

<sql-query name=”loginGetList”callable=”true”>

{call login_getlist(?)}

<return alias=”1” class=”com.test.Login” />

</sql-query>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.