Configuration
details for the iBatis are mentioned in the SqlMapConfig.xml
file.
Before explaining the configuration file I
will like to mention the following:
Ø Database will be accessed using
JDBC.
Ø Namespace prefixes will be used
when referring to "mapped statements" in our Java code.
Ø All the mappings are done through
the files Employee_SqlMap.xml and Department_SqlMap.xml.
Ø We
will use SqlMapConfig.properties
file to write all the database properties so that configuration file can be
used independent of database.
<?
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>
<properties
resource="com/college/config/SqlMapConfig.properties"/>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver"
value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username"
value="${username}"/>
<property name="JDBC.Password"
value="${password}"/>
</dataSource>
</transactionManager> # any xml file created for mapping needs to
be added here
<sqlMap
resource=“employee_SqlMap.xml"/>
<sqlMap
resource=“department_SqlMap.xml"/>
</sqlMapConfig>
Mapped
statements give ids to SQL statements. These ids are referenced in the code
that invokes those using methods in the org.ibatis.sqlmap.client.SqlMapClient
interface such as queryForObject
and queryForList.
Because we specified that we wanted to "useStatementNamespaces"
in our configuration file, our references to statements will have the
syntax "{namespace}.{statement-id}".
Mapped statements are defined in XML files
that are referenced from the SqlMapConfig.xml file To use “statementNamespaces”
All the SQL Maps mentioned in Configuration
file will be compiled into one file after code compiles.
Employee_SqlMap.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 namespace=“emp">
<parameterMap id=“emp.delEmpParams" class="map">
<parameter property=“empName" jdbcType="VARCHAR"
mode="IN"/>
<parameter property="retSts" jdbcType="VARCHAR”
mode="OUT"/>
</parameterMap>
<procedure id=“emp.delemp"
parameterMap=“emp.delEmpParams">
{ call Employee.delEmp(?,?) }
</procedure>
<parameterMap id=“emp.addEmpParams"
class="map">
<parameter property=“empName" jdbcType="VARCHAR"
mode="IN"/>
<parameter property="retSts" jdbcType="VARCHAR"
mode="OUT"/>
</parameterMap>
<procedure id=“emp.addEmp"
parameterMap=“emp.addEmpParams">
{call Employee.addEmployee(?,?) }
</procedure>
<resultMap id=“emp.selEmpResults" class="com.bean.employee">
<result
property="id" column="id"/>
<result
property="name" column="name"/>
</resultMap>
<parameterMap id=“emp.selAllEmpParams"
class="map">
<parameter property=“empList" jdbcType="ORACLECURSOR"
mode="OUT"/>
</parameterMap>
<procedure id=“emp.selAllEmp"
parameterMap=“emp.selAllEmpParams“
resultMap=“emp.selEmpResults">
{call Employee.selAllEmp(?) }
</procedure>
<parameterMap id=“emp.selEmpByDeptParams"
class="map">
<parameter property=“DeptName" jdbcType="INTEGER"
mode="IN"/>
<parameter property=“empList" jdbcType="ORACLECURSOR"
mode="OUT"/>
</parameterMap>
<procedure id=“emp.selEmpByDept"
parameterMap=" emp.selEmpByDeptParams"
resultMap=" emp.selEmpResults">
{call Employee.selEmpByDept(?,?) }
</procedure>
</sqlMap>
Department_SqlMap.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 namespace=“dept">
<resultMap id=“dept.selDeptResults" class="com.bean.department">
<result
property="id" column="id"/>
<result
property="name" column="name"/>
</resultMap>
<parameterMap id=“dept.selEmpByDeptParams"
class="map">
<parameter property=“EmptName" jdbcType=“VARCHAR2"
mode="IN"/>
<parameter property=“deptName" jdbcType=“VARCHAR2"
mode="OUT"/>
</parameterMap>
<procedure id=“dept.selDeptforEmp"
parameterMap="dept.selEmpByDeptParams"
resultMap=" dept.selDeptResults ">
{call Employee.selEmpByDept(?,?) }
</procedure>
</sqlMap>
Call to SqlMaps
•import com.ibatis.dao.client.DaoManager;
•import com.ibatis.dao.client.template.SqlMapDaoTemplate;
private void delEmp(EmpName) throws SQLException {
HashMap params = new HashMap();
params.add(“EmpName”, EmpName);
queryForObject(“emp.delEmp”,params);
String returnStatus = new String("");
returnStatus = (String)params.get("retSts");
}
private void addEmployee(String empName, String deptName)
throws SQLException {
HashMap params = new HashMap();
Params.put(“name”, empName);
Params.put(“Emp_id”, deptName);
queryForObject(“emp.addEmp”,params);
String returnStatus = new String("");
returnStatus = (String)params.get("retSts");
}
private List getEmp() throws SQLException {
HashMap params = new HashMap();
List EmpList = queryForList(“emp.selAllEmp”,params);
return EmpList;
}
.....Will be Continued