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