<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)航:
     
    主站蜘蛛池模板: 青青青国产免费一夜七次郎| 97无码人妻福利免费公开在线视频| 美丽的姑娘免费观看在线播放| 亚洲国产a∨无码中文777| 美女隐私免费视频看| 国产精品免费视频播放器| 亚洲AV无码成人精品区日韩| 在线观看免费毛片| 亚洲a无码综合a国产av中文| 免费国产成人高清视频网站| 香蕉视频免费在线播放| 亚洲精品成人a在线观看| j8又粗又长又硬又爽免费视频| 亚洲国产精品成人| AAA日本高清在线播放免费观看| 亚洲狠狠综合久久| 日韩免费一区二区三区在线播放| 亚洲国产成人精品无码区在线秒播 | 日韩电影免费在线观看网址| 免费一看一级毛片全播放| 羞羞视频在线观看免费| 亚洲熟妇无码乱子AV电影| 国产精品免费高清在线观看| 亚洲国产精久久久久久久| 亚洲黄色免费网址| 亚洲人成www在线播放| 国产免费直播在线观看视频| 人妻免费久久久久久久了| 亚洲Av无码专区国产乱码DVD| **aaaaa毛片免费同男同女| 亚洲色大成网站www永久网站| 亚洲国产成人乱码精品女人久久久不卡 | av片在线观看永久免费| 亚洲高清在线播放| 日本不卡视频免费| 成人无码WWW免费视频| 亚洲天堂男人影院| 亚洲午夜日韩高清一区| 2021精品国产品免费观看| 国产精品亚洲色图| 久久久无码精品亚洲日韩蜜臀浪潮|