Saturday, 21 July 2012

iBatis Code FLow

JAR :  2.3.4

package mybatis;

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 IbatisRead
{
  public static void main(String[] args)
   throws IOException,SQLException{
   Reader rd = Resources.getResourceAsReader("mybatis\\SqlMapConfig.xml");
   SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);

  
    /* This would insert one record in Employee table. */
   System.out.println("Going to insert record.....");
   A_HARRY em1 = new A_HARRY("Farah", "Ali", 5000);

   smc.insert("ServiceRequestLog.insert", em1);

   System.out.println("Record Inserted Successfully ");

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

  
   /* This would read all records from the Employee table. */
   System.out.println("Going to read records.....");
   List <A_HARRY> ems = (List<A_HARRY>) smc.queryForList("ServiceRequestLog.getAll", null);
   A_HARRY em = null;
   for (A_HARRY 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("");
   }   
  
   A_HARRY rec = new A_HARRY();
   rec.setId(1);
   rec.setFirstName("Roma");
   smc.update("ServiceRequestLog.update", rec );
   System.out.println("Record updated Successfully ");

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



______________________


package mybatis;

public class A_HARRY {

    private int id;
    private String first_name;
    private String last_name;
    private int salary;

    public A_HARRY() {
    }

    public A_HARRY(String fname, String lname, int salary) {
        this.first_name = fname;
        this.last_name = lname;
        this.salary = salary;
    }


    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;
    }
}



_______________________________


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
     <settings useStatementNamespaces="true"/>
     <transactionManager type="JDBC">
        <dataSource type="SIMPLE">
           
          <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
          <property name="JDBC.ConnectionURL"
               value="jdbc:oracle:thin:@192.168.5.13:1521:veqa"/>
          <property name="JDBC.Username" value="een"/>
          <property name="JDBC.Password" value="en123"/>
        </dataSource>
      </transactionManager>
     <sqlMap resource="mybatis\ServiceRequestLog.xml"/>
</sqlMapConfig>





______________________________________




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="ServiceRequestLog">
   
    <select id="getAll" resultClass="mybatis.A_HARRY">
        SELECT * FROM A_HARRY
    </select>
   
    <insert id="insert" parameterClass="mybatis.A_HARRY">
        <selectKey keyProperty="id" resultClass="int">
            SELECT EXAMPLE_ID_SEQ.NEXTVAL as val from dual
        </selectKey>
        insert into A_HARRY(id,first_name, last_name, salary)
        values (#id#,#first_name#, #last_name#, #salary#)
    </insert>
   
    <update id="update" parameterClass="mybatis.A_HARRY">
        UPDATE A_HARRY
        SET    first_name = #first_name#
        WHERE  id = #id#
    </update>
   
   
   
</sqlMap>

No comments:

Post a Comment