2. 创建测试数据库,并在数据库中创建一个Person 表,其中包含三个字段:
NameTypeSize
PER_IDLongInteger4 not null
PER_FIRST_NAMEnvarchar40 not null
PER_LAST_NAMEnvarchar40 not null
PER_BIRTH_DATE DateTime8
PER_WEIGHT_KGfloat8
PER_HEIGHT_Mfloat8
3. 为了在开发过程更加直观,我们需要将ibatisnet日志打开以便观察ibatinets运作的细节。
ibatisnet采用IBatisNet.Common.Logging.Log4Net,就是使用Log4Net纪录日志的包装。在Web.config中增加下面的配置内容:
<configSections>
<sectionGroup name="iBATIS">
<section name="logging" type="IBatisNet.Common.Logging.ConfigurationSectionHandler, IBatisNet.Common" />
sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
configSections>
<iBATIS>
<logging>
<logFactoryAdapter type="IBatisNet.Common.Logging.Impl.Log4NetLoggerFA, IBatisNet.Common.Logging.Log4Net">
<arg key="configType" value="inline" />
logFactoryAdapter>
logging>
iBATIS>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="2" />
<param name="MaximumFileSize" value="100KB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
layout>
appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
layout>
appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="ConsoleAppender" />
root>
<logger name="IBatisNet.DataMapper.Configuration.Cache.CacheModel">
<level value="DEBUG" />
logger>
<logger name="IBatisNet.DataMapper.Configuration.Statements.PreparedStatementFactory">
<level value="DEBUG" />
logger>
<logger name="IBatisNet.DataMapper.LazyLoadList">
<level value="DEBUG" />
logger>
<logger name="IBatisNet.DataAccess.DaoSession">
<level value="DEBUG" />
logger>
<logger name="IBatisNet.DataMapper.SqlMapSession">
<level value="DEBUG" />
logger>
<logger name="IBatisNet.Common.Transaction.TransactionScope">
<level value="DEBUG" />
logger>
<logger name="IBatisNet.DataAccess.Configuration.DaoProxy">
<level value="DEBUG" />
logger>
log4net>
构建IBatisNet基础代码
IBatisNet 基础代码包括:
1. IBatisNet 实例配置
一个典型的配置文件如下(具体配置项目的含义见后):
< sqlMapConfig xmlns ="http://ibatis.apache.org/dataMapper" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< properties embedded ="database.config, IBatisNetDemo" />
< settings >
< setting useStatementNamespaces ="${useStatementNamespaces}" />
< setting cacheModelsEnabled ="true" />
< setting validateSqlMap ="false" />
settings >
< providers embedded ="providers.config,IBatisNetDemo" />
< database >
< provider name ="sqlServer1.1" />
< dataSource name ="iBatisNet" connectionString ="data source=localhost;database=Northwind;user id=sa;password=;connection reset=false;connection lifetime=5; min pool size=1; max pool size=50" />
database >
< sqlMaps >
< sqlMap embedded ="Map.SqlClient.Person.xml,IBatisNetDemo" />
sqlMaps >
sqlMapConfig >
2. PO(Plain Ordinary Object)
下面是我们用作示例的一个PO:
public class Person
{
private int id;
private string firstName;
private string lastName;
private DateTime? birthDate;
private double? weightInKilograms;
private double? heightInMeters;
public Person() {
public int Id
{
get { return id; }
set { id = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public DateTime? BirthDate
{
get { return birthDate; }
set { birthDate = value; }
}
public double? WeightInKilograms
{
get { return weightInKilograms; }
set { weightInKilograms = value; }
}
public double? HeightInMeters
{
get { return heightInMeters; }
set { heightInMeters = value; }
}color: #