tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 iBATIS > Basics > SelectKey Type

SelectKey Type 

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. Instead of manually ordering the queries we can use select key type attribute to specify the order of query execution. The possible values are pre and post.

File Name  :  
/IBATIS001/config/basic/SelectKeyType.xml 

File Name  :  
com/bethecoder/tutorials/ibatis/tests/basic/SelectKeyType.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
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 SelectKeyType {

  /**
   @param args
   @throws IOException 
   @throws SQLException 
   */
  public static void main(String[] argsthrows IOException, SQLException {

    Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
    SqlMapClient sqlMapClent = SqlMapClientBuilder.buildSqlMapClient(reader);

    Student student = new Student("Prasad""Kasireddi"(short)29"+914444444444""Singing");
    sqlMapClent.insert("Student.selKeyType", student);
    
    System.out.println(student + " record created successfully..");
  }
}
   

It gives the following output,
Student[7, Prasad, Kasireddi, 29, Singing, +914444444444] record created successfully..



 
  


  
bl  br