ibatisnet框架使用说明
ibatis配置文件主要包括三个 sqlmap.config,providers.config,database.config,注意所有文件生成操作都为嵌入的资源。其中database.config主要是配置数据库参数的一个config文件
<?xml version="1.0" encoding="utf-8" ?> <settings> <!-- User application and configured property settings go here.--> <!-- To run tests, create a file named DataBase.config with your own value for datasource. (don‘t included it in the solution and don‘t commit it in SVN) --> <add key="userid" value="sa" /> <!--数据库连接登录名 --> <add key="password" value="sa" /><!--数据库连接密码 --> <add key="database" value="person" /><!--数据库名字 --> <add key="datasource" value="." /><!--服务器名--> <add key="selectKey" value="select @@IDENTITY as value" /> <add key="directory" value="Maps" /> <add key="useStatementNamespaces" value="false" /> </settings>
其次 providers.config 这个主要存放连接数据库的驱动程序 主要有 oracle sqlserver等等。
最后讲解 sqlmap.config这个配置比较重要。
<?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="../../database.config"/> --> <!--加载配置文件注意命名空间IBatisNetDemo--> <properties embedded="database.config, IBatisNetDemo"/> <settings> <setting useStatementNamespaces="${useStatementNamespaces}"/> <setting cacheModelsEnabled="true"/> <setting validateSqlMap="false"/> </settings> <!-- Optional if resource --> <providers embedded="providers.config,IBatisNetDemo"/> <!--加载配置文件注意命名空间IBatisNetDemo--> <!-- ==== SqlClient configuration ========= --> <!-- Rem : If used via a DataAccess context, database tag will be ignored --> <database> <!-- Optional ( default ) --> <!--加载数据库连接字符串这里连接sqlserver数据库--> <provider name="sqlServer1.1"/> <dataSource name="test" connectionString="data source=${datasource};database=${database};user id=${userid};password=${password};connection reset=false;connection lifetime=5; min pool size=1; max pool size=50"/> </database> <sqlMaps> <!-- user via embedded--> <!--加载配置文件注意命名空间IBatisNetDemo--> <sqlMap embedded="Map.SqlClient.Dep.xml,IBatisNetDemo"/> <sqlMap embedded="Map.SqlClient.Person.xml,IBatisNetDemo"/> </sqlMaps> </sqlMapConfig>
其次讲解xml文件 这里主要讲解两个xml文件,第一个Person.xml
<?xml version="1.0" encoding="utf-8" ?> <sqlMap namespace="Person" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <alias> <typeAlias alias="Person" type="IBatisNetDemo.Domain.Person,IBatisNetDemo" /> <!--加载实体类,这里主要指下面要调用的实体类--> </alias> <resultMaps> <!--返回数据解析的实体--> <resultMap id="SelectAllResult" class="Person"> <result property="Id" column="PER_ID" /> <result property="FirstName" column="PER_FIRST_NAME" /> <result property="LastName" column="PER_LAST_NAME" /> <result property="BirthDate" column="PER_BIRTH_DATE" /> <result property="WeightInKilograms" column="PER_WEIGHT_KG" /> <result property="HeightInMeters" column="PER_HEIGHT_M" /> <result property="depid" column="DepID" /> </resultMap> </resultMaps> <statements><!-- 这里主要写sql语句和存储过程--> <select id="SelectPersonByDepId" resultMap="SelectAllResult"> select PER_ID, PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M, DepID from PERSON where DepID=#DepID# </select> <select id="SelectAllPerson" resultMap="SelectAllResult"> select PER_ID, PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M, DepID from PERSON </select> <select id="SelectByPersonId" resultMap="SelectAllResult" parameterClass="Hashtable"> select PER_ID, PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M, DepID from PERSON <dynamic prepend="where"><!--动态添加sql语句条件--> <isParameterPresent> <isNotEmpty prepend="and" property="id" > PER_ID = ‘$id$‘ </isNotEmpty> <isNotEmpty prepend="and" property="name" > PER_FIRST_NAME LIKE ‘%$name$%‘ </isNotEmpty> </isParameterPresent> </dynamic> </select> <insert id="InsertPerson" parameterclass="Person" > <selectKey property="Id" type="post" resultClass="int"> ${selectKey} </selectKey> insert into Person ( PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M) values (#FirstName#,#LastName#,#BirthDate#, #WeightInKilograms#, #HeightInMeters#) </insert> <update id="UpdatePerson" parameterclass="Person"> <![CDATA[ update Person set PER_FIRST_NAME =#FirstName#, PER_LAST_NAME =#LastName#, PER_BIRTH_DATE =#BirthDate#, PER_WEIGHT_KG=#WeightInKilograms#, PER_HEIGHT_M=#HeightInMeters# where PER_ID = #Id# ]]> </update> <delete id="DeletePerson" parameterclass="Person"> delete from Person where PER_ID = #Id# </delete> </statements> </sqlMap>
第二个xml文件 Dep.xml
<?xml version="1.0" encoding="utf-8" ?> <sqlMap namespace="Dep" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > <alias> <typeAlias alias="Dep" type="IBatisNetDemo.Domain.Dep,IBatisNetDemo" /> </alias> <resultMaps> <resultMap id="SelectAllResultDep" class="Dep"> <result property="DepId" column="DepID" /> <result property="DepName" column="DepName" /> <result property="list" column="DepID" select="SelectPersonByDepId" /><!--配置一对多关系,这里的select选择id为person.xml文件的id,注意这里id都是公用--> </resultMap> </resultMaps> <statements> <select id="SelectDepById" resultMap="SelectAllResultDep"> select DepID,DepName from Dep where DepId=#DepID# </select> </statements> </sqlMap>
其中一个为部门,一个为人员,两个之间关系为一对多的关系
配置文件讲解完过后我们来讲解怎么调用这个框架,首先要引用IBatisNet.Common.dll,IBatisNet.DataMapper.dll 两个文件
其实加载sqlmap对象
代码如下:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using IBatisNet.DataMapper; using IBatisNet.DataMapper.Configuration; using IBatisNetDemo.Domain; namespace IBatisNetDemo { public class BaseDao<T> where T : class { private ISqlMapper sqlMap; //private string fileName = "sqlMap.Config"; public BaseDao() { Assembly assembly = Assembly.Load("IBatisNetDemo"); Stream stream = assembly.GetManifestResourceStream("IBatisNetDemo.sqlmap.config"); DomSqlMapBuilder builder = new DomSqlMapBuilder(); sqlMap = builder.Configure(stream); } public ISqlMapper SqlMap { get { return sqlMap; } } public IList<T> GetAllList(string key) { return SqlMap.QueryForList<T>(key, null); } public T GetModel(string key, object id) { return SqlMap.QueryForObject<T>(key, id); } public object Insert(string key, T model) { object o = null; o = sqlMap.Insert(key, model); return o; } } }
源码数据库下载地址:下载
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。