Spring JdbcTemplate BatchUpdate () instance

Source: Internet
Author: User
Tags bulk insert

In some cases, you may want to insert a batch of records into the database. If you invoke a plug-in method for each record, the SQL statement will be compiled repeatedly, causing the system to slow down. In these cases, you can use the JdbcTemplate BATCHUPDATE () method to perform a bulk insert operation. In this way, the statement is compiled only once and executed multiple times. See the BATCHUPDATE () example of the JdbcTemplate class.
Insert Batch examplepublic void Insertbatch (final list<customer> customers) {  String sql = "INSERT INTO CUSTOM ER "+" (cust_id, NAME, age) VALUES (?,?,?) ";  Getjdbctemplate (). batchUpdate (SQL, new BatchPreparedStatementSetter () {@Overridepublic void Setvalues ( PreparedStatement PS, int i) throws SQLException {Customer customer = Customers.get (i);p s.setlong (1, Customer.getcustid ( ));p s.setstring (2, Customer.getname ());p S.setint (3, Customer.getage ());} @Overridepublic int getbatchsize () {return customers.size ();}  });}
Alternatively, you can execute SQL directly.
Insert batch example with Sqlpublic void Insertbatchsql (final String sql) {getjdbctemplate (). BatchUpdate (New string[]{ SQL});
Spring's bean configuration file
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "><bean id=" Customerdao "class=" Com.yiibai.customer.dao.impl.JdbcCustomerDAO "><property name=" DataSource "ref=" DataSource "/></bean ><bean id= "DataSource"         class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >< Property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/><property name= "url" value= "jdbc:mysql:// Localhost:3306/yiibaijava "/><property name=" username "value=" root "/><property name=" password "value=" Password "/></bean></beans>

Execute it

Package Com.yiibai.common;import Java.util.arraylist;import Java.util.list;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.yiibai.customer.dao.CustomerDAO; Import Com.yiibai.customer.model.customer;public class App {public static void main (string[] args) {Applicati             Oncontext context = new Classpathxmlapplicationcontext ("Spring-customer.xml");        Customerdao Customerdao = (Customerdao) context.getbean ("Customerdao");        Customer customer1 = new Customer (1, "Yiibai1", 21);        Customer Customer3 = new Customer (2, "Yiibai2", 22);          Customer Customer2 = new Customer (3, "Yiibai3", 23);        List<customer>customers = new arraylist<customer> ();        Customers.add (Customer1);        Customers.add (CUSTOMER2);                Customers.add (CUSTOMER3);        Customerdao.insertbatch (Customers);  String sql = "UPDATE CUSTOMER SET NAME = ' BATCHUPDATE '";      Customerdao.insertbatchsql (SQL); }}
In this example, a record of three customers is inserted and the names of all customers are updated in bulk. Download code –HTTP://PAN.BAIDU.COM/S/1JGTXFGI

Spring JdbcTemplate BatchUpdate () instance

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.