is developed in the Dao layer for back end service, especially when large data batch is inserted, when the ordinary ORM framework (Mybatis, hibernate, JPA) can not meet the requirements of the performance of the program. Of course, it is impossible for us to use the original JDBC to operate, so the efficiency will be high, but the complexity will increase.

takes into account the use of JdbcTemplate and named parameter namedParameterJdbcTemplate in Spring for batch operation. Before


, before we begin to explain, let's first look at how the JPA before batch operation.

entity class User:

 public class AppStudent {private Integer ID; private Integer. NTS DynamicQuery {@PersistenceContext private EntityManager em; public EntityManager getEntityManager () {return em;} < resultList.size (); i++) {T t = resultList.get (I); em.persist (T);}} 

retrofit method: can be used to execute any statement, generally used to execute a statement The

  • update method and the batchUpdate method: the update method is used to perform new, modified, and deleted statements; the batchUpdate method is used to perform batch related statements; the
  • query method and queryForXXX method: for executing query related statements;
  • call Square: for executing stored procedures, functions, and functions A number of related statements.
  • we only need to use @Autowired for injection in the jdbcTemplate class:

     @Autowired private JdbcTemplate jdbcTemplate; Gs.add (New Object[]{1, "Xiao Ming", 21}); batchArgs.add (New Object[]{2, "little red", 22}); batchArgs.add (New Object[]{3, "Lucy", 23}); But when the database fields are much more, the encoding in the form of a placeholder may not be so good, where spring also provides SimpleJdbcTemplate (after Spring3.1+ is marked as outdated, Spring 4.3 is completely removed, the following fully meets the requirements) and NamedParameterJdbcTemp Late template engine. 


    believes that students who have used Hibernate know that HQL can be used. Or: * configuring query parameters externally. In the Spring JDBC framework, a way of binding SQL parameters is also provided, using the named parameter.

    we only need to use @Autowired for injection in the NamedParameterJdbcTemplate class:

     @Autowired private NamedParameterJdbcTemplate namedParameterJdbcTemplate; 

    batch operation. T; User> / / / / / / new user list.add (New AppStudent (1, "Zhang San", 21)); list.add (New AppStudent (1, "Li Si", 22)); list.add (New AppStudent (1, "Wang two pock", 23)); P_student (class_id, name, age) VALUES (: classId, name, age) "; namedParameterJdbcTemplate.batchUpdate (SQL, beanSources);} finally we use it to compare the execution time before and after the concrete transformation.

     long start = System.currentTimeMillis (); / / / / / retrofit code, long end = System.currentTimeMillis (); System.out.println ("spend time:" + (end-start)); 

    is fast, and as far as how much, it is related to the amount of data and machine configuration.

    project source code:

    above is all of the content of this article, hope to help everyone's learning, and also hope that a lot of people support the script home.

    , you may be interested in the article:

    This concludes the body part

    This paper fixed link: | Script Home | +Copy Link

    Article reprint please specify:Sample code for SpringBoot JdbcTemplate batch operation | Script Home

    You may also be interested in these articles!