JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven









Example:- A simple iBATIS Application with Eclipse.
  • Create  New Project


  • Select Java Project


  • Give it any name we named JavaIBATISApplication





  • Add iBATIS Neccessory Jar files.












  • Create a clas file in package mypack  named Employee as given below
package mypack;

public class Employee {
  private int id;
  private String name;

  public Employee() {

  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

  • Create another class file in utility package anmed Ibatis as given below.
package utility;
import java.io.IOException;
import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.sqlmap.engine.mapping.sql.Sql;

public class Ibatis {
  
private static SqlMapClient sqlMap = null;
  
public static SqlMapClient getSqlMap()
throws IOException {

    if (sqlMap == null) {

      Reader reader = Resources.
getResourceAsReader
("sqlMapconfig.xml");
      System.out.println("sqlMap Config file is found");
      sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
      System.out.println("iBATIS Initialized");
      
    }
    return sqlMap;
    }
 

  • Create  sqlMap file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap>
  <select id="getEmployees" resultClass="mypack.Employee">
<![CDATA[
SELECT emp_id as id, emp_name as name
 FROM employee
 ]]>
  </select>
</sqlMap>

  • Create sqlConfig file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
  <transactionManager type="JDBC">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver"
value=
"oracle.jdbc.driver.OracleDriver" />
      <property name="JDBC.ConnectionURL"
 
value="jdbc:oracle:thin:@localhost:1521:XE" />
      <property name="JDBC.Username" value="system" />
      <property name="JDBC.Password" value="manager" />
    </dataSource>
  </transactionManager>
  <sqlMap resource="sqlMap.xml" />
</sqlMapConfig>
  • Create Client program in mypackage named Program as given below.
package mypack;

import utility.Ibatis;

import java.io.IOException;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;

public class Program {

  public static void main(String[] args)
 
throws Exception {

    SqlMapClient sqlMap = Ibatis.getSqlMap();
    List<Employee> list = sqlMap.queryForList
("getEmployees"null);

    for (Employee e : list) {
      System.out.println(e.getName());
    }
  }
}

Directory Structure



Output:-Run the Program class.


Download Eclipse Zip Code