IBatis框架例子
DaoProvider工程
AbstractDaoService.cs
ContentObject.cs
ContentMapper.xml
AbstractDaoService.cs
using System;
using System.Collections.Generic;
using System.Text;
using IBatisNet.DataMapper;
namespace DaoProvider
{
public abstract class AbstractDaoService
{
protected SqlMapper GetMapper()
{
return Mapper.Instance();
}
}
}
ContentDaoService.csusing System.Collections.Generic;
using System.Text;
using IBatisNet.DataMapper;
namespace DaoProvider
{
public abstract class AbstractDaoService
{
protected SqlMapper GetMapper()
{
return Mapper.Instance();
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using IBatisNet.DataMapper;
namespace DaoProvider
{
public class ContentDaoService : AbstractDaoService
{
public void Insert(ContentObject p_dataObject)
{
Mapper.Instance().Insert("ContentObject_DefaultInsert", p_dataObject);
}
public ContentObject Query(int p_intId)
{
return null;
}
public IList Query()
{
return Mapper.Instance().QueryForList("Content_DefaultSelect", null) as IList;
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Text;
using IBatisNet.DataMapper;
namespace DaoProvider
{
public class ContentDaoService : AbstractDaoService
{
public void Insert(ContentObject p_dataObject)
{
Mapper.Instance().Insert("ContentObject_DefaultInsert", p_dataObject);
}
public ContentObject Query(int p_intId)
{
return null;
}
public IList Query()
{
return Mapper.Instance().QueryForList("Content_DefaultSelect", null) as IList;
}
}
}
ContentObject.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace DaoProvider
{
public class ContentObject
{
private int m_intId;
public int ID
{
get { return m_intId; }
set { m_intId = value; }
}
private string m_strTitle;
public string Title
{
get { return m_strTitle; }
set { m_strTitle = value; }
}
private string m_strContentText;
public string ContentText
{
get { return m_strContentText; }
set { m_strContentText = value; }
}
private string m_strCreator;
public string Creator
{
get { return m_strCreator; }
set { m_strCreator = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
namespace DaoProvider
{
public class ContentObject
{
private int m_intId;
public int ID
{
get { return m_intId; }
set { m_intId = value; }
}
private string m_strTitle;
public string Title
{
get { return m_strTitle; }
set { m_strTitle = value; }
}
private string m_strContentText;
public string ContentText
{
get { return m_strContentText; }
set { m_strContentText = value; }
}
private string m_strCreator;
public string Creator
{
get { return m_strCreator; }
set { m_strCreator = value; }
}
}
}
ContentMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Account" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<alias>
<typeAlias alias="ContentObject" type="DaoProvider.ContentObject,DaoProvider"/>
</alias>
<resultMaps>
<resultMap id="ContentObject_DefaultResult" class="ContentObject">
<result column="ID" property="ID" type="int" dbType="Int"/>
<result column="Title" property="Title" type="string" dbType="Varchar"/>
<result column="ContentText" property="ContentText" type="string" dbType="Varchar"/>
<result column="Creator" property="Creator" type="string" dbType="Varchar"/>
</resultMap>
</resultMaps>
<statements>
<insert id="ContentObject_DefaultInsert" parameterClass="ContentObject">
INSERT INTO Content(Title,ContentText,Creator) VALUES(#Title#,#ContentText#,#Creator#)
<selectKey property="ID" resultClass="int" type="post">
SELECT @@IDENTITY AS value
</selectKey>
</insert>
<select id="Content_DefaultSelect" listClass="ArrayList" resultMap="ContentObject_DefaultResult" >
SELECT ID, Title, ContentText, Creator FROM Content
</select>
</statements>
<parameterMaps>
<parameterMap id="ContentObject_DefaultParameter" class="ContentObject" >
<parameter column="Title" property="Title" type="string" dbType="Varchar" direction="Input"/>
<parameter column="ContentText" property="ContentText" type="string" dbType="Varchar" direction="Input"/>
<parameter column="Creator" property="Creator" type="string" dbType="Varchar" direction="Input"/>
</parameterMap>
</parameterMaps>
</sqlMap>
SqlMap.config<sqlMap namespace="Account" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<alias>
<typeAlias alias="ContentObject" type="DaoProvider.ContentObject,DaoProvider"/>
</alias>
<resultMaps>
<resultMap id="ContentObject_DefaultResult" class="ContentObject">
<result column="ID" property="ID" type="int" dbType="Int"/>
<result column="Title" property="Title" type="string" dbType="Varchar"/>
<result column="ContentText" property="ContentText" type="string" dbType="Varchar"/>
<result column="Creator" property="Creator" type="string" dbType="Varchar"/>
</resultMap>
</resultMaps>
<statements>
<insert id="ContentObject_DefaultInsert" parameterClass="ContentObject">
INSERT INTO Content(Title,ContentText,Creator) VALUES(#Title#,#ContentText#,#Creator#)
<selectKey property="ID" resultClass="int" type="post">
SELECT @@IDENTITY AS value
</selectKey>
</insert>
<select id="Content_DefaultSelect" listClass="ArrayList" resultMap="ContentObject_DefaultResult" >
SELECT ID, Title, ContentText, Creator FROM Content
</select>
</statements>
<parameterMaps>
<parameterMap id="ContentObject_DefaultParameter" class="ContentObject" >
<parameter column="Title" property="Title" type="string" dbType="Varchar" direction="Input"/>
<parameter column="ContentText" property="ContentText" type="string" dbType="Varchar" direction="Input"/>
<parameter column="Creator" property="Creator" type="string" dbType="Varchar" direction="Input"/>
</parameterMap>
</parameterMaps>
</sqlMap>
<?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<!-- Rem : If used via a DataAccess context, properties tag will be ignored -->
<!--<properties resource="http://www.cnblogs.com/database.config"/>-->
<settings>
<!--<setting useStatementNamespaces="${useStatementNamespaces}"/>-->
<setting cacheModelsEnabled="true"/>
<setting validateSqlMap="false"/>
</settings>
<providers resource="providers.config"/>
<database>
<!-- Optional ( default ) -->
<provider name="sqlServer2.0"/>
<dataSource name="iBatisNet" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"/>
</database>
<!--<typeHandlers>
<typeHandler type="bool" dbType="Varchar" callback="OuiNonBool"/>
</typeHandlers>-->
<alias>
<typeAlias alias="ArrayList" type="System.Collections.ArrayList,mscorlib"/>
</alias>
<sqlMaps>
<!-- <sqlMap url="E:/Projet/iBatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/${directory}/MSSQL/SqlClient/Account.xml"/> -->
<sqlMap resource="Mappers/ContentMapper.xml"/>
<!--<sqlMap resource="Mappers/Employees.xml"/>-->
<!-- Rem : If used as embbeded Resources, use
<sqlMap embedded="Maps.MSSQL.SqlClient.Account.xml, IBatisNet.DataMapper.Test"/>-->
</sqlMaps>
</sqlMapConfig>
<sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<!-- Rem : If used via a DataAccess context, properties tag will be ignored -->
<!--<properties resource="http://www.cnblogs.com/database.config"/>-->
<settings>
<!--<setting useStatementNamespaces="${useStatementNamespaces}"/>-->
<setting cacheModelsEnabled="true"/>
<setting validateSqlMap="false"/>
</settings>
<providers resource="providers.config"/>
<database>
<!-- Optional ( default ) -->
<provider name="sqlServer2.0"/>
<dataSource name="iBatisNet" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"/>
</database>
<!--<typeHandlers>
<typeHandler type="bool" dbType="Varchar" callback="OuiNonBool"/>
</typeHandlers>-->
<alias>
<typeAlias alias="ArrayList" type="System.Collections.ArrayList,mscorlib"/>
</alias>
<sqlMaps>
<!-- <sqlMap url="E:/Projet/iBatis/trunk/cs/mapper/IBatisNet.DataMapper.Test/${directory}/MSSQL/SqlClient/Account.xml"/> -->
<sqlMap resource="Mappers/ContentMapper.xml"/>
<!--<sqlMap resource="Mappers/Employees.xml"/>-->
<!-- Rem : If used as embbeded Resources, use
<sqlMap embedded="Maps.MSSQL.SqlClient.Account.xml, IBatisNet.DataMapper.Test"/>-->
</sqlMaps>
</sqlMapConfig>