版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
MyBatis的基礎(chǔ)知識(shí)本課程將深入淺出地介紹MyBatis框架的基礎(chǔ)知識(shí),包括其發(fā)展歷程、核心組件、配置、數(shù)據(jù)類型、動(dòng)態(tài)SQL語(yǔ)句、參數(shù)傳遞、結(jié)果集映射、緩存機(jī)制、注解開(kāi)發(fā)以及與Spring框架的整合。MyBatis簡(jiǎn)介定義MyBatis是一個(gè)持久層框架,它提供了將Java對(duì)象映射到數(shù)據(jù)庫(kù)記錄的機(jī)制,簡(jiǎn)化了數(shù)據(jù)庫(kù)操作。優(yōu)勢(shì)MyBatis具有簡(jiǎn)單易用、靈活高效、支持多種數(shù)據(jù)庫(kù)、性能優(yōu)越等特點(diǎn)。MyBatis的發(fā)展歷程12002年iBatis誕生,是一個(gè)基于Java的開(kāi)源持久層框架。22010年iBatis更名為MyBatis,并加入了新的功能和特性。32013年MyBatis3.0發(fā)布,引入了注解開(kāi)發(fā)方式。42016年MyBatis3.4發(fā)布,增加了對(duì)SpringBoot的支持。MyBatis的特點(diǎn)1簡(jiǎn)單易用使用XML或注解配置,語(yǔ)法簡(jiǎn)潔易懂。2靈活高效支持自定義SQL語(yǔ)句,能夠滿足各種復(fù)雜的業(yè)務(wù)需求。3支持多種數(shù)據(jù)庫(kù)可以無(wú)縫地與各種主流數(shù)據(jù)庫(kù)進(jìn)行連接。4性能優(yōu)越通過(guò)緩存機(jī)制和動(dòng)態(tài)SQL語(yǔ)句提高性能。為什么使用MyBatis提高開(kāi)發(fā)效率簡(jiǎn)化了數(shù)據(jù)持久層操作,降低了開(kāi)發(fā)成本。增強(qiáng)代碼可讀性通過(guò)XML或注解配置,使代碼結(jié)構(gòu)清晰易懂。提高代碼可維護(hù)性將SQL語(yǔ)句與Java代碼分離,方便維護(hù)和修改。MyBatis的工作原理1.加載配置文件加載MyBatis配置文件(SqlMapConfig.xml)和映射文件。2.創(chuàng)建SqlSessionFactory根據(jù)配置文件創(chuàng)建SqlSessionFactory對(duì)象。3.獲取SqlSession從SqlSessionFactory獲取SqlSession對(duì)象。4.執(zhí)行SQL語(yǔ)句通過(guò)SqlSession執(zhí)行SQL語(yǔ)句,并將結(jié)果映射到Java對(duì)象。5.關(guān)閉SqlSession完成操作后,關(guān)閉SqlSession對(duì)象。MyBatis的核心組件SqlSessionFactory負(fù)責(zé)創(chuàng)建SqlSession對(duì)象。SqlSession執(zhí)行SQL語(yǔ)句,并將結(jié)果映射到Java對(duì)象。Mapper定義SQL語(yǔ)句和結(jié)果映射規(guī)則。配置數(shù)據(jù)源<environmentid="development"><transactionManagertype="JDBC"></transactionManager><dataSourcetype="POOLED"><propertyname="driver"value="com.mysql.cj.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/mybatis_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"/><propertyname="username"value="root"/><propertyname="password"value="password"/></dataSource></environment>編寫(xiě)SqlMapConfig.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-////DTDConfig3.0//EN""/dtd/mybatis-3-config.dtd"><configuration><environmentsdefault="development"><!--數(shù)據(jù)源配置--><environmentid="development"><transactionManagertype="JDBC"></transactionManager><dataSourcetype="POOLED"><propertyname="driver"value="com.mysql.cj.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/mybatis_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"/><propertyname="username"value="root"/><propertyname="password"value="password"/></dataSource></environment></environments><!--映射文件配置--><mappers><mapperresource="mapper/UserMapper.xml"/></mappers></configuration>編寫(xiě)映射文件<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-////DTDMapper3.0//EN""/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.example.mapper.UserMapper"><selectid="getUserById"resultType="com.example.entity.User">select*fromuserwhereid=#{id}</select></mapper>MyBatis支持的數(shù)據(jù)類型INTEGERIntegerVARCHARStringDATEDateTIMESTAMPDate動(dòng)態(tài)SQL語(yǔ)句動(dòng)態(tài)SQL語(yǔ)句是指根據(jù)條件動(dòng)態(tài)生成SQL語(yǔ)句,可以提高代碼的靈活性和可維護(hù)性。IF條件判斷<selectid="getUserByNameAndAge"resultType="com.example.entity.User">select*fromuser<where><iftest="name!=nullandname!=''">name=#{name}</if><iftest="age!=null">andage=#{age}</if></where></select>Choose條件判斷<selectid="getUserByStatus"resultType="com.example.entity.User">select*fromuser<where><choose><whentest="status==1">status=1</when><whentest="status==2">status=2</when><otherwise>statusin(1,2)</otherwise></choose></where></select>Where動(dòng)態(tài)SQL<selectid="getUserByConditions"resultType="com.example.entity.User">select*fromuser<where><iftest="name!=nullandname!=''">name=#{name}</if><iftest="age!=null">andage=#{age}</if></where></select>動(dòng)態(tài)SQL之Trim<selectid="getUserByMultipleConditions"resultType="com.example.entity.User">select*fromuser<where><trimprefix="("suffix=")"prefixOverrides="and"><iftest="name!=nullandname!=''">name=#{name}</if><iftest="age!=null">andage=#{age}</if><iftest="email!=nullandemail!=''">andemail=#{email}</if></trim></where></select>動(dòng)態(tài)SQL之Set<updateid="updateUser"parameterType="com.example.entity.User">updateuser<set><iftest="name!=nullandname!=''">name=#{name},</if><iftest="age!=null">age=#{age},</if><iftest="email!=nullandemail!=''">email=#{email},</if></set>whereid=#{id}</update>MyBatis的參數(shù)傳遞MyBatis支持多種參數(shù)傳遞方式,可以根據(jù)實(shí)際情況選擇合適的方案。單個(gè)參數(shù)傳遞<selectid="getUserById"resultType="com.example.entity.User">select*fromuserwhereid=#{id}</select>多個(gè)參數(shù)傳遞<selectid="getUserByNameAndAge"resultType="com.example.entity.User">select*fromuserwherename=#{name}andage=#{age}</select>使用Map傳參<selectid="getUserByConditions"resultType="com.example.entity.User">select*fromuser<where><iftest="name!=nullandname!=''">name=#{name}</if><iftest="age!=null">andage=#{age}</if></where></select>使用JavaBean傳參<selectid="getUserByConditions"resultType="com.example.entity.User">select*fromuser<where><iftest="name!=nullandname!=''">name=#{name}</if><iftest="age!=null">andage=#{age}</if></where></select>結(jié)果集映射結(jié)果集映射是指將數(shù)據(jù)庫(kù)查詢結(jié)果映射到Java對(duì)象的機(jī)制。一對(duì)一關(guān)聯(lián)關(guān)系<resultMapid="userResultMap"type="com.example.entity.User"><idcolumn="id"property="id"/><resultcolumn="name"property="name"/><resultcolumn="age"property="age"/><associationproperty="address"javaType="com.example.entity.Address"column="address_id"><idcolumn="address_id"property="id"/><resultcolumn="street"property="street"/><resultcolumn="city"property="city"/></association></resultMap>一對(duì)多關(guān)聯(lián)關(guān)系<resultMapid="userResultMap"type="com.example.entity.User"><idcolumn="id"property="id"/><resultcolumn="name"property="name"/><resultcolumn="age"property="age"/><collectionproperty="orders"javaType="java.util.List"ofType="com.example.entity.Order"column="id"><idcolumn="order_id"property="id"/><resultcolumn="product_name"property="productName"/><resultcolumn="price"property="price"/></collection></resultMap>多對(duì)多關(guān)聯(lián)關(guān)系<resultMapid="userResultMap"type="com.example.entity.User"><idcolumn="id"property="id"/><resultcolumn="name"property="name"/><resultcolumn="age"property="age"/><collectionproperty="roles"javaType="java.util.List"ofType="com.example.entity.Role"column="id"><idcolumn="role_id"property="id"/><resultcolumn="role_name"property="roleName"/></collection></resultMap>延遲加載延遲加載是指在需要時(shí)才加載關(guān)聯(lián)對(duì)象,可以提高查詢效率。一級(jí)緩存一級(jí)緩存是SqlSession級(jí)別的緩存,默認(rèn)開(kāi)啟,緩存數(shù)據(jù)只在當(dāng)前SqlSession有效。二級(jí)緩存二級(jí)緩存是Mapper級(jí)別的緩存,默認(rèn)關(guān)閉,需要在SqlMapConfig.xml中配置開(kāi)啟。注解開(kāi)發(fā)MyBatis支持使用注解進(jìn)行開(kāi)發(fā),簡(jiǎn)化了XML配置。注解之@Select@Select("select*fromuserwhereid=#{id}")UsergetUserById(@Param("id")Integerid);注解之@Insert@Insert("insertintouser(name,age)values(#{name},#{age})")intinsertUser(Useruser);注解之@Update@Update("updateusersetname=#{name},age=#{age}whereid=#{id}")intupdateUser(Useruser);注解之@Delete@Delete("deletefromuserwhereid=#{id}")intdeleteUser(Integerid);注解之@Param@Select("select*fromuserwherename=#{userName}andage=#{userAge}")UsergetUserByNameAndAge(@Param("userName")Stringname,@Param("userAge")Integerage);注解之@ResultMap@Select("select*fromuser")@Results(id="userResultMap",value={@Result(column="id",property="id"),@Result(column="name",property="name"),@Result(column="age",property="age")})List<User>getAllUsers();注解之@One@Select("select*fromuser")@Results(id="userResultMap",value={@Result(column="id",property="id"),@Result(column="name",property="name"),@Result(column="age",property="age"),@Result(property="address",column="address_id",javaType=Address.class,one=@One(select="com.example.mapper.AddressMapper.getAddressById"))})List<User>getAllUsers();注解之@Many@Select("select*fromuser")@Results(id="userResultMap",value={@Result(column="id",property="id"),@Result(column="name",property="name"),@Result(column="age",property="age"),@Result(property="orders",column="id",javaType=List.class,many=@Many(select="com.example.mapper.OrderMapper.getOrdersByUserId"))})List<User>getAllUsers();注解之@CacheNamespace@CacheNamespace(flushInterval=60000,readOnly=true)publicinterfaceUserMapper{//...}整合SpringMyBatis可以與Spring框架進(jìn)行整合,方便管理數(shù)據(jù)源、SqlSessionFactory和Mapper。整合Spring之整合數(shù)據(jù)源<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"value="com.mysql.cj.jdb
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年及未來(lái)5年市場(chǎng)數(shù)據(jù)中國(guó)贛州市酒店行業(yè)市場(chǎng)發(fā)展數(shù)據(jù)監(jiān)測(cè)及投資前景展望報(bào)告
- 2026年及未來(lái)5年市場(chǎng)數(shù)據(jù)中國(guó)永磁同步電機(jī)行業(yè)市場(chǎng)競(jìng)爭(zhēng)格局及發(fā)展趨勢(shì)預(yù)測(cè)報(bào)告
- 2026年及未來(lái)5年市場(chǎng)數(shù)據(jù)中國(guó)木制餐具行業(yè)市場(chǎng)深度分析及投資策略研究報(bào)告
- 2026年及未來(lái)5年市場(chǎng)數(shù)據(jù)中國(guó)船舶壓載水檢測(cè)行業(yè)發(fā)展?jié)摿︻A(yù)測(cè)及投資策略研究報(bào)告
- 2026年安徽交通控股集團(tuán)合肥高速公路管理中心收費(fèi)協(xié)管員招聘8名考試備考試題及答案解析
- 2026福建福州市教育局公費(fèi)師范生專項(xiàng)招聘92人筆試模擬試題及答案解析
- 2026廣西貴港市事業(yè)單位公開(kāi)招聘工作人員777人筆試參考題庫(kù)及答案解析
- 上海市執(zhí)法類公務(wù)員招錄體能測(cè)評(píng)健康承諾書(shū)考試備考試題及答案解析
- 2026年杭州市拱墅區(qū)人民政府武林街道辦事處公開(kāi)招聘編外工作人員4人考試備考題庫(kù)及答案解析
- 2026年中鐵西北科學(xué)研究院有限公司中鐵防災(zāi)技術(shù)中心招聘考試參考試題及答案解析
- 藥店物價(jià)收費(fèi)員管理制度
- 數(shù)據(jù)風(fēng)險(xiǎn)監(jiān)測(cè)管理辦法
- 國(guó)家開(kāi)放大學(xué)《公共政策概論》形考任務(wù)1-4答案
- 肝惡性腫瘤腹水護(hù)理
- 兒童語(yǔ)言發(fā)育遲緩課件
- 2025年河南省鄭州市中考一模英語(yǔ)試題及答案
- 《高等職業(yè)技術(shù)院校高鐵乘務(wù)專業(yè)英語(yǔ)教學(xué)課件》
- DB15T 3758-2024基本草原劃定調(diào)整技術(shù)規(guī)程
- 醫(yī)學(xué)類單招入學(xué)考試題庫(kù)及答案(修正版)
- 腦機(jī)接口技術(shù)在疼痛管理中的應(yīng)用研究
- 《項(xiàng)目經(jīng)理安全管理培訓(xùn)課件》
評(píng)論
0/150
提交評(píng)論