Create Record
The following example shows inserting a student record into DB.
The selectKey tag allows us to generate a primary key.
Depending upon the database and primary key generation strategy
the selectKey tag has to be specified before or
after the insert query.
package com.bethecoder.tutorials.ibatis.tests.basic;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import com.bethecoder.tutorials.ibatis.common.Student;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class Insert {
/**
* @param args
* @throws IOException
* @throws SQLException
*/
public static void main ( String [] args ) throws IOException, SQLException {
Reader reader = Resources.getResourceAsReader ( "SqlMapConfig.xml" ) ;
SqlMapClient sqlMapClent = SqlMapClientBuilder.buildSqlMapClient ( reader ) ;
Student student = new Student ( "Sriram" , "Kasireddi" , ( short ) 1 , "+919999999999" , "Boxing" ) ;
sqlMapClent.insert ( "Student.insert" , student ) ;
System.out.println ( student + " record created successfully.." ) ;
}
}
It gives the following output,
Student[6, Sriram, Kasireddi, 1, Boxing, +919999999999] record created successfully..