<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
    其實(shí)Ibatis的文檔里面已經(jīng)講得很詳細(xì),所以這里指總結(jié)一些簡單的入門問題:
    配置文件
    ??? 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();
    }

    在設(shè)置好這些東西后,使用類似上面的代碼取得sqlmapclient之后就可以使用sqlmapclient進(jìn)行數(shù)據(jù)庫操作了。
    應(yīng)該注意到因?yàn)榕渲梦募惺褂昧?/span>namespace(在<settings中設(shè)置了 useStatementNamespaces="true"/>),所以在sqlmapclient進(jìn)行操作時(shí)要記住使用namespace。如:
    sqlmapclient.insert("NS_Account.insert",new?Acount());
    這是為了避免sqlmap配置文件中出現(xiàn)相同名字的方法時(shí)產(chǎn)生沖突。


    如果使用spring進(jìn)行代碼的組織,那么事情將變得更加簡單,你只要在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進(jìn)行注冊,然后注入到相應(yīng)的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接口(這只是一個標(biāo)志性借口,沒有任何方法)
    在程序中就可以使用:
    ????????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提供的方法與具體的實(shí)現(xiàn)分離開來。
    在使用ibatis的dao框架時(shí),最好在dao實(shí)現(xiàn)類中繼承ibatis提供的SqlMapDaoTemplate ,
    它是dao框架提供的一個模版類,用來管理dao mapper框架的各個方面,也可以簡化你的工作

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

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

    這樣ibatis的介紹基本完成。

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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲日产2021三区| 国产精品免费久久| 亚洲国产成人久久精品99| a级毛片免费网站| 亚洲专区先锋影音| 青青草国产免费久久久91| 亚洲免费在线观看| 亚洲中文无码线在线观看| 911精品国产亚洲日本美国韩国 | 国产精品亚洲自在线播放页码| 免费人成年激情视频在线观看| 久久九九全国免费| 老牛精品亚洲成av人片| 国产亚洲成av片在线观看| 免费看香港一级毛片| 男人进去女人爽免费视频国产| 亚洲日本VA午夜在线影院| 亚洲av无码av制服另类专区| 美女黄网站人色视频免费国产| 爱丫爱丫影院在线观看免费| 亚洲国产成人精品无码区二本| 亚洲国产高清在线| 免费一级毛片女人图片| 又粗又大又黑又长的免费视频| 精品无码国产污污污免费网站国产| 亚洲人成综合在线播放| 亚洲欧洲成人精品香蕉网| 国产成人免费高清在线观看| **实干一级毛片aa免费| 两性色午夜免费视频| 亚洲成在人线aⅴ免费毛片| 亚洲精品在线免费观看视频| 亚洲五月综合缴情在线观看| 四虎影在线永久免费观看| 黄页免费的网站勿入免费直接进入| 国产精成人品日日拍夜夜免费| 黄 色一级 成 人网站免费| 久久亚洲精品高潮综合色a片| 亚洲三级中文字幕| 亚洲日本在线免费观看| 亚洲av无码潮喷在线观看|