Sql.xml of bulk data upload

Source: Internet
Author: User

<!--User.xml--
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<!--namespace namespace, the role is to classify the SQL management, can be understood as SQL isolation
Note: Using the Mapper proxy method development, namespace has a special important role, namespace to be consistent with the interface name
-
<mapper namespace= "Test" >
<!--Configure many SQL statements in the mapping file--
<!--performing database queries with SELECT
ID: Identifies the SQL in the mapping file, encapsulates the SQL statement into the Mappedstatement object, so called the statement ID
In the Mapper proxy development method, the ID should be consistent with the method name of the interface
#{} represents a placeholder
ParameterType specifying the type of input parameters
#{id}: Where the ID represents the input parameter, the parameter name is the ID, and if the input parameter is a simple type, the parameter name in #{} can be arbitrarily
RESULTTYPE Specifies the type of Java object to which the output results are mapped, and the automatically generated file uses the Resultmap specified
-
<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 = 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 configuration file stream
InputStream inputstream = null;
Sqlsession sqlsession = null;
try {
InputStream = Resources.getresourceasstream (Resource);
To create a session factory
Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);
Get sqlsession through the factory
sqlsession = Sqlsessionfactory.opensession ();
manipulating databases with sqlsession
The first parameter: the ID in the statement in the mapping file, equal to Namespace+ "." ID of the +statement
Second parameter: specifying and matching parameters of the ParameterType type in the mapping file
Sqlsession.selectone result is an object of type Resulttype that matches the mapping 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 {
Freeing resources
Sqlsession.close ();
}
}

}

Sql.xml of bulk data upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.