Batch SQL Implementation of getjdbctemplate () in spring

Source: Internet
Author: User
// Application Scenario:
See the following SQL:
String SQL = "insert into to_order_return_info values (seq_order_return_info.nextval ,?,?,?,?,?,?) ";
This statement is a forwardTo_order_return_infoInsert a new record to the table

However, if you need to insert n data entries, the normal solution is to perform n data insertion operations.
JDBC supports batch SQL operations on the same data in batches and mixed data in batches.
Spring encapsulates JDBC and supports batch operations.
The following are batch operations on the same data (data inserted into a unified table:

RequiredBatchupdate(); Method
AndBatchpreparedstatementsetterInterface
ImplementationGetbatchsize(); AndSetvaluesMethod

Getbatchsize (); returns the number of batch entries, that is, the number of added, deleted, and modified operations;
 
Setvalues (preparedstatement ps,IntI)
This method will automatically retrieve the corresponding add, delete, and modify data through the I traversal list.



1 Public Int Orderreturnoperate ( Final List <order_return_info> Orders) 2 Throws Baseexception { 3 4 Try { 5 String [] ordernums = New String [orders. Size ()]; 6 For ( Int J = 0; j <ordernums. length; j ++ ){ 7 If (Orders. Get (j )! = Null ){ 8 Ordernums [J] = Orders. Get (j). getordernum (); 9 } Else { 10 Log.info ("ordernum is null! " ); 11 Return -1 ; 12 } 13 } 14 Updateorderitemsstatus (ordernums, orders. Get (0 )! = Null ? Orders 15 . Get (0). getorderstatus (): "2014" ); 16 17 String querystatussql = "select f_status_name from to_order_status where f_status_num =? " ; 18 Final String statusname = (string) This . Getjdbctemplate () 19 . Queryforobject (querystatussql, 20 New Object [] {orders. Get (0 ). Getorderstatus ()}, 21 String. Class );
22 String SQL = "insert into to_order_return_info values (seq_order_return_info.nextval ,?,?,?,?,?,?) " ; 23 // Execute batch SQL to process multiple insert operations 24 Int [] Count = This . Getjdbctemplate ().Batchupdate(SQL, 25 New Batchpreparedstatementsetter(){ 26 27 @ Override 28 // Number of executions 29 Public Int Getbatchsize (){ 30 Return Orders. Size (); 31 } 32 33 @ Override 34 // Execution Parameters 35 Public Void Setvalues (preparedstatement ps, Int I) 36 Throws Sqlexception { 37 PS. setstring (1 , Orders. Get (I). getordernum ()); 38 PS. setstring (2 , Orders. Get (I). getorderstatus ()); 39 PS. setstring (3 , Statusname ); 40 PS. setstring (4 , Orders. Get (I). getopid ()); 41 PS. setstring (5 , Orders. Get (I). getopname ()); 42 PS. setstring (6, Orders. Get (I). getconfirmtime ()); 43 } 44 45 }); 46 } Catch (Exception e ){ 47 Log.info (E. getmessage ()); 48 Return -1 ; 49 } 50 Return 1 ; 51 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.