• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Introduction to Ibatis by Rohit
 

Introduction to Ibatis by Rohit

on

  • 3,517 views

iBATIS in other words is an additional layer of indirection between the classes and the tables allowing it in more flexibility in how classes and tables are mapped with out making any changes to the ...

iBATIS in other words is an additional layer of indirection between the classes and the tables allowing it in more flexibility in how classes and tables are mapped with out making any changes to the Data model and the Object model. The layer of indirection here is the SQL.

Statistics

Views

Total Views
3,517
Views on SlideShare
3,415
Embed Views
102

Actions

Likes
2
Downloads
84
Comments
0

6 Embeds 102

http://www.rohitprabhakar.com 65
http://www.slideshare.net 28
http://www.linkedin.com 5
http://rohitprabhakar.com 2
http://www.lmodules.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Introduction to Ibatis by Rohit Introduction to Ibatis by Rohit Presentation Transcript

    • IBatis
      Get to know iBatis
      Author: Rohit Prabhakar
      http://rohitprabhakar.com
    • Get to know iBatis from Rohit @ http://rohitprabhakar.com
      iBatis
      Topics:
      • Introduction
      • Installation
      • Environment
      • Spring-iBatis Integration
      • SQL Mappings
      • Conclusion
    • iBatis:Introduction
      iBATIS is a Data Persistence Framework. iBATIS in other words is an additional layer of indirection between the classes and the tables allowing it in more flexibility in how classes and tables are mapped with out making any changes to the Data model and the Object model. The layer of indirection here is the SQL. This enables mapping SQL queries to POJOs-Plain Old Java Objects. This works like the same as JPA[Hibernate/Spring framework] but faster than that. The main advantages are- 1. Reduces the amount of Java code to access a DataBase with the use of XML files containing SQL queries. 2.Faster than JPA. 3.Uses existing SQL Stored Procedures.
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • iBatis: Installation
      iBATISis delivered in a single JAR file: ibatis-version.build.jarand should be copied and made available at runtime via the application's class path.
      This can be downloaded from iBatis homepage.
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • iBatis:Environment Configuration
      Using Spring - iBATIS with a stand-alone application:
      • You should place the ibatis-x.x.x.x.jar in the application's classpath.
      • Define and place the following three configuration files in the classpath
      Spring config - This file defines the database connection parameters, the location of the SQL Map config file, and one or more Spring beans for use within the application. ( applicationContext.xml).
      SQL Map config - This file defines any iBATIS-specific configuration settings that you may need and declares the location for any SQL Map files that should be accessible through this config file. (SqlMapConfig.xml)
      SQL Map(s) One or more SQL Map files are declared in the SQL Map config and typically mapped to a single business entity within the application ,often represented by a single Java class (domainObject.xml).
      Using Spring - iBATIS with a web application
      • Same as the above ,but just need one more xml (web.xml).
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • Spring-iBatis Integration
      • Add to the iBatis jar to the project classpath
      • Create a package “com.test.ibatis”
      • Create a class “Student “
      package com.test.ibatis;
      public class Student {
      private long studentId;
      private String firstName;
      private String lastName;
      private String address;
      }
      • Generate Getters and Setters for all the items in the Bean
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • Spring-iBatis Integration
      • Create a table “STUDENT” with a table-per-class relationship
      CREATE TABLE STUDENT(
      STUDENT_ID NUMBER (6, 0) NOT NULL,
      FIRST_NAME VARCHAR(50) NOT NULL,
      LAST_NAME VARCHAR(50) ,
      ADDRESS VARCHAR(200) NOT NULL,
      PRIMARY KEY (STUDENT_ID)
      )
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • Spring-iBatis Integration: applicationContext.xml
      Add the following
      <bean id="dataSource"
      class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
      <property name="driverClassName"
      value="oracle.jdbc.driver.OracleDriver" />
      <property name="url"
      value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
      <property name="username" value="scott" />
      <property name="password" value="tiger" />
      </bean>
      <bean id="sqlMap"
      class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
      <property name="configLocation">
      <value>classpath:SqlMapConfig.xml</value>
      </property>
      <property name="dataSource" ref="dataSource" />
      </bean>
      <bean id="studentService"
      class="com.test.ibatis.StudentServiceImpl">
      <property name="sqlMapClient" ref="sqlMap" />
      </bean>
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • Spring-iBatis Integration
      SqlMapConfig.xml
      As for the configLocation property, it should be configured with the path to an XML file that enumerates the locations of the iBATIS SQL maps.
      <?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>
      <settings useStatementNamespaces="true" />
      <sqlMap resource="StudentSQLMap.xml" />
      </sqlMapConfig>
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • Spring-iBatis Integration
      StudentSQLMap.xml
      <?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 namespace="student">
      <typeAlias alias="student" type="com.test.ibatis.Student"/>
      <resultMap id="studentResult" class="Student">
      <result property="studentId" column="STUDENT_ID"/>
      <result property="firstName" column="FIRST_NAME"/>
      <result property="lastName" column="LAST_NAME"/>
      <result property="address" column="ADDRESS"/>
      </resultMap>
      <select id="getTrade" parameterClass="long" resultMap="studentResult">
      SELECT
      STUDENT_ID,
      FIRST_NAME,
      FIRST_NAME,
      ADDRESS
      FROM
      STUDENT
      WHERE
      STUDENT_ID = #id#
      </select>
      </sqlMap>
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • Spring-iBatis Integration
      Create an Interface StudentService
      package com.test.ibatis;
      public interface StudentService {
      public Student getStudent(long studentId) throws Exception;
      }
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • Spring-iBatis Integration
      Create an Implementation Class
      package com.test.ibatis;
      import com.ibatis.sqlmap.client.SqlMapClient;
      public class StudentServiceImpl implements StudentService {
      protected SqlMapClientsqlMapclient=null;
      public void setSqlMapclient(SqlMapClientsqlMapclient) {
      this.sqlMapclient = sqlMapclient;
      }
      public Student getStudent(long studentId) throws Exception {
      return (Student) sqlMapclient.queryForObject("student.getStudent", studentId);
      }
      }
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • Spring-iBatis Integration: Student Client
      package com.test.ibatis;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      public class StudentClient {
      public static void Main(String[] args)
      {
      // load spring beans
      ApplicationContextctx= new ClassPathXmlApplicationContext("applicationContext.xml");
      StudentServicestudentService= (StudentService) ctx.getBean("studentService");
      //get Object Student
      intstudentId=1;
      try {
      System.out.println("**********************************");
      System.out.println(studentService.getStudent(studentId));
      System.out.println("**********************************");
      } catch (Exception e) {e.printStackTrace();}
      }
      }
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • Spring-iBatis Integration: Execute
      Run the Client and check what it prints?
      Get to know iBatis from Rohit @ http://rohitprabhakar.com
    • iBatis: Tools
      Ibator
      Ibator is a code generator and Eclipse plugin. It can be used to get a head start on your project by generating your Beans, Mappings and SQL from an existing database schema.
      Get to know iBatis from Rohit @ http://rohitprabhakar.com