Define Result Class : iBatis « J2EE « Java

Define Result Class


public class Account {

  private int id;
  private String firstName;
  private String lastName;
  private String emailAddress;

  public int getId() {
    return id;

  public void setId(int id) { = id;

  public String getFirstName() {
    return firstName;

  public void setFirstName(String firstName) {
    this.firstName = firstName;

  public String getLastName() {
    return lastName;

  public void setLastName(String lastName) {
    this.lastName = lastName;

  public String getEmailAddress() {
    return emailAddress;

  public void setEmailAddress(String emailAddress) {
    this.emailAddress = emailAddress;


File: Account.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap      
    PUBLIC "-// SQL Map 2.0//EN"      

<sqlMap namespace="Account">

  <typeAlias alias="Account" type="Account"/>

  <resultMap id="myResult" class="AccountResult">
    <result property="id" column="ACC_ID"/>
    <result property="firstName" column="ACC_FIRST_NAME"/>


  <select id="getByLike" resultClass="AccountResult" parameterClass="Account">
      select ACC_ID as id,
      ACC_FIRST_NAME as firstName
      from ACCOUNT
  where ACC_EMAIL  = #emailAddress# and  ACC_LAST_NAME = #lastName#

  <!-- Insert example, using the Account parameter class -->
  <insert id="insertAccount" parameterClass="Account">
    insert into ACCOUNT (
    )values (
      #id#, #firstName#, #lastName#, #emailAddress#


public class AccountResult {

  private int id;
  private String firstName;

  public int getId() {
    return id;

  public void setId(int id) { = id;

  public String getFirstName() {
    return firstName;

  public void setFirstName(String firstName) {
    this.firstName = firstName;


import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

public class Main {

  public static void main(String[] a) throws Exception {
    Util util = new Util();
        .executeSQLCommand("create table ACCOUNT(ACC_ID int, ACC_FIRST_NAME varchar,ACC_LAST_NAME varchar,ACC_EMAIL varchar);");
    util.executeSQLCommand("create table Message(Message_ID int, content varchar);");

    SqlMapClient sqlMapper = util.getSqlMapClient();

    Account account = new Account();

    sqlMapper.insert("insertAccount", account);
    util.checkData("select * from account");

    List list = sqlMapper.queryForList("getByLike", account);



File: SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig      
    PUBLIC "-// SQL Map Config 2.0//EN"      


  <!-- Configure a built-in transaction manager.  If you're using an 
       app server, you probably want to use its transaction manager 
       and a managed datasource -->
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="org.hsqldb.jdbcDriver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:hsqldb:data/tutorial"/>
      <property name="JDBC.Username" value="sa"/>
      <property name="JDBC.Password" value=""/>

  <!-- List the SQL Map XML files. They can be loaded from the 
       classpath, as they are here ( -->
  <sqlMap resource="Account.xml"/>



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class Util {
  Statement st;
  public Util() throws Exception{
    // Load the JDBC driver.
    System.out.println("Driver Loaded.");
    // Establish the connection to the database.
    String url = "jdbc:hsqldb:data/tutorial";

    Connection conn = DriverManager.getConnection(url, "sa", "");
    System.out.println("Got Connection.");
    st = conn.createStatement();
  public SqlMapClient getSqlMapClient() throws Exception{
    Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
    SqlMapClient sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
    return sqlMapper;
  public void executeSQLCommand(String sql) throws Exception {
  public void checkData(String sql) throws Exception {
    ResultSet rs = st.executeQuery(sql);
    ResultSetMetaData metadata = rs.getMetaData();

    for (int i = 0; i < metadata.getColumnCount(); i++) {
      System.out.print("\t"+ metadata.getColumnLabel(i + 1)); 

    while ( {
      for (int i = 0; i < metadata.getColumnCount(); i++) {
        Object value = rs.getObject(i + 1);
        if (value == null) {
          System.out.print("\t       ");
        } else {


       942 k)

Related examples in the same category

1.iBatis Development Environment Setup
2.Delete By ID
3.Get List Of Objects
4.Get Object From iBatis
5.Hard Code Constant In SQL
6.Insert Into Database
7.iBatis JPetStore 5.0
8.Set Cache Mode
9.String Parameter
10.Update Object
11.Use Map To Pass In Two String Parameter
12.Use Order By clause
13.User Object As Parameter
14.Use Like in where clause