<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-5  評論-41  文章-13  trackbacks-0
    其實Ibatis的文檔里面已經講得很詳細,所以這里指總結一些簡單的入門問題:
    配置文件
    ??? sql-map-config.xml
    <!DOCTYPE?sqlMapConfig?PUBLIC
    ??????????"-//ibatis.apache.org//DTD?SQL?Map?Config?2.0//EN"??????
    ??????????"http://ibatis.apache.org/dtd/sql-map-config-2.dtd"
    >

    <sqlMapConfig>

    ??
    <properties?resource="properties/database.properties"/>

    ??
    <settings?cacheModelsEnabled="true"?enhancementEnabled="false"
    ????????????maxSessions
    ="64"?maxTransactions="8"?maxRequests="128" useStatementNamespaces="true"/>

    ??
    <transactionManager?type="JDBC">
    ????
    <dataSource?type="SIMPLE">
    ??????
    <property?value="${driver}"?name="JDBC.Driver"/>
    ??????
    <property?value="${url}"?name="JDBC.ConnectionURL"/>
    ??????
    <property?value="${username}"?name="JDBC.Username"/>
    ??????
    <property?value="${password}"?name="JDBC.Password"/>
    ??????
    <property?value="15"?name="Pool.MaximumActiveConnections"/>
    ??????
    <property?value="15"?name="Pool.MaximumIdleConnections"/>
    ??????
    <property?value="1000"?name="Pool.MaximumWait"/>
    ????
    </dataSource>
    ??
    </transactionManager>
    ??
    <sqlMap?resource="com/xxx/sql/Accout.xml"/>
    </sqlMapConfig>

    database.properties
    ####################################
    #?Database?Connectivity?Properties
    ####################################

    driver
    =com.mysql.jdbc.Driver
    url
    =jdbc:mysql://localhost:3306/test
    username
    =root
    password
    =


    Accout.xml? (sqlmap)
    <?xml?version="1.0"?encoding="UTF-8"?standalone="no"?>
    <!DOCTYPE?sqlMap?PUBLIC?"-//iBATIS.com//DTD?SQL?Map?2.0//En"?"http://www.ibatis.com/dtd/sql-map-2.dtd">

    <sqlMap?namespace="NS_Account">
    ????
    <typeAlias?alias="Account"?type="com.ibatis.domain.Accout"/>
    ????
    <resultMap?id="AccountResult"?class="Account">
    ????????
    <result?property="accountId"?column="Account_ID"/>
    ????????
    <result?property="name"?column="Name"/>
    ????????
    <result?property="password"?column="Password"/>
    ????????
    <result?property="type"?column="Type"/>
    ????
    </resultMap>

    ????
    <insert?id="insert"?parameterClass="Account">
    ????????
    <![CDATA[
    ????????insert?into?T_Admin_Account(
    ????????????Name,?Password,?Type,?Account_ID
    ????????)?values?(
    ????????????#name#,?#password#,?#type#,?#accountId#
    ????????)
    ????????
    ]]>
    ????
    </insert>

    ????
    <select?id="findAll"?resultMap="AccountResult"?parameterClass="Account">
    ????????
    <![CDATA[
    ????????select?*?from?T_Admin_Account
    ????????order?by?Account_ID
    ????????
    ]]>
    ????
    </select>

    ????
    <delete?id="delete"?parameterClass="Account">
    ????????
    <![CDATA[
    ????????delete??from?T_Admin_Account
    ????????where?Account_ID?=?#accountId#
    ????????
    ]]>
    ????
    </delete>

    ????
    <update?id="update"?parameterClass="Account">
    ????????
    <![CDATA[
    ????????update?T_Admin_Account
    ????????set?Name?=?#name#,?Password?=?#password#,?Type?=?#type#
    ????????where?Account_ID?=?#accountId#
    ????????
    ]]>
    ????
    </update>

    </sqlMap>
    取得sqlmapclient:
    private?static?SqlMapClient sqlmapclient=?null;
    //

    Reader?reader?
    =?null;
    try?{
    ??reader?
    =?Resources.getResourceAsReader(
    ????
    "com/ibatis/domain/sql/sql-map-config.xml");
    ?
    sqlmapclient=?SqlMapClientBuilder.buildSqlMapClient(reader);
    }?
    catch?(Exception?e)?{
    ??e.printStackTrace();
    }

    在設置好這些東西后,使用類似上面的代碼取得sqlmapclient之后就可以使用sqlmapclient進行數據庫操作了。
    應該注意到因為配置文件中使用了
    namespace(在<settings中設置了 useStatementNamespaces="true"/>),所以在sqlmapclient進行操作時要記住使用namespace。如:
    sqlmapclient.insert("NS_Account.insert",new?Acount());
    這是為了避免sqlmap配置文件中出現相同名字的方法時產生沖突。


    如果使用spring進行代碼的組織,那么事情將變得更加簡單,你只要在spring的配置文件里面使用
    ????<bean?id="sqlMapClient"?class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    ????????
    <property?name="configLocation">
    ????????????
    <value>com/ibatis/domain/sql/sql-map-config.xml</value>
    ????????
    </property>
    ????????
    <property?name="dataSource">
    ????????????
    <ref?bean="dataSource"/>
    ????????
    </property>
    ????
    </bean>
    sqlMapClient進行注冊,然后注入到相應的dao里面就可以了,spring還提供了簡便的SqlMapClientDaoSupport,這也會對你的程序很有幫助。

    如果你只是想使用ibatis自己提供的dao框架也可以。為了使用dao框架,還需要另外的配置文件:
    dao.xml:
    <?xml?version="1.0"?encoding="UTF-8"?>
    <!DOCTYPE?daoConfig?PUBLIC?"-//iBATIS.com//DTD?DAO?Configuration?2.0//EN"?"http://www.ibatis.com/dtd/dao-2.dtd">
    <daoConfig>
    ????
    <!--?Example?SQL?Maps?DAO?Configuration?-->
    ????
    <context>
    ????????
    <transactionManager?type="SQLMAP">
    ????????????
    <property?name="SqlMapConfigResource"
    ????????????????value
    ="sql-map-config.xml"?/>
    ????????
    </transactionManager>
    ????????
    <dao?interface="com.ln.dao.CategoryDao"
    ????????????implementation
    ="com.ln.daoImpl.ibatis.CategoryDaoImplSqlMap"?/>
    ????????????
    <dao?interface="com.ln.dao.CalDao"
    ????????????implementation
    ="com.ln.daoImpl.ibatis.CalDaoImplSqlMap"?/>
    ????
    </context>
    </daoConfig>
    而且在你的dao interface中要繼承com.ibatis.dao.client.Dao接口(這只是一個標志性借口,沒有任何方法)
    在程序中就可以使用:
    ????????Reader?reader?=?null;
    ????????DaoManager?daoManager
    =null;
    ????????
    try?{
    ??????????reader?
    =?Resources.getResourceAsReader(
    ????????????
    "com/ibatis/domain/sql/dao.xml");
    ??????????daoManager?
    =?DaoManagerBuilder.buildDaoManager(reader);
    ????????}?
    catch?(Exception?e)?{
    ??????????e.printStackTrace();
    ????????}
    ????????daoManager.getDao(CalDao.
    class);

    這樣的程序來取得dao,這也能很好的把dao提供的方法與具體的實現分離開來。
    在使用ibatis的dao框架時,最好在dao實現類中繼承ibatis提供的SqlMapDaoTemplate ,
    它是dao框架提供的一個模版類,用來管理dao mapper框架的各個方面,也可以簡化你的工作

    public?classCalDaoImpl extends SqlMapDaoTemplate implementsCalDao{
    ??
    publicCalDaoImpl (DaoManager?daoManager)?{
    ????
    super(daoManager);
    ??}
    }

    繼承之后只用在實現一個帶DaoManager 參數的構造函數就可以了。這個構造函數并不需要手動調用,使用daoManager.getDao(CalDao.class);來取得dao實例的時候,框架會自動進行調用。
    之后在你的daoImpl里面就可以直接使用queryForList等sqlmapclient提供的方法了

    這樣ibatis的介紹基本完成。

    那為什么要使用Ibatis呢?原因很簡單,它很好地屏蔽掉了
    Connection,statement,resultset等直接使用jdbc是很煩人的地方,又可以直接使用sql語句非常靈活地進行數據庫操作(特別是配置文件中的動態sql語句更是足夠的靈活),而且還提供了連接池,cache,事務管理等支持。使用起來簡單而又強大
    posted on 2006-04-12 10:18 OO 閱讀(803) 評論(0)  編輯  收藏 所屬分類: 框架、工具的使用

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 蜜臀AV免费一区二区三区| 亚洲视频在线免费观看| 男女交性永久免费视频播放| 亚洲网站免费观看| 中文字幕亚洲免费无线观看日本| 成人午夜亚洲精品无码网站| a毛片成人免费全部播放| 亚洲色图综合在线| kk4kk免费视频毛片| 337p日本欧洲亚洲大胆裸体艺术 | 亚洲国产成人五月综合网 | 亚洲综合无码一区二区痴汉| 欧洲精品成人免费视频在线观看| 亚洲精品中文字幕无乱码麻豆| 亚洲免费网站观看视频| 亚洲乱亚洲乱妇无码| 免费国内精品久久久久影院| 免费一区二区三区在线视频 | 美女网站免费福利视频| 亚洲中文字幕无码av| 免费看一级做a爰片久久| 乱淫片免费影院观看| 亚洲成熟xxxxx电影| 97人伦色伦成人免费视频 | 亚洲精品美女久久久久久久| 日韩亚洲国产二区| a级毛片毛片免费观看久潮| 亚洲精品成人图区| 国产一级淫片免费播放电影| www永久免费视频| 亚洲国产精品久久丫| 免费国产小视频在线观看| 美女视频黄a视频全免费网站色窝| 99久久亚洲精品无码毛片| 全免费a级毛片免费看无码| 久久国产一片免费观看| 亚洲va精品中文字幕| 国产亚洲精品线观看动态图| h视频在线观看免费完整版| 免费一级全黄少妇性色生活片 | 亚洲七久久之综合七久久|