IBatis配置SQLite

1、添加System.Data.SQLite.dll文件

2、在Providers.config中配置Provider:

 <provider name="SQLite3"
     description="SQLite, SQLite.NET provider V1.0.66.0"
     enabled="true"
     assemblyName="System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"
     connectionClass="System.Data.SQLite.SQLiteConnection"
     commandClass="System.Data.SQLite.SQLiteCommand"
     parameterClass="System.Data.SQLite.SQLiteParameter"
     parameterDbTypeClass="System.Data.SQLite.SQLiteType"
     parameterDbTypeProperty="DbType"
     dataAdapterClass="System.Data.SQLite.SQLiteDataAdapter"
     commandBuilderClass="System.Data.SQLite.SQLiteCommandBuilder"
     usePositionalParameters="false"
     useParameterPrefixInSql="true"
     useParameterPrefixInParameter="true"
     parameterPrefix="@"
     setDbParameterPrecision="false"
     setDbParameterScale="false"
     allowMARS="false"
  />

其中1.0.66.0为添加进去的System.Data.SQLite.dll的版本,可通过其属性进行查看。enable属性设置为“true”,使该<provider>节点可以使用。

3.在app.config中设置要使用的数据库的连接字符串,然后把数据库文件添加到项目中即可:
    <!--连接字符串-->
    <add key="Host" value=""/>
    <add key="UserName" value="root"/>
    <add key="Password" value=""/>
    <add key="Database" value="ewei_chainsclient.db"/>
    <add key="Port" value="3306"/>
    <add key="DbTemplate" value="Data Source={3}; Version=3;"/>

4、创建sqlMap.config:

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="ewei.DAL.ShopManagement.Shop" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!-- 数据库映射关系-->
  <alias>
    <typeAlias alias="Shops" type="ewei.IDAL.ShopManagement.model.Shop,ewei.IDAL.ShopManagement"/>
  </alias>
  <!-- 结果对象的封装 -->
  <resultMaps>
    <!-- 主实体映射 -->
    <resultMap id="ShopResult" class="Shops">
      <result property="ShopCode" column="shopCode"/>
    </resultMap>
  </resultMaps>
  <cacheModels>
    <cacheModel id="Shops-Cache" implementation="LRU" serialize="false">
      <flushInterval hours="24"/>
      <flushOnExecute statement="Shops.insert"/>
      <flushOnExecute statement="Shops.update"/>
      <flushOnExecute statement="Shops.delete"/>
      <property name="CacheSize" value="20"/>
    </cacheModel>
  </cacheModels>

 <statements>
    <sql id ="shopShop.listSql">
      SELECT
      shopCode      <!-- 店铺编码 -->
      FROM SE_Shop
    </sql>
    
    <!-- 查询单个店铺,参数类型固定为[map],结果类型为店铺,查询使用缓冲 -->
    <select id="shopShop.object" parameterClass="map" resultMap="ShopResult" cacheModel="Shop-Cache">
      <include refid="shopShop.listSql"/>
      <dynamic prepend=" WHERE ">
        <isNotNull property="shop.ShopCode" prepend="AND">
          shopCode = #shop.ShopCode#
        </isNotNull>
      </dynamic>
    </select>

</statements>
</sqlMap>

按照以上步骤即可使SQLite在IBatis上使用了,如果调试时出现Providers Loading错误时,只需在app.config文件的<configuration>节点内设置

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>

即可。

IBatis配置SQLite,布布扣,bubuko.com

IBatis配置SQLite

上一篇:数据库事务


下一篇:基于CAS实现单点登录(SSO):配置CAS服务端的数据库查询认证机制