Write the code below, and I hope you will criticize and correct it.
First, the domain object. The annotation method used here is a new version.
User. java
Copy codeThe Code is as follows:
Package com. bao. sample. s3h4. domain;
Import javax. persistence. Column;
Import javax. persistence. Entity;
Import javax. persistence. GeneratedValue;
Import javax. persistence. GenerationType;
Import javax. persistence. Id;
Import javax. persistence. Table;
Import com. bao. sample. base. domain. BaseDomain;
@ Entity
@ Table (name = "t_user ")
Public class User extends BaseDomain {
Private static final long serialVersionUID = 1L;
Private int id;
Private String username;
Private String password;
/**
* @ Description annotation is best marked on the get method. note: a consistent tag is used. The annotation is based on the Id tag. If it is marked on the get method, the Annotation on property is ignored.
* @ Return
*/
@ Id
@ GeneratedValue (strategy = GenerationType. IDENTITY)
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
@ Column (nullable = false)
Public String getUsername (){
Return username;
}
Public void setUsername (String username ){
This. username = username;
}
@ Column (nullable = false)
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}
Public User (){
Super ();
}
Public User (int id, String username, String password ){
Super ();
This. id = id;
This. username = username;
This. password = password;
}
}
The Dao interface inherits a BaseDao interface.
Copy codeThe Code is as follows:
Package com. bao. sample. s3h4. dao;
Import java. util. List;
Import com. bao. sample. base. dao. BaseDao;
Import com. bao. sample. s3h4. domain. User;
Public interface UserBatchDao extends BaseDao <User> {
/**
* @ Description add operations in batches
* @ Return-1: operation failed; 0: Execution normal;> 0: Number of successful executions
*/
Public int batchAddUsingJdbc (List <User> users );
Public int batchAddUsingHibernate (List <User> users );
Public int batchAddUsingJdbcTemplate (List <User> users );
}
Implementation of UserBatchDao:
Copy codeThe Code is as follows:
UserBatchDaoImpl
Package com. bao. sample. s3h4. dao;
Import java. SQL. Connection;
Import java. SQL. PreparedStatement;
Import java. SQL. SQLException;
Import java. util. List;
Import javax. annotation. Resource;
Import org. hibernate. Session;
Import org. springframework. jdbc. core. BatchPreparedStatementSetter;
Import org. springframework. jdbc. core. JdbcTemplate;
Import org. springframework. orm. hibernate4.SessionFactoryUtils;
Import org. springframework. stereotype. Repository;
Import org. springframework. transaction. annotation. Transactional;
Import com. bao. sample. base. dao. BaseDaoImpl;
Import com. bao. sample. s3h4. domain. User;
/**
*
* @ Description three methods are added in batch. The execution efficiency is jdbc, jdbcTemplate, and hibernate. <br/> jdbc and jdbcTemplate have similar execution efficiency. However, jdbcTemplate can be controlled using transaction annotations, so it is preferred.
* @ Author Bob hehe198504@126.com.
* @ Date 2012-8-13
*/
@ Repository ("userBatchDao ")
Public class UserBatchDaoImpl extends BaseDaoImpl <User> implements UserBatchDao {
@ Resource
Protected JdbcTemplate jdbcTemplate;
/**
* Execute 10 million records, which takes about 15188 ms
*/
@ Override
Public int batchAddUsingJdbc (List <User> users ){
Int result = 0;
Connection conn = null;
PreparedStatement pstmt = null;
String SQL = "insert into t_user (username, password) values (?,?) ";
Try {
Conn = SessionFactoryUtils. getDataSource (sessionFactory). getConnection ();
Conn. setAutoCommit (false );
Pstmt = conn. prepareStatement (SQL );
For (int I = 0; I <users. size (); I ++ ){
Int j = 1;
Pstmt. setString (j ++, users. get (I). getUsername ());
Pstmt. setString (j ++, users. get (I). getPassword ());
Pstmt. addBatch ();
}
Pstmt.exe cuteBatch ();
Conn. commit ();
Conn. setAutoCommit (true );
} Catch (SQLException e ){
If (conn! = Null ){
Try {
Conn. rollback ();
} Catch (SQLException e1 ){
E1.printStackTrace ();
}
}
} Finally {
If (pstmt! = Null ){
Try {
Pstmt. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
If (conn! = Null ){
Try {
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
Return result;
}
/**
* Execute 10 million records, which takes about 131203 ms, roughly 10 times that of jdbc or jdbcTemplate.
*/
@ Override
// @ Transactional (noRollbackFor = RuntimeException. class)
@ Transactional
Public int batchAddUsingHibernate (List <User> users ){
Session session = this. getSession ();
For (int I = 0; I <users. size (); I ++ ){
Session. save (users. get (I ));
// After 20 entries are added, the data is forcibly imported into the database.
// Clear () clear the cache
// The isolation level of the ipvs database is committed Read (Read committed ),
// After the flush operation, the data cannot be seen. The data can only be seen after the commit operation,
// If it fails, rollback and the preceding flush data will not be stored into the database
If (I % 20 = 0 ){
Session. flush ();
Session. clear ();
}
}
Return 0;
}
/**
* Execute 10 million records, which takes about 15671 ms
*/
// @ Transactional (noRollbackFor = RuntimeException. class)
@ Transactional
Public int batchAddUsingJdbcTemplate (List <User> users ){
String SQL = "insert into t_user (username, password) values (?,?) ";
Final List <User> tempUsers = users;
Final int count = users. size ();
BatchPreparedStatementSetter pss = new BatchPreparedStatementSetter (){
// Set parameters for prepared statement. The number of times this method will be called throughout the process
Public void setValues (PreparedStatement pstmt, int I) throws SQLException {
Int j = 1;
Pstmt. setString (j ++, tempUsers. get (I). getUsername ());
Pstmt. setString (j ++, tempUsers. get (I). getPassword ());
}
// Return the number of updated result sets
Public int getBatchSize (){
Return count;
}
};
JdbcTemplate. batchUpdate (SQL, pss );
Return 0;
}
Public JdbcTemplate getJdbcTemplate (){
Return jdbcTemplate;
}
}
The peripheral framework is not attached. If you need it, you can leave a message. I will provide package download.
Author: Yu Xuan