Introduction to Ibatis by Rohit


Published on

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.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction to Ibatis by Rohit

  1. 1. IBatis<br />Get to know iBatis<br />Author: Rohit Prabhakar<br /><br />
  2. 2. Get to know iBatis from Rohit @<br />iBatis<br />Topics:<br /><ul><li>Introduction
  3. 3. Installation
  4. 4. Environment
  5. 5. Spring-iBatis Integration
  6. 6. SQL Mappings
  7. 7. Conclusion</li></li></ul><li>iBatis:Introduction<br />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. <br />Get to know iBatis from Rohit @<br />
  8. 8. iBatis: Installation<br />iBATISis delivered in a single JAR file: should be copied and made available at runtime via the application&apos;s class path.<br />This can be downloaded from iBatis homepage.<br />Get to know iBatis from Rohit @<br />
  9. 9. iBatis:Environment Configuration<br />Using Spring - iBATIS with a stand-alone application:<br /><ul><li>You should place the ibatis-x.x.x.x.jar in the application's classpath.
  10. 10. Define and place the following three configuration files in the classpath</li></ul>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). <br />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) <br />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). <br />Using Spring - iBATIS with a web application<br /><ul><li>Same as the above ,but just need one more xml (web.xml).</li></ul>Get to know iBatis from Rohit @<br />
  11. 11. Spring-iBatis Integration<br /><ul><li>Add to the iBatis jar to the project classpath
  12. 12. Create a package “com.test.ibatis”
  13. 13. Create a class “Student “</li></ul>package com.test.ibatis;<br />public class Student {<br />private long studentId;<br />private String firstName;<br />private String lastName;<br />private String address;<br />}<br /><ul><li>Generate Getters and Setters for all the items in the Bean</li></ul>Get to know iBatis from Rohit @<br />
  14. 14. Spring-iBatis Integration<br /><ul><li>Create a table “STUDENT” with a table-per-class relationship</li></ul>CREATE TABLE STUDENT(<br />STUDENT_ID NUMBER (6, 0) NOT NULL,<br />FIRST_NAME VARCHAR(50) NOT NULL,<br />LAST_NAME VARCHAR(50) ,<br />ADDRESS VARCHAR(200) NOT NULL,<br />PRIMARY KEY (STUDENT_ID)<br />)<br />Get to know iBatis from Rohit @<br />
  15. 15. Spring-iBatis Integration: applicationContext.xml<br />Add the following<br />&lt;bean id=&quot;dataSource&quot;<br />class=&quot;org.apache.commons.dbcp.BasicDataSource&quot;<br />destroy-method=&quot;close&quot;&gt;<br />&lt;property name=&quot;driverClassName&quot;<br />value=&quot;oracle.jdbc.driver.OracleDriver&quot; /&gt;<br />&lt;property name=&quot;url&quot;<br />value=&quot;jdbc:oracle:thin:@; /&gt;<br />&lt;property name=&quot;username&quot; value=&quot;scott&quot; /&gt;<br />&lt;property name=&quot;password&quot; value=&quot;tiger&quot; /&gt;<br />&lt;/bean&gt;<br />&lt;bean id=&quot;sqlMap&quot;<br />class=&quot;org.springframework.orm.ibatis.SqlMapClientFactoryBean&quot;&gt;<br />&lt;property name=&quot;configLocation&quot;&gt;<br />&lt;value&gt;classpath:SqlMapConfig.xml&lt;/value&gt;<br />&lt;/property&gt;<br />&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; /&gt;<br />&lt;/bean&gt;<br />&lt;bean id=&quot;studentService&quot;<br />class=&quot;com.test.ibatis.StudentServiceImpl&quot;&gt;<br />&lt;property name=&quot;sqlMapClient&quot; ref=&quot;sqlMap&quot; /&gt;<br />&lt;/bean&gt;<br />Get to know iBatis from Rohit @<br />
  16. 16. Spring-iBatis Integration<br />SqlMapConfig.xml<br />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.<br />&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;!DOCTYPE sqlMapConfig PUBLIC &quot;-// SQL MAP Config 2.0//EN&quot;<br /> &quot;;&gt;<br />&lt;sqlMapConfig&gt;<br />&lt;settings useStatementNamespaces=&quot;true&quot; /&gt;<br />&lt;sqlMap resource=&quot;StudentSQLMap.xml&quot; /&gt;<br />&lt;/sqlMapConfig&gt;<br />Get to know iBatis from Rohit @<br />
  17. 17. Spring-iBatis Integration<br />StudentSQLMap.xml<br />&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;!DOCTYPE sqlMap PUBLIC &quot;-// SQL MAP 2.0//EN&quot; <br />&quot;;&gt;<br />&lt;sqlMap namespace=&quot;student&quot;&gt;<br /> &lt;typeAlias alias=&quot;student&quot; type=&quot;com.test.ibatis.Student&quot;/&gt; <br /> &lt;resultMap id=&quot;studentResult&quot; class=&quot;Student&quot;&gt;<br /> &lt;result property=&quot;studentId&quot; column=&quot;STUDENT_ID&quot;/&gt; <br /> &lt;result property=&quot;firstName&quot; column=&quot;FIRST_NAME&quot;/&gt; <br /> &lt;result property=&quot;lastName&quot; column=&quot;LAST_NAME&quot;/&gt;<br /> &lt;result property=&quot;address&quot; column=&quot;ADDRESS&quot;/&gt;<br /> &lt;/resultMap&gt;<br /> &lt;select id=&quot;getTrade&quot; parameterClass=&quot;long&quot; resultMap=&quot;studentResult&quot;&gt;<br /> SELECT <br /> STUDENT_ID, <br /> FIRST_NAME, <br /> FIRST_NAME,<br /> ADDRESS<br /> FROM <br /> STUDENT <br /> WHERE <br /> STUDENT_ID = #id#<br /> &lt;/select&gt; <br />&lt;/sqlMap&gt;<br />Get to know iBatis from Rohit @<br />
  18. 18. Spring-iBatis Integration<br />Create an Interface StudentService<br />package com.test.ibatis;<br />public interface StudentService {<br />public Student getStudent(long studentId) throws Exception;<br />}<br />Get to know iBatis from Rohit @<br />
  19. 19. Spring-iBatis Integration<br />Create an Implementation Class<br />package com.test.ibatis;<br />import com.ibatis.sqlmap.client.SqlMapClient;<br />public class StudentServiceImpl implements StudentService {<br />protected SqlMapClientsqlMapclient=null;<br />public void setSqlMapclient(SqlMapClientsqlMapclient) {<br />this.sqlMapclient = sqlMapclient;<br />}<br />public Student getStudent(long studentId) throws Exception {<br /> return (Student) sqlMapclient.queryForObject(&quot;student.getStudent&quot;, studentId);<br />}<br />}<br />Get to know iBatis from Rohit @<br />
  20. 20. Spring-iBatis Integration: Student Client<br />package com.test.ibatis;<br />import org.springframework.context.ApplicationContext;<br />import;<br />public class StudentClient {<br />public static void Main(String[] args)<br />{<br />// load spring beans<br />ApplicationContextctx= new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);<br />StudentServicestudentService= (StudentService) ctx.getBean(&quot;studentService&quot;);<br />//get Object Student<br />intstudentId=1;<br />try {<br />System.out.println(&quot;**********************************&quot;);<br />System.out.println(studentService.getStudent(studentId));<br />System.out.println(&quot;**********************************&quot;);<br />} catch (Exception e) {e.printStackTrace();}<br />}<br />}<br />Get to know iBatis from Rohit @<br />
  21. 21. Spring-iBatis Integration: Execute<br />Run the Client and check what it prints?<br />Get to know iBatis from Rohit @<br />
  22. 22. iBatis: Tools<br />Ibator<br />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. <br />Get to know iBatis from Rohit @<br />