Ibatis使用知識總結

          1.  從官方網站下載,ibatis的jar及相關的文件,http://ibatis.apache.org/
          2 . 新建一個工程, 我們如果要進行ibatis相關的操作, 就一個要導入ibatis-2.3.3.720.jar
          3 . 建立pojo與pojo.xml形成映射.
          4.  一般一個pojo對應一個pojo.xml, 例如.User.java.與User.xml
          pojo的類最好實現java.io.Serializable接口, 以備應用時, 進一步的擴展, 還要提供一個缺省的構造方法(空構造方法)。
          5. ibatis的配置文件.SqlMapConfig.xml, 這個文件一般放到src下.
          <?xml version="1.0" encoding="UTF-8" >
          <!DOCTYPE sqlMapConfig     
              PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"     
              "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

          <sqlMapConfig>

            <!-- Configure a built-in transaction manager.  If you're using an
                 app server, you probably want to use its transaction manager
                 and a managed datasource --
                  <properties resource="dbconfig.properties"/>/*數據庫的驅動配置文件,一般與SqlMapConfig.xml是同一級目錄*/
                                                            
            <transactionManager type="JDBC" commitRequired="false">
              <dataSource type="SIMPLE">
                <property name="JDBC.Driver" value="${driver}"/>
                <property name="JDBC.ConnectionURL" value="${url}"/>
                <property name="JDBC.Username" value="${user}"/>
                <property name="JDBC.Password" value="${password}"/>/*配置文件的Key*/
              </dataSource>
            </transactionManager>
            
           
            <sqlMap resource="com/lxit/test/LXGroup.xml"/>

            /*指定pojo的映射文件,  這句話一定要加上, 否則, 找不到配置文件,包名/類名.xml*/
          </sqlMapConfig>
          LXGroup.xml
          <?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="LXGroup">

              <typeAlias alias="LXGroup" type="com.lxitedu.pojo.permission.LXGroup" />
               類型別名 , 為一個pojo類取一個別名, 以便在下面用時候, 直接用別名引用對象(上面的解釋)
              <resultMap class="LXGroup" id="paginationList">
                  <result property="groupId" column="ID" />
                  <result property="groupName" column="groupName" />
                  <result property="description" column="description" />
              </resultMap>
               數據返回類型,我用的是一張表 , id 與select 標簽的id一致, 就可以指定返回是多個pojo
              <select id="userGroupRowCount" resultClass="int">
                  select count(*) from userGroup
              </select>
               我們可以, 根據自己的SQL查詢語句, 及傳入的參數來決定它的返回類型,
                 resutlClass="返回類型" parameterClass="傳入的參數類型"
              <select id="userGroupList" resultMap="paginationList">
                  select * from userGroup
               </select>
              <delete id="deleteUserGroup" parameterClass="String">
                  delete from userGroup where groupName=#value#
                      #value#由方法的傳入參數填充
               </delete>
              <select id="userGroup" parameterClass="String"
                  resultClass="LXGroup">
                  select ID as groupId,groupName as groupName,description as description from
                  userGroup where groupName=#value#
               </select>
              <insert id="addUserGroup" parameterClass="com.lxitedu.pojo.permission.LXGroup">
                  insert into userGroup(groupName,description)values
                  (#groupName#,## where
                  ID=#groupId#
               </update>description#)
                  <selectKey resultClass="int" keyProperty="groupId">
                      select LAST_INSERT_ID() as value
                </selectKey>(這是主鍵自動增長)
              </insert>
              <update id="updateUserGroup" parameterClass="LXGroup">
                  update userGroup set groupName=#groupName#,description=#description
          </sqlMap>
          6. 這里所有配置文件都完成了
          之后就是得到SqlMapClient對象, 調用相應的方法, 完成相應操作
          private static SqlMapClient sqlMapper;
              public static synchronized SqlMapClient getSqlMapClientInstance()
              {  
                    if(sqlMapper==null)
                    {
                        try {
                            Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
                            sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
                            reader.close();
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
                    }
                      return sqlMapper;
              }


                                   ibatis與spring的整合

          1. ibatis與spring整合, 我們要導入相應的jar.commons-dbcp-1.2.jar,commons-pool-1.4.jar,spring2.5.5.jar,這些是Spring的jar
          2. 要想spring與ibatis整合起來, 我們只需修改一下配置文件就可以了,如果成功之后, 我們就會發現, 他會使的我們的程序越來越簡單
          第一步:在SqlMapConfig.xml里 , 要刪除 連接數據庫驅動。
          第二步:在ApplicationContext.xml里修改驅動

              <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
                  <property name="ignoreResourceNotFound" value="true" />
                  <property name="locations">
                      <list>
                          <value>classpath:dbconfig.properties</value>
                      </list>
                  </property>
              </bean>
              <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                  destroy-method="close">
                  <property name="driverClassName">
                      <value>${driver}</value>
                  </property>
                  <property name="url">
                      <value>${url}</value>
                  </property>
                  <property name="username">
                      <value>${user}</value>
                  </property>
                  <property name="password">
                      <value>${password}</value>
                  </property>
              </bean>
          第三步: 整合ibatis與spring
            <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
              <property name="configLocation" value="classpath:SqlMapConfig.xml"/>(src下)
              <property name="dataSource" ref="dataSource"/>
            </bean>

          第四步:修改實例對象方式
          如果是單獨的ibatis, 我們要通過讀取SqlMapConfig.xml來得到SqlMapConfig來得到SqlMapClient對象
          現在與spring整合之間, 我們只需要修改類的繼承關系就可以了, 一般繼承SqlMapClientDaoSupport
          this.getSqlMapClient()就可以實現得到數據庫的操作對象,簡化了操作。
          第五步:在sqlmapconfig.xml里面我們只要寫映射pojo.xml的路徑就可以了。

          posted on 2009-11-12 17:00 飛熊 閱讀(660) 評論(0)  編輯  收藏 所屬分類: Ibatis

          <2009年11月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 肃南| 安义县| 雷州市| 两当县| 葵青区| 遂昌县| 察隅县| 临安市| 肥东县| 神农架林区| 和顺县| 巴彦淖尔市| 安义县| 普兰店市| 孝昌县| 荔浦县| 乳山市| 万年县| 新安县| 濉溪县| 闸北区| 海宁市| 西贡区| 历史| 蓬溪县| 九龙坡区| 勃利县| 阿瓦提县| 宜章县| 东乌| 遂川县| 安顺市| 海林市| 江永县| 白玉县| 曲周县| 尉氏县| 邓州市| 禹州市| 汨罗市| 延川县|