国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ibatisnet框架使用說明

2019-11-14 16:08:06
字體:
來源:轉載
供稿:網友

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;        }    }}
源碼數據庫下載地址:下載

 



 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 留坝县| 方正县| 化德县| 隆尧县| 绥棱县| 屯昌县| 德惠市| 云南省| 阿荣旗| 原阳县| 南丹县| 饶平县| 康马县| 富顺县| 安远县| 长岭县| 澳门| 尖扎县| 孟州市| 石狮市| 镇坪县| 泗洪县| 福贡县| 巴彦淖尔市| 汝阳县| 砚山县| 同心县| 莱芜市| 綦江县| 贡嘎县| 边坝县| 德安县| 花垣县| 苏州市| 泗洪县| 泽州县| 罗甸县| 平昌县| 瓮安县| 上杭县| 德化县|