更新時間:2022-03-23 10:19:38 來源:動力節點 瀏覽1485次
myBatis是一個持久層ORM框架,也就是一個半成品軟件,它解決了軟件開發中的通用性問題,簡化了開發步驟,提高了開發效率。
ORM:對象關系映射。對象關系映射,將程序中的對象與表中記錄的數據一一對應。
1.支持自定義sql、存儲過程和高級映射
2.對于原生的JDBC操作進行了封裝,幾乎消除了多余的jdbc代碼
3.支持xml配置sql和注解配置sql,并自動進行ORM操作,返回結果。
1. 有很多冗余代碼
2.手動訪問,關閉連接
3.需要封裝ResultSet
4.效率不高,沒有緩存
一種。在db.properties中配置數據庫連接參數
--<properties resource="db.properties"></properties>
--<property name="driver" value="${driver}"/>
Copy code
用druid替換連接池——定義連接池工廠,繼承UnpooledDataSourceFactory提供構造方法,在該方法中,給父類dataSource賦值在 mybatis 的主配置文件中,通過 type 指向自定義的dataSourceFactory
配置實體類別名
方式一:基于xml來配置
<typeAliases>
// Define an alias for a single entity class type: type alias: Alias
<!--<typeAlias type="com.qf.entity.Sale" alias="sale"></typeAlias>-->
// Batch define aliases for entity classes under the package by scanning the package , The default class name is lowercase or uppercase
<package name="com.qf.entity"/>
</typeAliases>
Copy code
方式二:基于注解配置
實體類的屬性名與字段不一致
方式一:定義別名
方式二:將 resultType 定義為 map
<select id="findById2" resultType="map/int/double/javaBean">
select * from sale where id=#{id}
</select>
Copy code
方式三:自定義resultMap,指定字段名和屬性名的對應關系
<select id="findById3" resultMap="saleMap">
select * from sale where id=#{id}
</select>
<resultMap id="saleMap" type="sale1">
<id column="id" property="id"></id>
<result column="prodName" property="name"></result>
<!--<result column="qty" property="qty"></result>-->
</resultMap>
Copy code
以上就是關于“MyBatis性能優化介紹”,如果大家對此比較感興趣,想了解更多相關知識,不妨來關注一下動力節點的Mybatis-Plus視頻教程,里面的課程內容細致全面,通俗易懂,很適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習