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

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

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

    閑人野居
    好好學(xué)習(xí),天天向上
    posts - 57,  comments - 137,  trackbacks - 0
    ??? 用了很久hibernate ,突然想換個別的orm 工具,當(dāng)然在orm領(lǐng)域中,hibernate是老大。看了一下ibatis,發(fā)現(xiàn)如果對于crud操作不是很多的系統(tǒng)來說,是個不錯的選擇,尤其是適合那些對sql和性能熱衷的開發(fā)者。綜合來說ibatis不能算orm工具,只能算個半成品。不過比起直接用jdbc寫,那還是方便多了。主要的好處是分離了sql和代碼,如果你想追求性能,那么sql是你很好的利器,當(dāng)然ibatis的緩存也不錯。比起hibernate,ibatis就簡單多了,估計也就3天能夠基本掌握了,這大大減少了學(xué)習(xí)成本。
    ??? 說了那么多廢話,下面開始正題,通過一個簡單的實例開始ibatis之旅,文章大部分參考網(wǎng)上的ibatis 開發(fā)指南一文。
    ??? 主要的jar:ibatis 2.3.0,spring 2.0.1,log4j 1.2.9,commons-logging 1.0.4,hsqldb 1.8.0
    ??? ibatis實例配置:
    ?
    <sqlMapConfig>
    <!-- 事務(wù)采用spring 管理 -->
    ? <!--
    ? <transactionManager type="JDBC" commitRequired="false">
    ??? <dataSource type="SIMPLE">
    ????? <property name="JDBC.Driver" value="org.hsqldb.jdbcDriver"/>
    ????? <property name="JDBC.ConnectionURL" value="jdbc:hsqldb:hsql://localhost/xdb"/>
    ????? <property name="JDBC.Username" value="sa"/>
    ????? <property name="JDBC.Password" value=""/>
    ??? </dataSource>
    ? </transactionManager>
    -->
    ? <sqlMap resource="org/esoft/bo/xml/Account.xml"/>
    </sqlMapConfig>?? ?

    創(chuàng)建POJO對象:

    ?
    package com.esoft.bo;

    public class Account {

    ??? private String emailAddress;

    ??? private String firstName;

    ??? private int id;

    ??? private String lastName;

    ??? public String getEmailAddress() {
    ??????? return emailAddress;
    ??? }

    ??? public String getFirstName() {
    ??????? return firstName;
    ??? }

    ??? public int getId() {
    ??????? return id;
    ??? }

    ??? public String getLastName() {
    ??????? return lastName;
    ??? }

    ??? public void setEmailAddress(String emailAddress) {
    ??????? this.emailAddress = emailAddress;
    ??? }

    ??? public void setFirstName(String firstName) {
    ??????? this.firstName = firstName;
    ??? }

    ??? public void setId(int id) {
    ??????? this.id = id;
    ??? }

    ??? public void setLastName(String lastName) {
    ??????? this.lastName = lastName;
    ??? }

    }?? ?

    映射文件,感覺比較的麻煩。以后有機(jī)會的話一定自動生成此文件,尤其現(xiàn)在jpa當(dāng)?shù)馈?br />?
    <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE sqlMap???? ?
    ??? PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"???? ?
    ??? "http://ibatis.apache.org/dtd/sql-map-2.dtd">

    <sqlMap namespace="Account">
    ?? ?
    ?? ?<!-- Use type aliases to avoid typing the full classname every time. -->
    ?? ?<typeAlias alias="Account" type="com.esoft.bo.Account"/>
    ?? ?
    ?? ?<!-- Result maps describe the mapping between the columns returned
    ?? ?from a query, and the class properties.? A result map isn't
    ?? ?necessary if the columns (or aliases) match to the properties
    ?? ?exactly. -->
    ?? ?<resultMap id="AccountResult" class="Account">
    ?? ??? ?<result property="id" column="ACC_ID"/>
    ?? ??? ?<result property="firstName" column="ACC_FIRST_NAME"/>
    ?? ??? ?<result property="lastName" column="ACC_LAST_NAME"/>
    ?? ??? ?<result property="emailAddress" column="ACC_EMAIL"/>
    ?? ?</resultMap>
    ?? ?
    ?? ?<!-- Select with no parameters using the result map for Account class. -->
    ?? ?<select id="selectAllAccounts" resultMap="AccountResult">
    ?? ??? ?select * from ACCOUNT
    ?? ??? ?</select>
    ?? ?
    ?? ?<select id="selectByName" resultMap="AccountResult" parameterClass="String">
    ?? ??? ?select * from Account where ACC_FIRST_NAME like #name#
    ?? ?</select>
    ?? ?
    ?? ?<!-- A simpler select example without the result map.? Note the
    ?? ?aliases to match the properties of the target result class. -->
    ?? ?<select id="selectAccountById" parameterClass="int" resultClass="Account">
    ?? ??? ?select ACC_ID as id, ACC_FIRST_NAME as firstName, ACC_LAST_NAME as lastName,
    ?? ??? ?ACC_EMAIL as emailAddress from ACCOUNT where ACC_ID = #id# </select>
    ?? ?
    ?? ?<!-- Insert example, using the Account parameter class -->
    ?? ?<insert id="insertAccount" parameterClass="Account"> insert into ACCOUNT ( ACC_ID,
    ?? ??? ?ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL) values ( #id#, #firstName#,
    ?? ??? ?#lastName#, #emailAddress# ) </insert>
    ?? ?
    ?? ?<!-- Update example, using the Account parameter class -->
    ?? ?<update id="updateAccount" parameterClass="Account"> update ACCOUNT set
    ?? ??? ?ACC_FIRST_NAME = #firstName#, ACC_LAST_NAME = #lastName#, ACC_EMAIL =
    ?? ??? ?#emailAddress# where ACC_ID = #id# </update>
    ?? ?
    ?? ?<!-- Delete example, using an integer as the parameter class -->
    ?? ?<delete id="deleteAccountById" parameterClass="int"> delete from ACCOUNT where
    ?? ??? ?ACC_ID = #id# </delete>
    ?? ?
    ?? ?<delete id="clearAccount"> delete from ACCOUNT </delete>
    ?? ?
    </sqlMap>?? ?

    spring 配置:
    ?
    ...
    <bean id="dataSource"
    ?? ??? ?class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    ?? ??? ?<property name="driverClassName">
    ?? ??? ??? ?<value>${jdbc.driverClassName}</value>
    ?? ??? ?</property>
    ?? ??? ?<property name="url">
    ?? ??? ??? ?<value>${jdbc.url}</value>
    ?? ??? ?</property>
    ?? ??? ?<property name="username">
    ?? ??? ??? ?<value>${jdbc.username}</value>
    ?? ??? ?</property>
    ?? ??? ?<property name="password">
    ?? ??? ??? ?<value>${jdbc.password}</value>
    ?? ??? ?</property>
    ?? ?</bean>
    ???????? <bean id="transactionManager"
    ?? ??? ?class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    ?? ??? ?<property name="dataSource" ref="dataSource"/>
    ?? ? </bean>
    ?? ?
    ?????? <bean id="sqlMapClient"
    ?? ??? ?class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    ?? ??? ?<property name="dataSource" ref="dataSource"/>
    ?? ??? ?<property name="configLocation">
    ?? ??? ??? ?<value>SqlMapConfig.xml</value>
    ?? ??? ?</property>
    ????? </bean>
    ????? <bean id="accountDao" class="org.esoft.dao.AccountDaoImpl">
    ???????????? <property name="sqlMapClient" ref="sqlMapClient"/>
    ????? </bean>?? ?? ?
    ?? ?

    主要的代碼:
    public class AccountDaoImpl
    ??????? extends SqlMapClientDaoSupport {
    ??? public PK save(Account obj) {
    ??????? return (PK) getSqlMapClientTemplate().insert("insertAccount", obj);
    ??? }
    ??? public void update(Accountobj) {
    ??????? getSqlMapClientTemplate().update("updateAccount", obj);
    ??? }
    ???? public void delete(Account obj) {
    ??????? getSqlMapClientTemplate().delete(
    ??????????????? "deleteAccountById",
    ??????????????? obj.getPk());
    ??? }
    ??? public Account get(PK primaryKey) {
    ??????? return (Account) getSqlMapClientTemplate().queryForObject(
    ??????????????? "selectAccountById",
    ??????????????? primaryKey);
    ??? }
    }


    posted on 2007-01-10 20:27 布衣郎 閱讀(2288) 評論(1)  編輯  收藏 所屬分類: orm

    FeedBack:
    # re: ibatis 開始之旅
    2007-01-14 22:57 | JooRoo
    好文!  回復(fù)  更多評論
      

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


    網(wǎng)站導(dǎo)航:
     

    <2007年1月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(12)

    隨筆分類(59)

    隨筆檔案(57)

    blog

    java

    uml

    搜索

    •  

    積分與排名

    • 積分 - 357265
    • 排名 - 155

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲综合激情视频| 久久精品7亚洲午夜a| 亚洲精品无码av片| 日本最新免费网站| 亚洲人成网站18禁止久久影院| 99久9在线|免费| 亚洲精品人成电影网| 久久国内免费视频| 亚洲色偷偷色噜噜狠狠99网| 免费看片A级毛片免费看| ass亚洲**毛茸茸pics| 成人免费无码大片A毛片抽搐色欲| xxx毛茸茸的亚洲| 国产一区二区视频免费| 老妇激情毛片免费| 国产成人精品久久亚洲高清不卡 | 中文在线日本免费永久18近| 亚洲精品狼友在线播放| 最近免费中文字幕大全免费版视频| 久久国产亚洲精品无码| 成人免费视频网址| 日韩少妇内射免费播放| 久久亚洲精品成人综合| 午夜宅男在线永久免费观看网| 无码亚洲成a人在线观看| 亚洲精品高清在线| 久久精品免费一区二区| 亚洲AV无码一区二区一二区| 国产成人精品曰本亚洲79ren| 最近2019中文字幕免费大全5| 亚洲色偷偷偷综合网| 日韩一卡2卡3卡4卡新区亚洲| 中国xxxxx高清免费看视频| 亚洲欧洲无码一区二区三区| 国产亚洲精品线观看动态图| 免费A级毛片无码专区| 国产亚洲精品欧洲在线观看| 亚洲AV无码成人精品区天堂| 黄网址在线永久免费观看 | 亚洲AV无码资源在线观看| 久久久久亚洲爆乳少妇无|