生活随笔
收集整理的這篇文章主要介紹了
mybatis、ibatis 和spring集成
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
mybatis是ibatis的升級版,spring也有自帶mybatis的orm。所以,搭建ibatis的框架也會有多種方式(我這里mybatis是3.0的,ibatis是2.3的,spring是3.0的,數(shù)據(jù)庫是mysql)。下面介紹3中方式
1,只是用mybatis3。
2,使用mybatis3+spring3(使用mybatis的SqlSessionFactory )。
3,使用ibatis2.3+spring(使用spring自帶的ibatis)
spring的orm包中只有ibatis,沒有mybatis。而mybatis和ibatis還是有些區(qū)別的,比如配置文件屬性不同。
?
第一種方式(只使用mybatis):
1)jar包:
cglib-2.2.jar
asm-3.1.jar
mysql-connector-java-3.1.13.jar
mybatis-3.0.5.jar
junit.jar
2)mybatis配置文件:
[html] view plaincopy
<?xml?version="1.0"?encoding="UTF-8"??><!DOCTYPE?configuration?PUBLIC?"-//mybatis.org//DTD?Config?3.0//EN"?"http://mybatis.org/dtd/mybatis-3-config.dtd">??<configuration>????????????<settings>????????????????????<setting?name="cacheEnabled"?value="true"/>????????????????????<setting?name="lazyLoadingEnabled"?value="true"/>????????????????????<setting?name="aggressiveLazyLoading"?value="true"/>????????????????????<setting?name="multipleResultSetsEnabled"?value="true"/>????????????????????<setting?name="useColumnLabel"?value="true"/>????????????????????<setting?name="useGeneratedKeys"?value="true"/>????????????????????<setting?name="autoMappingBehavior"?value="PARTIAL"/>????????????????????<setting?name="defaultExecutorType"?value="SIMPLE"/>????????????????????<setting?name="defaultStatementTimeout"?value="25000"/>??????</settings>??????????????????<typeAliases>??????????<typeAlias?alias="pageAccessURL"??type="com.lgm.mybatis.model.PageAccessURL"?/>??????</typeAliases>????????????<environments?default="development">????????????????????<environment?id="development1">??????????????<!--????????????????????事務配置?type=?JDBC、MANAGED??????????????????1.JDBC:這個配置直接簡單使用了JDBC的提交和回滾設置。它依賴于從數(shù)據(jù)源得到的連接來管理事務范圍。??????????????????2.MANAGED:這個配置幾乎沒做什么。它從來不提交或回滾一個連接。而它會讓容器來管理事務的整個生命周期(比如Spring或JEE應用服務器的上下文)。??????????????????默認情況下它會關(guān)閉連接。然而一些容器并不希望這樣,因此如果你需要從連接中停止它,將closeConnection屬性設置為false????????????????????????????-->??????????????<transactionManager?type="JDBC"/>??????????????<!--???????????????<transactionManager?type="MANAGED">??????????????????<property?name="closeConnection"?value="false"/>??????????????</transactionManager>???????????????-->???????????????<!--????????????????????數(shù)據(jù)源類型:type?=?UNPOOLED、POOLED、JNDI??????????????????1.UNPOOLED:這個數(shù)據(jù)源的實現(xiàn)是每次被請求時簡單打開和關(guān)閉連接。它有一點慢,這是對簡單應用程序的一個很好的選擇,因為它不需要及時的可用連接。??????????????????不同的數(shù)據(jù)庫對這個的表現(xiàn)也是不一樣的,所以對某些數(shù)據(jù)庫來說配置數(shù)據(jù)源并不重要,這個配置也是閑置的??????????????????2.POOLED:這是JDBC連接對象的數(shù)據(jù)源連接池的實現(xiàn),用來避免創(chuàng)建新的連接實例時必要的初始連接和認證時間。??????????????????這是一種當前Web應用程序用來快速響應請求很流行的方法。??????????????????3.JNDI:這個數(shù)據(jù)源的實現(xiàn)是為了使用如Spring或應用服務器這類的容器,容器可以集中或在外部配置數(shù)據(jù)源,然后放置一個JNDI上下文的引用???????????????-->??????????????<dataSource?type="UNPOOLED">??????????????????<property?name="driver"?value="com.mysql.jdbc.Driver"/>??????????????????<property?name="url"?value="jdbc:mysql://localhost:3306/appdb"/>??????????????????<property?name="username"?value="root"/>??????????????????<property?name="password"?value="123456"/>??????????????????<!--????????????????????默認連接事務隔離級別??????????????????<property?name="defaultTransactionIsolationLevel"?value=""?/>??????????????????-->??????????????</dataSource>????????????????????????</environment>??????????????????????????????<environment?id="development2">??????????????<transactionManager?type="JDBC"/>??????????????<dataSource?type="POOLED">??????????????????<property?name="driver"?value="com.mysql.jdbc.Driver"/>??????????????????<property?name="url"?value="jdbc:mysql://localhost:3306/appdb"/>??????????????????<property?name="username"?value="root"/>??????????????????<property?name="password"?value="123456"/>????????????????????????????????????<property?name="poolMaximumActiveConnections"?value="10"/>????????????????????????????????????<property?name="poolMaximumIdleConnections"?value="5"/>????????????????????????????????????<property?name="poolMaximumCheckoutTime"?value="20000"/>????????????????????????????????????<property?name="poolTimeToWait"?value="20000"/>????????????????????????????????????<property?name="poolPingQuery"?value="NO?PING?QUERY?SET"/>????????????????????????????????????<property?name="poolPingEnabled"?value="false"/>????????????????????????????????????<property?name="poolPingConnectionsNotUsedFor"?value="0"/>??????????????</dataSource>??????????</environment>??????????????????????????????<environment?id="development3">??????????????<transactionManager?type="JDBC"/>??????????????<dataSource?type="JNDI">??????????????????<property?name="data_source"?value="java:comp/env/jndi/mybatis"/>??????????????????<property?name="env.encoding"?value="UTF8"/>??????????????????<!--???????????????????<property?name="initial_context"?value=""/>??????????????????<property?name="env.encoding"?value="UTF8"/>???????????????????-->??????????????</dataSource>??????????</environment>????????????????????????</environments>??????????????<mappers>??????????<mapper?resource="com/lgm/mybatis/config/pageAccessURL.xml"/>??????</mappers>????</configuration>?? ?
其中<environments>屬性是數(shù)據(jù)源環(huán)境配置,可以配置多個數(shù)據(jù)源配置。每個<environment>屬性代表一種配置方式。
加載事務配置<transactionManager > 和數(shù)據(jù)源配置<dataSource >。
<transactionManager >? 有兩種配置方式,分別是JDBC和MANAGED,詳細說明見配置注釋。
<dataSource > 有三種配置方式,分別是UNPOOLED、POOLED、JNDI,詳細說明見配置注釋。
<typeAliases>定義別名,使用指定的別名來定義。
注:關(guān)于JNDI的配置,見tomcat的幾種JNDI配置方法
3)mybatis的sql映射配置文件:
[html] view plaincopy
<?xml?version="1.0"?encoding="UTF-8"??>??<!DOCTYPE?mapper?PUBLIC?"-//mybatis.org//DTD?Mapper?3.0//EN"??"http://mybatis.org/dtd/mybatis-3-mapper.dtd">????<mapper?namespace="pageAccessURL"?>????????<!--?????????????cache?-?配置給定命名空間的緩存。????????????cache-ref?–?從其他命名空間引用緩存配置。????????????resultMap?–?最復雜,也是最有力量的元素,用來描述如何從數(shù)據(jù)庫結(jié)果集中來加載你的對象。????????????parameterMap?–?已經(jīng)被廢棄了!老式風格的參數(shù)映射。內(nèi)聯(lián)參數(shù)是首選,這個元素可能在將來被移除。這里不會記錄。????????????sql?–?可以重用的SQL塊,也可以被其他語句引用。????????????insert?–?映射插入語句????????????update?–?映射更新語句????????????delete?–?映射刪除語句????????????select?–?映射查詢語句??????-->????????????<!--?默認不開啟二級緩存,開啟緩存<cache??eviction="FIFO"?flushInterval="60000"?size="512"?readOnly="true"/>??????????eviction:緩存策略?eviction?=?LRU、FIFO、SOFT、WEAK(默認LRU)??????????????1)LRU:最近最少使用的:移除最長時間不被使用的對象??????????????2)FIFO:先進先出:按對象進入緩存的順序來移除它們。??????????????3)SOFT:軟引用:移除基于垃圾回收器狀態(tài)和軟引用規(guī)則的對象。??????????????4)WEAK:弱引用:更積極地移除基于垃圾收集器狀態(tài)和弱引用規(guī)則的對象。??????????flushInterval:刷新間隔?)可以被設置為任意的正整數(shù),而且它們代表一個合理的毫秒形式的時間段。???????????默認情況是不設置,也就是沒有刷新間隔,緩存僅僅調(diào)用語句時刷新。??????????size:引用數(shù)目?可以被設置為任意正整數(shù),要記住你緩存的對象數(shù)目和你運行環(huán)境的可用內(nèi)存資源數(shù)目。默認值是1024。??????????readOnly:屬性可以被設置為true或false。只讀的緩存會給所有調(diào)用者返回緩存對象的相同實例。因此這些對象不能被修改。??????????這提供了很重要的性能優(yōu)勢。可讀寫的緩存會返回緩存對象的拷貝(通過序列化)。這會慢一些,但是安全,因此默認是false。??????????----------------------------------------------------------------------------------------------??????????使用自定義緩存??????????<cache?type=”com.domain.something.MyCustomCache”>??????????????<property?name=”cacheFile”?value=”/tmp/my-custom-cache.tmp”/>??????????</cache>??????????type屬性指定的類必須實現(xiàn)org.mybatis.cache.Cache接口??????????----------------------------------------------------------------------------------??????????引用另外的緩存<cache-ref?namespace=”com.someone.application.data.SomeMapper”/>??????-->??????????????????<!--????????????select元素:??????????1)parameterType:參數(shù)類型??????????2)resultType:從這條語句中返回的期望類型的類的完全限定名或別名。??????????注意集合情形,那應該是集合可以包含的類型,而不能是集合本身。使用resultType或resultMap,但不能同時使用。??????????3)resultMap:命名引用外部的resultMap。返回map是MyBatis最具力量的特性,對其有一個很好的理解的話,許多復雜映射的情形就能被解決了。??????????使用resultMap或resultType,但不能同時使用。??????????4)flushCache:是否清空緩存,默認false,不清空,如果為true每次查詢都會清空緩存。??????????5)useCache:將其設置為true,將會導致本條語句的結(jié)果被緩存。默認值:true。??????????6)fetchSize:這是暗示驅(qū)動程序每次批量返回的結(jié)果行數(shù)。默認不設置(驅(qū)動自行處理)。??????????7)statementType:STATEMENT,PREPARED或CALLABLE的一種。這會讓MyBatis使用選擇使用??????????Statement,PreparedStatement或CallableStatement。默認值:PREPARED。??????????8)resultSetType:FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE中的一種。默認是不設置(驅(qū)動自行處理)。??????-->??????????????????<select?id="selectPageAccessURL"?parameterType="int"?resultType="pageAccessURL"?>??????????select?*?from?tables?where?URL_ID?=?#{id}??????</select>????????????<select?id="selectPageAccessURLByClass"?parameterType="pageAccessURL"?resultType="pageAccessURL">??????????select?*?from?tables?where?URL_ID?=?#{urlId}?and?URL?=?#{url}??????</select>????????????????<sql?id="usercolumns">URL_ID?as?urlId,url,moduleId,state,mark</sql>??????<select?id="selectPageAccessURL2"?parameterType="int"?resultType="pageAccessURL">??????????select?<include?refid="usercolumns"?/>????????????from?tables?where?URL_ID?=?#{id}??????</select>????????????????????????????<!--????????????resultMap?元素:??????????????1)constructor:類在實例化時,用來注入結(jié)果到構(gòu)造方法中??????????????????a)idArg:ID參數(shù);標記結(jié)果作為ID可以幫助提高整體效能??????????????????b)arg:注入到構(gòu)造方法的一個普通結(jié)果??????????????2)id:一個ID結(jié)果;標記結(jié)果作為ID可以幫助提高整體效能??????????????3)result:注入到字段或JavaBean屬性的普通結(jié)果??????????????4)association:一個復雜的類型關(guān)聯(lián);許多結(jié)果將包成這種類型??????????????5)collection:復雜類型的集??????????????6)discriminator:使用結(jié)果值來決定使用哪個結(jié)果映射??????????????????a)case:基于某些值的結(jié)果映射??????????????---------------------------------------------------???????????resultMap下?id、result?元素:??????????????1)property:映射到列結(jié)果的字段或?qū)傩??????????????2)column:從數(shù)據(jù)庫中得到的列名,或者是列名的重命名標簽。這也是通常和會傳遞給resultSet.getString(columnName)方法參數(shù)中相同的字符串。??????????????3)javaType:一個Java類的完全限定名,或一個類型別名(參加上面內(nèi)建類型別名的列表)。如果你映射到一個JavaBean,MyBatis通常可以斷定類型。??????????????然而,如果你映射到的是HashMap,那么你應該明確地指定javaType來保證所需的行為。??????????????4)jdbcType:在這個表格之后的所支持的JDBC類型列表中的類型。JDBC類型是僅僅需要對插入,更新和刪除操作可能為空的列進行處理。??????????????這是JDBC的需要,而不是MyBatis的。如果你直接使用JDBC編程,你需要指定這個類型-但僅僅對可能為空的值。??????????????5)typeHandler:。使用這個屬性,你可以覆蓋默認的類型處理器。這個屬性值是類的完全限定名或者是一個類型處理器的實現(xiàn),或者是類型別名。??????????resultMap下?constructor?元素:將查詢到的對象映射到javabean時,如果javabean的構(gòu)造函數(shù)有參數(shù),則可以使用該元素來指定??????????resultMap下?association?元素:關(guān)聯(lián),比如查詢博客和該博客的用戶,通過博客關(guān)聯(lián)用戶,association里面就是用戶屬性。??????????resultMap下?collection?元素:集合,一個博客下有很多文章,collection就是存放文章的集合屬性。??????????resultMap下?discriminator?元素:鑒別器,有時一個單獨的數(shù)據(jù)庫查詢也許返回很多不同(但是希望有些關(guān)聯(lián))數(shù)據(jù)類型的結(jié)果集。??????????鑒別器元素就是被設計來處理這個情況的,還有包括類的繼承層次結(jié)構(gòu)。鑒別器非常容易理解,因為它的表現(xiàn)很像Java語言中的switch語句。??????????<resultMap?id="detailedBlogResultMap"?type="Blog">??????????????<constructor>??????????????????<idArg?column="blog_id"?javaType="int"/>??????????????</constructor>??????????????<result?property="title"?column="blog_title"/>??????????????<association?property="author"?column="blog_author_id"?javaType="?Author">??????????????????<id?property="id"?column="author_id"/>??????????????????<result?property="username"?column="author_username"/>??????????????????<result?property="password"?column="author_password"/>??????????????????<result?property="email"?column="author_email"/>??????????????????<result?property="bio"?column="author_bio"/>??????????????????<result?property="favouriteSection"?column="author_favourite_section"/>??????????????</association>??????????????<collection?property="posts"?ofType="Post">??????????????????<id?property="id"?column="post_id"/>??????????????????<result?property="subject"?column="post_subject"/>??????????????????<association?property="author"?column="post_author_id"?javaType="Author"/>??????????????????<collection?property="comments"?column="post_id"?ofType="?Comment">??????????????????????<id?property="id"?column="comment_id"/>??????????????????</collection>??????????????????<collection?property="tags"?column="post_id"?ofType="?Tag"?>??????????????????????<id?property="id"?column="tag_id"/>??????????????????</collection>??????????????????<discriminator?javaType="int"?column="draft">??????????????????????<case?value="1"?resultType="DraftPost">??????????????????????????<result?property=”doorCount”?column="door_count"?/>??????????????????????</case>??????????????????</discriminator>??????????????</collection>??????????</resultMap>????????????????-->????????????????<!--?insert?元素:??????????1)useGeneratedKeys:這會告訴MyBatis使用JDBC的getGeneratedKeys方法來取出由數(shù)據(jù)??????????(比如:像MySQL和SQL?Server這樣的數(shù)據(jù)庫管理系統(tǒng)的自動遞增字段)內(nèi)部生成的主鍵。默認值:false。??????????2)keyProperty:標記一個屬性(自動生成的那個屬性,比如主鍵id),MyBatis會通過getGeneratedKeys或者通過insert語句的selectKey子元素設置它的值。默認:不設置。????????????-->??????<insert?id="insertTest"?>??????????<!--????????????selectKey?:在插入數(shù)據(jù)前先執(zhí)行selectKey語句,將返回的值作為keyProperty屬性值插入??????????????1)order:這可以被設置為BEFORE或AFTER。如果設置為BEFORE,那么它會首先選擇主鍵,設置keyProperty然后執(zhí)行插入語句。??????????????如果設置為AFTER,那么先執(zhí)行插入語句,然后是selectKey元素-這和如Oracle數(shù)據(jù)庫相似,可以在插入語句中嵌入序列調(diào)用。?????????????????2)statementType:和前面的相同,MyBatis支持STATEMENT,PREPARED和CALLABLE語句的映射類型,分別代表PreparedStatement和CallableStatement類型。???????????-->??????????<selectKey?keyProperty="id"?resultType="int"?order="BEFORE"?statementType="PREPARED">??????????????SELECT?FLOOR(1?+?(RAND()?*?1000000));???????????</selectKey>??????????insert?into?table?values(xx,xx);??????</insert>??????????</mapper>??????? ?
4)測試方法:
[java] view plaincopy
????public?void?testSelect(){????????????????????try?{??????????????Reader?reader?=?Resources.getResourceAsReader("mybatis-config-mappings.xml");??????????????SqlSessionFactory?sqlSessionFactory?=?new?SqlSessionFactoryBuilder().build(reader,"development1");??????????????SqlSession?session?=?sqlSessionFactory.openSession();??????????????PageAccessURL?pageAccessURL?=(PageAccessURL)session.selectOne("selectPageAccessURL2",?70001);??????????????????????????????????????session.close();??????????????reader.close();??????????????System.out.println(pageAccessURL.getUrl());??????????}?catch?(IOException?e)?{??????????????System.out.println(e);??????????}??????}?? SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader,"development1"); 后面的development1是前面講的數(shù)據(jù)源配置方式之一。如果要測試jndi的,則要通過容器加載后進行。
?
?第二種方式(mybatis3.0+spring3.0,spring自帶的orm中,只有ibatis的,沒有mybatis,所以使用mybatis3和spring整合的話只能用SqlSessionFactory 了);
1)jar包:
mybatis-3.0.5.jar
mysql-connector-java-3.1.13.jar
cglib-2.2.jar
asm-3.1.jar
aopalliance-1.0.jar
commons-logging-1.1.1.jar
hsqldb-1.8.0.10.jar
jstl-1.2.jar
log4j-1.2.16.jar
mybatis-spring-1.0.1.jar
spring-aop-3.0.5.RELEASE.jar
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-jdbc-3.0.5.RELEASE.jar
spring-tx-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
stripes-1.5.6.jar
commons-dbcp-1.2.2.jar
commons-pool-1.3.jar
junit.jar
2)spring配置文件:
applicationContext.xml
[html] view plaincopy
<?xml?version="1.0"?encoding="UTF-8"??>??<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">????<beans>??????<import?resource="applicationContext-dao.xml"?/>??</beans>?? applicationContext.xml
[html] view plaincopy
<?xml?version="1.0"?encoding="UTF-8"??>??<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">????<beans>????????<bean?id="transactionManager"?class="org.springframework.jdbc.datasource.DataSourceTransactionManager">??????????<property?name="dataSource"?ref="dataSource"?/>??????</bean>????????????<bean?id="sqlSessionFactory"?class="org.mybatis.spring.SqlSessionFactoryBean">??????????<property?name="configLocation"?value="classpath:mybatis-config-mappings.xml"?/>??????????<property?name="dataSource"?ref="dataSource"?/>??????</bean>????????????<bean?id="dataSource"?class="org.apache.commons.dbcp.BasicDataSource"?destroy-method="close">??????????<property?name="driverClassName"?value="com.mysql.jdbc.Driver"?/>??????????<property?name="url"?value="jdbc:mysql://localhost.hk:3306/appdb"?/>??????????<property?name="username"?value="root"?/>??????????<property?name="password"?value="123456"?/>??????????<property?name="maxActive"?value="100"?/>??????????<property?name="maxIdle"?value="5"?/>??????????<property?name="minEvictableIdleTimeMillis"?value="300000"?/>??????????<property?name="timeBetweenEvictionRunsMillis"?value="120000"?/>??????????<property?name="validationQuery"?value="SELECT?1"?/>??????????<property?name="testWhileIdle"?value="true"?/>??????????<property?name="testOnReturn"?value="true"?/>??????????<property?name="testOnBorrow"?value="true"?/>??????</bean>????????<bean?id="pageAccessURLManager"?class="com.lgm.mybatis.manager.PageAccessURLManager">??????????<property?name="sqlSessionFactory"?ref="sqlSessionFactory"?/>??????</bean>????</beans>??
關(guān)于spring的其他數(shù)據(jù)源配置,這里就不寫了。
?
4)mybatis的配置文件:
[html] view plaincopy
<?xml?version="1.0"?encoding="UTF-8"??>??<!DOCTYPE?configuration?PUBLIC?"-//mybatis.org//DTD?Config?3.0//EN"??"http://mybatis.org/dtd/mybatis-3-config.dtd">????<configuration>??????<settings>????????????????????<setting?name="cacheEnabled"?value="true"/>????????????????????<setting?name="lazyLoadingEnabled"?value="true"/>????????????????????<setting?name="aggressiveLazyLoading"?value="true"/>????????????????????<setting?name="multipleResultSetsEnabled"?value="true"/>????????????????????<setting?name="useColumnLabel"?value="true"/>????????????????????<setting?name="useGeneratedKeys"?value="true"/>????????????????????<setting?name="autoMappingBehavior"?value="PARTIAL"/>????????????????????<setting?name="defaultExecutorType"?value="SIMPLE"/>????????????????????<setting?name="defaultStatementTimeout"?value="25000"/>??????</settings>??????????????????<typeAliases>??????????<typeAlias?alias="pageAccessURL"??type="com.lgm.mybatis.model.PageAccessURL"?/>??????</typeAliases>????????????????????<mappers>??????????<mapper?resource="com/lgm/mybatis/config/pageAccessURL.xml"/>??????</mappers>????</configuration>??
使用了spring管理的話,這里就不用設置數(shù)據(jù)源等其他配置了
?
5)mybatis的sql映射文件配置:
同方式一配置的sql映射文件配置
?
6)配置DAO層:
[java] view plaincopy
public?class?PageAccessURLManager?{??????????private?SqlSessionFactory?sqlSessionFactory?;????????public?void?setSqlSessionFactory(SqlSessionFactory?sqlSessionFactory)?{??????????this.sqlSessionFactory?=?sqlSessionFactory;??????}????????????public?PageAccessURL?getPageAccessURL(int?url_id){??????????PageAccessURL?page?=?(PageAccessURL)sqlSessionFactory.openSession().selectOne("selectPageAccessURL",url_id);??????????System.out.println(page.getUrl());??????????return?page;??????}??}??
7)測試:
[java] view plaincopy
public?void?testSelect()?{??????ApplicationContext?tx?=?new?ClassPathXmlApplicationContext("applicationContext.xml");??????PageAccessURLManager?page?=?(PageAccessURLManager)tx.getBean("pageAccessURLManager");??????page.getPageAccessURL(123456);??}??
?
第三種方式(ibatis2.3+spring3):
1)jar包:
mysql-connector-java-3.1.13.jar
log4j-1.2.16.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
commons-logging-1.1.1.jar
spring-asm-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-jdbc-3.0.5.RELEASE.jar
spring-tx-3.0.5.RELEASE.jar
commons-dbcp-1.2.2.jar
commons-pool-1.3.jar
ibatis-2.3.0.677.jar
junit.jar
?
2)spring配置文件:
applicationContext.xml
[html] view plaincopy
<?xml?version="1.0"?encoding="UTF-8"??>??<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">????<beans>??????<import?resource="applicationContext-dao.xml"?/>??</beans>?? applicationContext-dao.xml
[html] view plaincopy
<?xml?version="1.0"?encoding="UTF-8"??>??<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">????<beans>????????<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="configLocation"?value="classpath:mybatis-config-mappings.xml"?/>??????????<property?name="dataSource"?ref="dataSource"?/>??????</bean>??????????????<bean?id="dataSource"?class="org.apache.commons.dbcp.BasicDataSource"?destroy-method="close">??????????<property?name="driverClassName"?value="com.mysql.jdbc.Driver"?/>??????????<property?name="url"?value="jdbc:mysql://localhost:3306/appdb"?/>??????????<property?name="username"?value="root"?/>??????????<property?name="password"?value="123456"?/>??????????<property?name="maxActive"?value="100"?/>??????????<property?name="maxIdle"?value="5"?/>??????????<property?name="minEvictableIdleTimeMillis"?value="300000"?/>??????????<property?name="timeBetweenEvictionRunsMillis"?value="120000"?/>??????????<property?name="validationQuery"?value="SELECT?1"?/>??????????<property?name="testWhileIdle"?value="true"?/>??????????<property?name="testOnReturn"?value="true"?/>??????????<property?name="testOnBorrow"?value="true"?/>??????</bean>????????<bean?id="pageAccessURLManager"?class="com.lgm.mybatis.manager.PageAccessURLManager">??????????<property?name="sqlMapClient"?ref="sqlMapClient"?/>??????</bean>????</beans>??
3)ibatis配置文件:
[html] view plaincopy
<?xml?version="1.0"?encoding="UTF-8"??>??<!DOCTYPE?sqlMapConfig?PUBLIC?"-//iBATIS.com//DTD?SQL?Map?Config?2.0//EN"?"http://www.ibatis.com/dtd/sql-map-config-2.dtd">????<sqlMapConfig>??????<settings?cacheModelsEnabled="true"?enhancementEnabled="true"??????????lazyLoadingEnabled="true"?maxRequests="32"?maxSessions="10"??????????maxTransactions="5"?useStatementNamespaces="false"?/>??????????????????<typeAlias?alias="pageAccessURL"??type="com.lgm.mybatis.model.PageAccessURL"?/>????????????<sqlMap?resource="com/lgm/mybatis/config/pageAccessURL.xml"/>????????</sqlMapConfig>??
4)ibatis的sql映射配置文件:
[html] view plaincopy
<?xml?version="1.0"?encoding="UTF-8"??>??<!DOCTYPE?sqlMap??PUBLIC?"-//iBATIS.com//DTD?SQL?Map?2.0//EN"?"http://www.ibatis.com/dtd/sql-map-2.dtd">??<sqlMap?namespace="pageAccessURL">????????????<cacheModel?id="productCache"?type="LRU">??????????<flushInterval?hours="24"/>??????????<property?name="size"?value="1000"?/>??????</cacheModel>????????<select?id="selectPageAccessURL"?parameterClass="int"?resultClass="pageAccessURL"?cacheModel="productCache">??????????select?*?from?PAGE_ACCESS_URL?where?URL_ID?=?#id#??????</select>????????????<select?id="selectPageAccessURLByClass"?parameterClass="pageAccessURL"?resultClass="pageAccessURL">??????????select?*?from?PAGE_ACCESS_URL?where?URL_ID?=?#urlId#?and?URL?=?#url#??????</select>????????<sql?id="usercolumns">URL_ID?as?urlId,url,moduleId,state,mark</sql>??????<select?id="selectPageAccessURL2"?parameterClass="int"?resultClass="pageAccessURL">??????????select?<include?refid="usercolumns"?/>????????????from?PAGE_ACCESS_URL?where?URL_ID?=?#id#??????</select>????????????<insert?id="insertTest"?>??????????<selectKey?keyProperty="id"?resultClass="int"?>??????????????SELECT?FLOOR(1?+?(RAND()?*?1000000));???????????</selectKey>??????????insert?into?table?values(xx,xx);??????</insert>????</sqlMap>??
5)配置DAO層:
[html] view plaincopy
public?class?PageAccessURLManager?{????????private?SqlMapClient?sqlMapClient?;??????public?void?setSqlMapClient(SqlMapClient?sqlMapClient)?{??????????this.sqlMapClient?=?sqlMapClient;??????}????????????public?void?getPageAccessURL(int?urlId)?throws?SQLException{??????????PageAccessURL?page?=?(PageAccessURL)this.sqlMapClient.queryForObject("selectPageAccessURL",?urlId);??????????System.out.println(page.getUrl());??????}????????????}??
注意:請仔細對比mybatis和ibatis的配置區(qū)別。
?
6)測試:
同方式二的測試;
?
?
?關(guān)于注解方式的我不是很喜歡,所以...
over....
轉(zhuǎn)載于:https://www.cnblogs.com/nizuimeiabc1/p/4254230.html
總結(jié)
以上是生活随笔為你收集整理的mybatis、ibatis 和spring集成的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。