introduction to ibatis by rohit
DESCRIPTION
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.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
iBATIS is delivered in a single JAR file: ibatis-version.build.jar and 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
1. 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).
2. 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)
3. 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 IntegrationStudentSQLMap.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 Classpackage com.test.ibatis;
import com.ibatis.sqlmap.client.SqlMapClient;
public class StudentServiceImpl implements StudentService {
protected SqlMapClient sqlMapclient=null;
public void setSqlMapclient(SqlMapClient sqlMapclient) {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
ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService= (StudentService) ctx.getBean("studentService");
//get Object Student
int studentId=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