<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)  編輯  收藏 所屬分類: 框架、工具的使用

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


    網站導航:
     
    主站蜘蛛池模板: 97国产在线公开免费观看| 成年大片免费高清在线看黄| 午夜免费福利片观看| 亚洲va久久久噜噜噜久久天堂| a级毛片100部免费观看| 亚洲精品少妇30p| 久久久久久久岛国免费播放 | 无码人妻AV免费一区二区三区| 亚洲线精品一区二区三区影音先锋| 好吊色永久免费视频大全| 亚洲国产无套无码av电影| 成人黄网站片免费视频| 99久久亚洲综合精品成人网| 中国xxxxx高清免费看视频| 亚洲人6666成人观看| 卡1卡2卡3卡4卡5免费视频| 综合一区自拍亚洲综合图区| 免费永久国产在线视频| 中文字幕无码毛片免费看| 久久国产亚洲高清观看| 91情侣在线精品国产免费| 精品亚洲视频在线| 狠狠亚洲婷婷综合色香五月排名| 美女巨胸喷奶水视频www免费| 久久国产精品亚洲一区二区| 精品无码免费专区毛片| 亚洲人AV在线无码影院观看| 亚洲精品成人久久久| 免费av片在线观看网站| 亚洲精品福利你懂| 亚洲性在线看高清h片| 免费不卡在线观看AV| 亚洲AV成人一区二区三区在线看| 亚洲AV无码之日韩精品| 无码A级毛片免费视频内谢| 亚洲国产av美女网站| 亚洲人成电影在线播放| 2021在线观看视频精品免费| 国产精品亚洲一区二区三区| 亚洲av成人无码久久精品| 青草草在线视频永久免费|