Organizational Research By

Surprising Reserch Topic

Question:how to Installation iBATIS?


Here are the simple steps you would need to carry out to install iBATIS on your Linux machine:

  • Download latest version of iBATIS from Download iBATIS.

  • Unzip the downloaded file to extract .jar file from the bundle and keep it in appropriate lib directory.

  • Set PATH and CLASSPATH variables at the extracted .jar file(s) appropriately.

Here are the steps I carried out on my linux machine after downloading iBATIS binary file:

$ unzip ibatis-2.3.4.726.zip
  inflating: META-INF/MANIFEST.MF
   creating: doc/
   creating: lib/
   creating: simple_example/
   creating: simple_example/com/
   creating: simple_example/com/mydomain/
   creating: simple_example/com/mydomain/data/
   creating: simple_example/com/mydomain/domain/
   creating: src/
  inflating: doc/dev-javadoc.zip
  inflating: doc/user-javadoc.zip
  inflating: jar-dependencies.txt
  inflating: lib/ibatis-2.3.4.726.jar
  inflating: license.txt
  inflating: notice.txt
  inflating: release.txt
$pwd
/var/home/ibatis
$set PATH=$PATH:/var/home/ibatis/
$set CLASSPATH=$CLASSPATH:/var/home/ibatis\
                       /lib/ibatis-2.3.4.726.jar

asked Sep 13, 2013 in iBATIS by rajesh
edited Sep 12, 2013
0 votes
46 views



Related Hot Questions

2 Answers

0 votes
iBATIS Installation:

Here are the simple steps you would need to carry out to install iBATIS on your Linux machine:

    Download latest version of iBATIS from Download iBATIS.

    Unzip the downloaded file to extract .jar file from the bundle and keep it in appropriate lib directory.

    Set PATH and CLASSPATH variables at the extracted .jar file(s) appropriately.

Here are the steps I carried out on my linux machine after downloading iBATIS binary file:

$ unzip ibatis-2.3.4.726.zip
  inflating: META-INF/MANIFEST.MF
   creating: doc/
   creating: lib/
   creating: simple_example/
   creating: simple_example/com/
   creating: simple_example/com/mydomain/
   creating: simple_example/com/mydomain/data/
   creating: simple_example/com/mydomain/domain/
   creating: src/
  inflating: doc/dev-javadoc.zip
  inflating: doc/user-javadoc.zip
  inflating: jar-dependencies.txt
  inflating: lib/ibatis-2.3.4.726.jar
  inflating: license.txt
  inflating: notice.txt
  inflating: release.txt
$pwd
/var/home/ibatis
$set PATH=$PATH:/var/home/ibatis/
$set CLASSPATH=$CLASSPATH:/var/home/ibatis\
                       /lib/ibatis-2.3.4.726.jar

Create SqlMapConfig.xml

Consider the following:

    We are going to use JDBC to access the database testdb.

    JDBC driver for MySQL is "com.mysql.jdbc.Driver".

    Connection URL is "jdbc:mysql://localhost:3306/testdb".

    We would use username and password is "root" and "root".

    Our sql statement mappings for all the operations would be described in "Employee.xml".

Based on the above assumption we have to create an XML configuration file with name SqlMapConfig.xml with the following content. This is where you need to provide all configurations required for iBatis:

It is important that both the files SqlMapConfig.xml and Employee.xml should be present in the class path. For now we would keep Employee.xml file empty and we would conver its content in subsequent chapters.


PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"" rel="nofollow" target="_blank">http://ibatis.apache.org/dtd/sql-map-config-2.dtd">


     
     
        
                          value="com.mysql.jdbc.Driver"/>
                          value="jdbc:mysql://localhost:3306/testdb"/>
          
          
        

      

     


iBATIS Practical Exmaple:

This example would show you how you can UPDATE and READ records from a MySQL database table using iBATIS.

Consider we have following EMPLOYEE table in MySQL database:

CREATE TABLE EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

This table is having only one record as follows:

mysql> select * from EMPLOYEE;
+----+------------+-----------+--------+
| id | first_name | last_name | salary |
+----+------------+-----------+--------+
|  1 | Zara       | Ali       |   5000 |
+----+------------+-----------+--------+
1 row in set (0.00 sec)

Employee POJO Class:

Write the following Employee.java file:

public class Employee {
  private int id;
  private String first_name;
  private String last_name;   
  private int salary;  

  /* Define constructors for the Employee class. */
  public Employee() {}
 
  public Employee(String fname, String lname, int salary) {
    this.first_name = fname;
    this.last_name = lname;
    this.salary = salary;
  }

 /* Here are the required method definitions */
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return first_name;
  }
  public void setFirstName(String fname) {
    this.first_name = fname;
  }
  public String getLastName() {
    return last_name;
  }
  public void setlastName(String lname) {
    this.last_name = lname;
  }
  public int getSalary() {
    return salary;
  }
  public void setSalary(int salary) {
    this.salary = salary;
  }

 } /* End of Employee */

Employee.xml File:

To define SQL mapping statement using iBATIS, we would add and
   SELECT * FROM EMPLOYEE



   UPDATE EMPLOYEE
   SET    first_name = #first_name#
   WHERE  id = #id#




IbatisUpdate.java File:

This file would have application level logic to update and read records from the Employee table:

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class IbatisUpdate{
  public static void main(String[] args)
   throws IOException,SQLException{
   Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
   SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);

   /* This would update one record in Employee table. */
   System.out.println("Going to update record.....");
   Employee rec = new Employee();
   rec.setId(1);
   rec.setFirstName( "Roma");
   smc.update("Employee.update", rec );
   System.out.println("Record updated Successfully ");

   System.out.println("Going to read records.....");
   List ems = (List)
                  smc.queryForList("Employee.getAll", null);
   Employee em = null;
   for (Employee e : ems) {
      System.out.print("  " + e.getId());
      System.out.print("  " + e.getFirstName());
      System.out.print("  " + e.getLastName());
      System.out.print("  " + e.getSalary());
      em = e;
      System.out.println("");
   }    

   System.out.println("Records Read Successfully ");

  }
}

Compilation and Run:

Here are the steps to compile and run the above mentioned software. Make sure you have set PATH and CLASSPATH appropriately before proceeding for the compilation and execution.

    Create SqlMapConfig.xml as shown above.

    Create Employee.xml as shown above.

    Create Employee.java as shown above and compile it.

    Create IbatisUpdate.java as shown above and compile it.

    Execute IbatisUpdate binary to run the program.

You would get following result, and a record would be updated in EMPLOYEE table and later same record would be read from the EMPLOYEE table.

Going to update record.....
Record updated Successfully
Going to read records.....
  1  Roma  Ali  5000
Records Read Successfully
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
iBATIS Installation:

Here are the simple steps you would need to carry out to install iBATIS on your Linux machine:

    Download latest version of iBATIS from Download iBATIS.

    Unzip the downloaded file to extract .jar file from the bundle and keep it in appropriate lib directory.

    Set PATH and CLASSPATH variables at the extracted .jar file(s) appropriately.

Here are the steps I carried out on my linux machine after downloading iBATIS binary file:

$ unzip ibatis-2.3.4.726.zip
  inflating: META-INF/MANIFEST.MF
   creating: doc/
   creating: lib/
   creating: simple_example/
   creating: simple_example/com/
   creating: simple_example/com/mydomain/
   creating: simple_example/com/mydomain/data/
   creating: simple_example/com/mydomain/domain/
   creating: src/
  inflating: doc/dev-javadoc.zip
  inflating: doc/user-javadoc.zip
  inflating: jar-dependencies.txt
  inflating: lib/ibatis-2.3.4.726.jar
  inflating: license.txt
  inflating: notice.txt
  inflating: release.txt
$pwd
/var/home/ibatis
$set PATH=$PATH:/var/home/ibatis/
$set CLASSPATH=$CLASSPATH:/var/home/ibatis\
                       /lib/ibatis-2.3.4.726.jar

Create SqlMapConfig.xml

Consider the following:

    We are going to use JDBC to access the database testdb.

    JDBC driver for MySQL is "com.mysql.jdbc.Driver".

    Connection URL is "jdbc:mysql://localhost:3306/testdb".

    We would use username and password is "root" and "root".

    Our sql statement mappings for all the operations would be described in "Employee.xml".

Based on the above assumption we have to create an XML configuration file with name SqlMapConfig.xml with the following content. This is where you need to provide all configurations required for iBatis:

It is important that both the files SqlMapConfig.xml and Employee.xml should be present in the class path. For now we would keep Employee.xml file empty and we would conver its content in subsequent chapters.


PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"" rel="nofollow" target="_blank">http://ibatis.apache.org/dtd/sql-map-config-2.dtd">


     
     
        
                          value="com.mysql.jdbc.Driver"/>
                          value="jdbc:mysql://localhost:3306/testdb"/>
          
          
        

      

     


iBATIS Practical Exmaple:

This example would show you how you can UPDATE and READ records from a MySQL database table using iBATIS.

Consider we have following EMPLOYEE table in MySQL database:

CREATE TABLE EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

This table is having only one record as follows:

mysql> select * from EMPLOYEE;
+----+------------+-----------+--------+
| id | first_name | last_name | salary |
+----+------------+-----------+--------+
|  1 | Zara       | Ali       |   5000 |
+----+------------+-----------+--------+
1 row in set (0.00 sec)

Employee POJO Class:

Write the following Employee.java file:

public class Employee {
  private int id;
  private String first_name;
  private String last_name;   
  private int salary;  

  /* Define constructors for the Employee class. */
  public Employee() {}
 
  public Employee(String fname, String lname, int salary) {
    this.first_name = fname;
    this.last_name = lname;
    this.salary = salary;
  }

 /* Here are the required method definitions */
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return first_name;
  }
  public void setFirstName(String fname) {
    this.first_name = fname;
  }
  public String getLastName() {
    return last_name;
  }
  public void setlastName(String lname) {
    this.last_name = lname;
  }
  public int getSalary() {
    return salary;
  }
  public void setSalary(int salary) {
    this.salary = salary;
  }

 } /* End of Employee */

Employee.xml File:

To define SQL mapping statement using iBATIS, we would add and
   SELECT * FROM EMPLOYEE



   UPDATE EMPLOYEE
   SET    first_name = #first_name#
   WHERE  id = #id#




IbatisUpdate.java File:

This file would have application level logic to update and read records from the Employee table:

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class IbatisUpdate{
  public static void main(String[] args)
   throws IOException,SQLException{
   Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
   SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);

   /* This would update one record in Employee table. */
   System.out.println("Going to update record.....");
   Employee rec = new Employee();
   rec.setId(1);
   rec.setFirstName( "Roma");
   smc.update("Employee.update", rec );
   System.out.println("Record updated Successfully ");

   System.out.println("Going to read records.....");
   List ems = (List)
                  smc.queryForList("Employee.getAll", null);
   Employee em = null;
   for (Employee e : ems) {
      System.out.print("  " + e.getId());
      System.out.print("  " + e.getFirstName());
      System.out.print("  " + e.getLastName());
      System.out.print("  " + e.getSalary());
      em = e;
      System.out.println("");
   }    

   System.out.println("Records Read Successfully ");

  }
}

Compilation and Run:

Here are the steps to compile and run the above mentioned software. Make sure you have set PATH and CLASSPATH appropriately before proceeding for the compilation and execution.

    Create SqlMapConfig.xml as shown above.

    Create Employee.xml as shown above.

    Create Employee.java as shown above and compile it.

    Create IbatisUpdate.java as shown above and compile it.

    Execute IbatisUpdate binary to run the program.

You would get following result, and a record would be updated in EMPLOYEE table and later same record would be read from the EMPLOYEE table.

Going to update record.....
Record updated Successfully
Going to read records.....
  1  Roma  Ali  5000
Records Read Successfully
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...