Archive for the ‘iBatis’ category

iBatis Tutorial Part 2 – SQLMapConfig.xml Basics

August 13th, 2008

This is where you need to provide all configurations for iBatis. Create an XML file with name SqlMapConfig.xml » Read more: iBatis Tutorial Part 2 – SQLMapConfig.xml Basics

IBatis vs Hibernate ? Which one to choose?

August 9th, 2008

A lot of people ask me when i give sessions on IBatis.Which is better ?IBatis or Hibernate? IBatis maps the Java Objects to the SQL Query’s results. Hibernate, maps Java Objects to DB tables (Object-Relational Mapping). Hibernate automatically generates all the SQL for you. In IBatis you can directly write queried in SQL, thus if you are a good SQL programmer, you would find learning Ibatis very easy. Thus Ibatis allows you to do anything what could be done through SQL. Thus you can virtually know what the affect of a query would be.

IBatis Tutorial – Sample Application Step 1

August 8th, 2008

Steps :

1) Creata a databas and a table

2) Create a package com.Employee

3) SQL mapper – Employee.xml
» Read more: IBatis Tutorial – Sample Application Step 1

Ibatis Tutorial – Sample Application Step 2

August 8th, 2008

Step 2: Create a package and a java object .



package com.Employee
import java.io.Serializable;
public class Employee implements Serializable {
private int employeeID;

» Read more: Ibatis Tutorial – Sample Application Step 2

Ibatis Tutorial – Sample Application Step 3

August 8th, 2008

Step 3: Create struts-config.xml

<?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>
<transactionManager type=”JDBC”>
<dataSource type=”SIMPLE”>
<property name=”JDBC.Driver” value=”com.mysql.jdbc.Driver”/>
<property name=”JDBC.ConnectionURL”
value=”jdbc:mysql://localhost:3306/employeedb”/>
<property name=”JDBC.Username” value=”root”/>
<property name=”JDBC.Password” value=”"/>
</dataSource>
</transactionManager>
<sqlMap resource=”com/Employee.xml”/>
</sqlMapConfig>

Also Read : IBatis vs Hibernate ? Which one to choose?
IBatis Tutorial – Sample Application Step 4
Introduction to IBatis Tutorial -Part 1

IBatis Tutorial – Sample Application Step 4

August 8th, 2008

Create Employee.xml

<?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>
<select id=“getEmployee” parameterClass=“int”
resultClass=“com.Contact”>
SELECT employeeID as employeeID,
firstName as firstName,
lastName as lastName,
email as email
from Employee
where employeeID=#value#
</select>
<select id=“getEmployees” resultClass=“com.Employee”>
SELECT employeeID as employeeID,
firstName as firstName,
lastName as lastName,
email as email
from Employee
</select>
</sqlMap>


Technorati :

Also Read : IBatis vs Hibernate ? Which one to choose?
IBatis Tutorial – Sample Application Step 5
Introduction to IBatis Tutorial -Part 1

Ibatis Tutorial – Sample Application Step 5

August 8th, 2008

Step 5 : Create a standalone java class to run the application .


import java.io.Reader;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.model.Employee;

public class QueryEmployee {

public static void main(String[] args) {
try {
String resource = “SqlMapConfig.xml”;

Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
Integer employeePrimaryKey = new Integer(1);
Employee employee =
(Employee)sqlMap.queryForObject(”getEmployee”,
employeePrimaryKey);
System.out.println(employee.getFirstName()
+ “\t” + employee.getLastName());
}catch(Exception e) {
e.printStackTrace();
}
}
}



Technorati :

Also Read : IBatis vs Hibernate ? Which one to choose?
IBatis Tutorial – Sample Application Step 1

IBatis Tutorial Part 3 – SQLMapConfig.xml optional settings

August 8th, 2008

Here are some of the options setting which are worth mentioning.


<settings

maxRequests ="120"

maxSessions ="20"

maxTransactions="10"

lazyLoadingEnabled =" true"

enhancementEnabled ="true"

cacheModelsEnabled ="true"

useStatementNamespaces ="false"

defaultStatementTimeout ="5" />

<typaAlias alias = ""employee> type ="com.Emloyee" />


Settings attributes : » Read more: IBatis Tutorial Part 3 – SQLMapConfig.xml optional settings

Introduction to IBatis Tutorial -Part 1

August 8th, 2008

This is a tutorial on IBatis . IBatis is a persistence framework which enables mapping SQL queries to POJOs (Plain Old Java Objects). The SQL queries are decoupled from an application by putting the queries in XML files. Mapping of the retrieved objects is automatic or semi-automatic.

IBatis couples objects with stored procedures or SQL statements using a XML descriptor.iBatis provides a set of APIs which can be used to call these statements written in an XML file. Also since IBatis gives you the power to write any SQL query in the XML file, there is nothing that IBatis cannot achieve, thus making it a very good persistence framework.IBatis is simple to learn and anyone who can write SQL queries can very well work on this. » Read more: Introduction to IBatis Tutorial -Part 1