SQL. xml for Batch Data uploading and SQL. xml for uploading
<! -- User. xml -->
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd ">
<! -- Namespace is used to classify and manage SQL, which can be understood as SQL isolation.
Note: using the mapper proxy method for development, namespace plays a special role. namespace must be consistent with the interface name.
-->
<Mapper namespace = "test">
<! -- Configure many SQL statements in the ing file -->
<! -- Execute database query Through select
Id: identifies the SQL statement in the ing file and encapsulates the SQL statement into the mappedStatement object. It is called the statement id.
In the mapper proxy development method, the id must be consistent with the method name of the interface.
# {} Represents a placeholder
ParameterType specifies the type of the input parameter
# {Id}: The id indicates the input parameter. The parameter name is id. If the input parameter is of simple type, the parameter name in # {} can be any
ResultType specifies the java object type mapped to the output result. The automatically generated file uses the resultMap parameter.
-->
<Insert id = "addUserBatch" useGeneratedKeys = "true" parameterType = "java. util. List">
Insert into user (id, first_name)
Values
<Foreach collection = "list" item = "item" index = "index" separator = ",">
(# {Item. id}, # {item. username })
</Foreach>
</Insert>
</Mapper>
<! -- User. java -->
Package entity;
Import java. util. Date;
Public class User {
Private Integer id;
Private String username;
Public Integer getId (){
Return id;
}
Public void setId (Integer id ){
This. id = id;
}
Public String getUsername (){
Return username;
}
Public void setUsername (String username ){
This. username = null? Null: username. trim ();
}
}
<! -- InsertTest. java -->
Package entity;
Import java. io. InputStream;
Import java. util. ArrayList;
Import java. util. List;
Import org. apache. ibatis. io. Resources;
Import org. apache. ibatis. session. SqlSession;
Import org. apache. ibatis. session. SqlSessionFactory;
Import org. apache. ibatis. session. SqlSessionFactoryBuilder;
Import org. apache. poi. hssf. record. UserSViewBegin;
Public class InsertTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// Mybatis configuration file
String resource = "SqlMapConfig. xml ";
// Get the configuration file stream
InputStream inputStream = null;
SqlSession sqlSession = null;
Try {
InputStream = Resources. getResourceAsStream (resource );
// Create a session Factory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). build (inputStream );
// Obtain SqlSession through the factory
SqlSession = sqlSessionFactory. openSession ();
// Operate the database through SqlSession
// The first parameter: id in the statement in the ing file, which is equal to the namespace + "." + statement id
// Second parameter: Specify the parameterType parameter that matches the ing File
// SqlSession. selectOne is the resultType object that matches the ing file.
User user = new User ();
User. setId (2 );
User. setUsername ("test ");
User user2 = new User ();
User2.setId (3 );
User2.setUsername ("test2 ");
List <User> users = new ArrayList <User> ();
Users. add (user );
Users. add (user2 );
SqlSession. insert ("test. addUserBatch", users );
SqlSession. commit ();
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
// Release resources
SqlSession. close ();
}
}
}