5. IBATIS.NET Developer Guide
5.1. Installing the Data Mapper for
.NET
5.1.1. Setup the Distribution
You
can download the latest version of IBATIS.NET from the below official site:
http://code.google.com/p/mybatisnet/downloads/list
I'm
going to download the IBatis.DataMapper.1.6.2.bin.zip
List
of file found under the distribution's source folder of
IBatis.DataMapper.1.6.2.bin.zip
Folder
name
|
Description
|
Castle.DynamicProxy
|
Creating Proxy
|
IBatisNet.Common
|
Assembly of classes
shared by DataAccess and DataMapper
|
IBatisNet.Common.Logging.Log4Net
|
Log4Net factory adapter
classes
|
iBatisNet.DataMapper
|
The DataMapper framework
|
Log4Net
|
Log4Net factory adapter
Base classes
|
providers.config
|
The database provider
definition file (XML Configuration file)
|
sample.SqlMap.config
|
The Data Mapper
configuration file (XML Configuration file)
|
SqlMap.xml
|
The Data Map definition
file (XML Schema File)
|
5.1.2. Add Assembly References
Add
references to the following files
1. Castle.DynamicProxy.dll
2. iBatisNet.DataMapper.dll
3.
iBatisNet.Common.dll
IBatis.NET
references
5.1.3. Add XML File Items
Add
following xml files to your application
1. providers.config
2. sample.SqlMap.config
3. SqlMap.xml
IBatis.NET
XML files
5.2. Provider Configuration File
I
want to connect my application to SQL Server 2005, so removed all the other
provider section and maintain "sqlServer2005" provider section alone.
Ensure
that the enabled attribute is set to true; this is set to false by default in
the sample installation files.
Provider
Configuration
5.3. Data Mapper Configuration File
Create
a copy of sample.SqlMap.config file and rename it with sqlMap.config
5.3.1. The Element
Name
attribute from the "provider.config"
file to be defined here based on any one of the database (SQL Server, Oracle, Access, and MySql).
Example:
5.3.2. The
element
The
element specifies ODBC datasource or connection string for
any one database (SQL Server, Oracle, Access, and MySql).
5.3.3. The Element
Example:
5.4. Create a map
Add
another XML file to the root of your project called UsersMap.xml.
5.5. Building a SqlMapper Instance
The
framework provides service methods that you can call which read the
configuration file (and any of its definition files) and builds a SqlMapper
object. The SqlMapper object provides access to the rest of the framework. The
SqlMapper is designed to be multi-threaded and long-lived, and so makes for a
good singleton.
Code:
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
namespace DataMapper
{
public class Mapper
{
private static volatile ISqlMapper
_mapper = null;
protected static void Configure(object
obj)
{
_mapper
= null;
}
protected static void InitMapper()
{
ConfigureHandler handler = new ConfigureHandler(Configure);
DomSqlMapBuilder builder = new DomSqlMapBuilder();
_mapper
= builder.ConfigureAndWatch(handler);
}
public static ISqlMapper Instance()
{
if (_mapper == null)
{
lock
(typeof(SqlMapper))
{
if
(_mapper == null) //
double-check
{
InitMapper();
}
}
}
return _mapper;
}
public static ISqlMapper Get()
{
return Instance();
}
}
}
5.6. Code to execute IBATIS.Net
Code:
The
DataLayer looks like below
using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using IBatisNet.Common;
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using DataMapper;
namespace IBATIS
{
public class DataLayer
{
public static IList<PVCSummary>
PVCReport(string XMLPARAM, int PAGENUMBER, string
PAGING)
{
parameters pm = new
parameters();
pm.XMLPARAM
= XMLPARAM;
pm.PAGENUMBER
= PAGENUMBER;
pm.PAGING
= PAGING;
IList<PLANNEDVSCOMPLETED>
objPvCReport = DataMapper.Mapper.Instance()
.QueryForList<PLANNEDVSCOMPLETED>("PVCReport", pm);
return objPvCReport;
}
}
public class parameters
{
public virtual string XMLPARAM { get;
set; }
public virtual int PAGENUMBER { get;
set; }
public virtual string PAGING { get; set; }
}
The
codebehind of aspx page as shown below
protected void
Page_Load(object sender, EventArgs e)
{
string Param = ""; //xml parameter
IList<PLANNEDVSCOMPLETED>
objReport = DataLayer.PVCReport(Param, 1,
"Y");
lstvw.DataSource
= objReport; //assign result
object directly to listview datasource
lstvw.DataBind()
}