40 JdbcTemplate batchUpdate() 实

作者: 笑Skr人啊 | 来源:发表于2017-08-28 16:55 被阅读48次

    在某些情况下,可能需要将一批记录插入到数据库中。如果你对每条记录调用一个插件的方法,SQL语句将被重复编译,造成系统缓慢进行。
    在上述情况下,你可以使用 JdbcTemplate BATCHUPDATE()方法来执行批量插入操作。用这种方法,该语句只被编译一次,执行多次。

    • 配置文件与上级篇中一直,只是路径不同

    详见 JdbcTemplate 类的 BATCHUPDATE()示例。

    package com.gp6.jdbc.template.batchUpdate.bean;
    
    public class Customer {
        private int custId;
        private String name;
        private int age;
        
        public Customer(int custId, String name, int age) {
            this.custId = custId;
            this.name = name;
            this.age = age;
        }
        
        public Customer(){
            
        }
        
        
        public int getCustId() {
            return custId;
        }
        public void setCustId(int custId) {
            this.custId = custId;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    
    package com.gp6.jdbc.template.batchUpdate.dao;
    
    import java.util.List;
    
    import com.gp6.jdbc.template.batchUpdate.bean.Customer;
    
    public interface CustomerDAO {
        public void insertBatch(final List<Customer> customers);
        public void insertBatchSQL(final String sql);
    }
    
    

    重点类

    package com.gp6.jdbc.template.batchUpdate.dao.impl;
    
    
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.util.List;
    
    import org.springframework.jdbc.core.BatchPreparedStatementSetter;
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    import com.gp6.jdbc.template.batchUpdate.bean.Customer;
    import com.gp6.jdbc.template.batchUpdate.dao.CustomerDAO;
    
    public class CustomerDAOImpl extends JdbcDaoSupport implements CustomerDAO {
        
        public void insertBatch(final List<Customer> customers){
            String sql = "INSERT INTO CUSTOMER (CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
                            
            getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                        
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    Customer customer = customers.get(i);
                    ps.setLong(1, customer.getCustId());
                    ps.setString(2, customer.getName());
                    ps.setInt(3, customer.getAge());
                }
                        
                @Override
                public int getBatchSize() {
                    return customers.size();
                }
            });
        }
        
        public void insertBatchSQL(final String sql){
            getJdbcTemplate().batchUpdate(new String[]{sql});
        }
    }
    
    
    
    
    package com.gp6.jdbc.template.batchUpdate;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.gp6.jdbc.template.batchUpdate.bean.Customer;
    import com.gp6.jdbc.template.batchUpdate.dao.CustomerDAO;
    
    public class Test {
        public static void main( String[] args ) {
            ApplicationContext context = new ClassPathXmlApplicationContext("com/gp6/jdbc/template/batchUpdate/etc/applicationContext.xml");
             
            CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO");
            Customer customer1 = new Customer(1, "gp5",21);
            Customer customer3 = new Customer(2, "gp6",22);
            Customer customer2 = new Customer(3, "gp7",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);
        }
    }   
    
    

    相关文章

      网友评论

        本文标题:40 JdbcTemplate batchUpdate() 实

        本文链接:https://www.haomeiwen.com/subject/xwygdxtx.html