版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
10.1SqlSession的使用范圍10.2原始DAO開發(fā)方法10.3Mapper接口開發(fā)方法10.1SqlSession的使用范圍10.1.1SqlSessionFactoryBuilderSqlSessionFactoryBuilder用于創(chuàng)建SqlSessionFacoty,一旦SqlSessionFacoty創(chuàng)建完成就不需要SqlSessionFactoryBuilder了。此處可將SqlSessionFactoryBuilder當(dāng)成一個(gè)工具類使用,其最佳使用范圍是方法范圍即方法體內(nèi)局部變量。10.1.2SqlSessionFactorySqlSessionFactory是一個(gè)接口,接口中定義了openSession的不同重載方法,SqlSessionFactory的最佳使用范圍是整個(gè)應(yīng)用運(yùn)行期間,一旦創(chuàng)建后可以重復(fù)使用,通常以單例模式管理SqlSessionFactory。10.1.3SqlSessionSqlSession是一個(gè)面向用戶的接口,定義了數(shù)據(jù)庫(kù)操作,默認(rèn)使用DefaultSqlSession實(shí)現(xiàn)類。執(zhí)行過(guò)程如下:①加載數(shù)據(jù)源等配置信息。Environmentenvironment=configuration.getEnvironment();②創(chuàng)建數(shù)據(jù)庫(kù)鏈接。③創(chuàng)建事務(wù)對(duì)象。④創(chuàng)建Executor,SqlSession所有操作都是通過(guò)Executor完成。MyBatis源碼如下:if(ExecutorType.BATCH==executorType){executor=newBatchExecutor(this,transaction);}elseif(ExecutorType.REUSE==executorType){executor=newReuseExecutor(this,transaction);}else{executor=newSimpleExecutor(this,transaction);}if(cacheEnabled){executor=newCachingExecutor(executor,autoCommit);}⑤SqlSession的實(shí)現(xiàn)類即DefaultSqlSession,此對(duì)象中對(duì)操作數(shù)據(jù)庫(kù)實(shí)質(zhì)上用的是Executor。結(jié)論:每個(gè)線程都應(yīng)該有它自己的SqlSession實(shí)例。SqlSession的實(shí)例不能共享使用,它也是線程不安全的。因此最佳的范圍是請(qǐng)求或方法范圍。絕對(duì)不能將SqlSession實(shí)例的引用放在一個(gè)類的靜態(tài)字段或?qū)嵗侄沃?。打開一個(gè)SqlSession;使用完畢就要關(guān)閉它。通常把這個(gè)關(guān)閉操作放到finally塊中以確保每次都能執(zhí)行關(guān)閉。如下:SqlSessionsession=sqlSessionFactory.openSession();try{//dowork}finally{session.close();}10.2原始DAO開發(fā)方法10.2.1映射文件<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-////DTDMapper3.0//EN""/dtd/mybatis-3-mapper.dtd"><mappernamespace="test"><!--根據(jù)id獲取用戶信息--><selectid="findUserById"parameterType="int"resultType="cn.itcast.mybatis.po.User">select*fromuserwhereid=#{id}</select><!--添加用戶--><insertid="insertUser"parameterType="cn.itcast.mybatis.po.User"><selectKeykeyProperty="id"order="AFTER"resultType="java.lang.Integer">selectLAST_INSERT_ID()</selectKey>insertintouser(username,birthday,sex,address)values(#{username},#{birthday},#{sex},#{address})</insert></mapper>10.2.2DAO接口PublicinterfaceUserDao{publicUsergetUserById(intid)throwsException;publicvoidinsertUser(Useruser)throwsException;}PublicclassUserDaoImplimplementsUserDao{//注入SqlSessionFactorypublicUserDaoImpl(SqlSessionFactorysqlSessionFactory){this.setSqlSessionFactory(sqlSessionFactory);}
privateSqlSessionFactorysqlSessionFactory;@OverridepublicUsergetUserById(intid)throwsException{SqlSessionsession=sqlSessionFactory.openSession();Useruser=null;try{//通過(guò)SqlSession調(diào)用selectOne方法獲取一條結(jié)果集//參數(shù)1:指定定義的statement的id,參數(shù)2:指定向statement中傳遞的參數(shù)
user=session.selectOne("test.findUserById",1);System.out.println(user);}finally{session.close();}returnuser;}@OverridePublicvoidinsertUser(Useruser)throwsException{SqlSessionsqlSession=sqlSessionFactory.openSession();try{sqlSession.insert("insertUser",user);
sqlSmit();}finally{session.close();}}}10.3Mapper接口開發(fā)方法10.3.1實(shí)現(xiàn)原理Mapper接口開發(fā)方法只需要程序員編寫Mapper接口(相當(dāng)于DAO接口),由Mybatis框架根據(jù)接口定義創(chuàng)建接口的動(dòng)態(tài)代理對(duì)象,代理對(duì)象的方法體同上邊DAO接口實(shí)現(xiàn)類方法。Mapper接口開發(fā)需要遵循以下規(guī)范:(1)Mapper.xml文件中的namespace與mapper接口的類路徑相同。(2)Mapper接口方法名和Mapper.xml中定義的每個(gè)statement的id相同。(3)Mapper接口方法的輸入?yún)?shù)類型和mapper.xml中定義的每個(gè)sql的parameterType的類型相同。(4)Mapper接口方法的輸出參數(shù)類型和mapper.xml中定義的每個(gè)sql的resultType的類型相同。10.3.2Mapper.xml(映射文件)定義Mapper映射文件UserMapper.xml(內(nèi)容同Users.xml),需要將namespace的值修改為UserMapper接口路徑。將UserMapper.xml放在classpath的mapper目錄下。<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-////DTDMapper3.0//EN""/dtd/mybatis-3-mapper.dtd"><mappernamespace="cn.itcast.mybatis.mapper.UserMapper"><!--根據(jù)id獲取用戶信息--><selectid="findUserById"parameterType="int"resultType="cn.itcast.mybatis.po.User">select*fromuserwhereid=#{id}</select><!--自定義條件查詢用戶列表--><selectid="findUserByUsername"parameterType="java.lang.String"resultType="cn.itcast.mybatis.po.User">select*fromuserwhereusernamelike'%${value}%'</select><!--添加用戶--><insertid="insertUser"parameterType="cn.itcast.mybatis.po.User"><selectKeykeyProperty="id"order="AFTER"resultType="java.lang.Integer">selectLAST_INSERT_ID()</selectKey>insertintouser(username,birthday,sex,address)values(#{username},#{birthday},#{sex},#{address})</insert></mapper>10.3.3Mapper.java(接口)/***用戶管理Mapper*/PublicinterfaceUserMapper{//根據(jù)用戶id查詢用戶信息publicUserfindUserById(intid)throwsException;//查詢用戶列表publicList<User>findUserByUsername(Stringusername)throwsException;//添加用戶信息publicvoidinsertUser(Useruser)throwsException;}接口定義有如下特點(diǎn):(1)Mapper接口方法名和Mapper.xml中定義的statement的id相同。(2)Mapper接口方法的輸入?yún)?shù)類型和mapper.xml中定義的statement的parameterType的類型相同。(3)Mapper接口方法的輸出參數(shù)類型和mapper.xml中定義的statement的resultType的類型相同。10.3.4加載UserMapper.xml文件修改SqlMapConfig.xml文件:<!--加載映射文件--><mappers><mapperresource="mapper/UserMapper.xml"/></mappers>10.3.5測(cè)試PublicclassUserMapperTestextendsTestCase{privateSqlSessionFactorysqlSessionFactory;protectedvoidsetUp()throwsException{//MyBatis配置文件Stringresource="sqlMapConfig.xml";
InputStreaminputStream=Resources.getResourceAsStream(resource);//使用SqlSessionFactoryBuilder創(chuàng)建sessionFactorysqlSessionFactory=newSqlSessionFactoryBuilder().build(inputStream);}PublicvoidtestFindUserById()throwsException{//獲取sessionSqlSessionsession=sqlSessionFactory.openSession();//獲取Mapper接口的代理對(duì)象UserMapperuserMapper=session.getMapper(UserMapper.class);//調(diào)用代理對(duì)象方法Useruser=userMapper.findUserById(1);System.out.println(user);
//關(guān)閉sessionsession.close();}@TestpublicvoidtestFindUserByUsername()throwsException{SqlSessionsqlSession=sqlSessionFactory.openSession();UserMapperuserM
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 六一活動(dòng)優(yōu)惠策劃方案(3篇)
- 藝術(shù)活動(dòng)策劃方案模板(3篇)
- 水電展板施工方案(3篇)
- 2026四川寧德時(shí)代宜賓區(qū)域生產(chǎn)技術(shù)員招聘3000人筆試備考題庫(kù)及答案解析
- 2026年上海海關(guān)學(xué)院公開招聘筆試備考試題及答案解析
- 2026河南洛陽(yáng)市第一高級(jí)中學(xué)附屬初級(jí)中學(xué)教師招聘12人參考考試題庫(kù)及答案解析
- 護(hù)理案例分享:護(hù)理科研與臨床實(shí)踐的結(jié)合
- 2026江蘇連云港興榆創(chuàng)業(yè)投資有限公司對(duì)外招聘崗位開考情況說(shuō)明備考考試試題及答案解析
- 2026江蘇東布洲科技園集團(tuán)有限公司下屬子公司招聘勞務(wù)派遣人員1人參考考試題庫(kù)及答案解析
- 2026年度菏澤市屬事業(yè)單位公開招聘初級(jí)綜合類崗位人員(9人)備考考試試題及答案解析
- 2026年及未來(lái)5年市場(chǎng)數(shù)據(jù)中國(guó)超細(xì)銅粉行業(yè)發(fā)展趨勢(shì)及投資前景預(yù)測(cè)報(bào)告
- 吞咽障礙患者誤吸的預(yù)防與管理方案
- (新教材)2025年人教版八年級(jí)上冊(cè)歷史期末復(fù)習(xí)全冊(cè)知識(shí)點(diǎn)梳理
- 2025-2026學(xué)人教版八年級(jí)英語(yǔ)上冊(cè)(全冊(cè))教案設(shè)計(jì)(附教材目錄)
- 鋁方通吊頂施工技術(shù)措施方案
- 湖南公務(wù)員考試申論試題(行政執(zhí)法卷)1
- 欠款過(guò)戶車輛協(xié)議書
- 2025年江西省高職單招文化統(tǒng)考(語(yǔ)文)
- 體檢的必要性
- 滾珠絲杠設(shè)計(jì)計(jì)算
- 貴州鋁基新材有限公司25萬(wàn)噸銅鎂鋁鋁基電子電池新材料建設(shè)項(xiàng)目環(huán)評(píng)報(bào)告
評(píng)論
0/150
提交評(píng)論