For quick learning, assume it as wrapper over JDBC.
It start with
1. Giving names to queries, passing parameters
<select id="getStudentRecord" parameterclass="StudentInformation" resultclass="StudentRecordDO">
</select>
1.1 We give name to student record fetching query as "getStudentRecord". So whenever we call getStudentRecord, we are actually calling this query.
1.2 Now we are passing studentId as parameter Here parameterClass="StudentInformation" means that Class studentInformation is passed as parameter and its member variable name will be used as parameter in this query. So studentId will be at StudentInformation->studentId.
1.3 Result are stored in resultClass i.e. StudentRecord may have member variables
StudentRecord->studentId
StudentRecord->name
StudentRecord->address
StudentRecord->joiningDate
These variable automatically get populated as result of query(Please not that order of students may be different but name should be same)
If names are different than we can use resultMap as
<resultmap id="MyMapId" class="MyClass">
<result property="MyClassMemberVariable column=" dbcolumn="">
</result>
only change will be instead of resultClass we will use resultMap
2. Setting all attributes in XML
2.1. We give alias to handle classes
<typealias alias="StudentRecord" type="com.helpdevelopers.iBatisExample.domain.StudentRecordDO">
so basically "com.helpdevelopers.iBatisExample.domain.StudentRecordDO" is the class used as resultClass in our named query(getStudentRecord).
2.2 We provide loose coupling and namespace for easily dividing queries.
Now wrapping all such named queries in XML files.
skeleton:
<sqlmap namespace="studentZone">
<!-- My Named queries will be placed here -->
</sqlmap>
Now assume that fileName is "studentInformation.xml".
2.3 We provide configuration details for JDBC use.
<sqlmapconfig>
<transactionmanager type="JDBC" commitrequired="true">
<datasource type="JDBC">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"> <!-- Driver Name -->
<property name="JDBC.ConnectionURL" value="jdbc:mysql://studentDBServer:3306/Student">
<property name="JDBC.Username" value="student"> <!-- username and password to access DB -->
<property name="JDBC.Password" value="student">
</datasource >
</transactionmanager >
<sqlmap resource="iBatisQueryFiles/studentInformation.xml">
</sqlmap>
</sqlmapconfig>
Here all parameters are already used for JDBC connection so driver, url, userName, password etc. are assumed to be know to user. Briefly driver is connector name to bridge JDBC-DB gap, url is link of active DB server, username and password are authentic credentials to access that DB.
See, we easily binded our file studentInformation.xml by providing relative path.
Save file as sqlMapper.xml.
That's all. Setup complete.
3. Binding all together.
Now using that iBatis instance..(Java example)
3.1. Download iBatis for Java
3.2. put iBatis jar file at your projects build path i.e. include it in your classpath when compiling.
3.3 Instantiate iBatis Mapper, have connection class as
Its singleton implementation, you can choose your.
public class DAOFactory{
private SqlMapClient sqlMapper=null;
Reader queryReader=null;
private static DAOFactory factory;
// method returns an instance of sqlmapclient
private DAOFactory()
{
try{
queryReader= Resources.getResourceAsReader("sqlMapper.xml");
//Giving path of sqlMapper.xml to start with
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(queryReader);
// executing chained XML's and collect all at sqlMapper
}
catch (Exception e) {
....... your exception code
}
}
public static DAOFactory getInstance()
{
if(factory==null)
{
factory=new DAOFactory(); //returning instance of Factory method
}
return factory;
}
public SqlMapClient getSQLMapper(){
//return the sqlmapper
return sqlMapper; // getting sqlMapper, whenever required.
}
}
That's all. Now calling would be
DAOFactory factory=DAOFactory.getInstance();
SqlMapper sqlMapper=factory.getSQLMapper();
sqlMapper.queryForObjects("getStudentRecord",studentInformation);
this will return null if student not found or object of StudentRecordDO
we can fetch list of records using
sqlMapper.queryForList(queryName,parameters);
We can also use update,delete, insert,select tag to define type of query operation like
<insert id="insertNewStaff" parameterclass="StaffInformation">
Your Insert Query..
</insert>
to call
sqlMapper.insert("insertNewStaff",StaffInformation).
4. Add-Ons:
4.1. Caching->iBatis provide database level caching. You can define cache-model as setting
cacheModelsEnabled="true", it also include many concepts including lazy loading and self pooling mechanism.
To use cache model we will use
cacheModel tag at XML.
4.2 Dyamic Queries:
iBatis is quite good in reducing your query efforts.
You can provide conditional tags, iterations and many more using iBatis.
iBatis for .net lovers at friendly blog