標籤:
【簡述】
SqlMapConfig.xml是Mybatis的全域設定檔,配置內容如下:
1.properties---------屬性
2.settings-----------全域配置參數
3.typeAliases--------類型別名
4.typeHandlers------類型處理器
5.objectFactory-----對象工廠
6.plugins------------外掛程式
7.environments-----環境集合屬性對象
environments ---------環境子屬性對象
transactionManager----------交易管理
dataSource-------------------資料來源
8.mappers ---------映射器
(1)properties屬性
需求:
將資料庫中的串連參數單獨配置在db.properties中,只需要在sqlMapConfig.xml中載入該db.properties設定檔。
在sqlMapConfig.xml中就不需要對資料庫連接參數寫入程式碼。
配置在db.properties中的優點:方便對參數進行統一的管理,其他的xml檔案可以引用該db.properties
原先的【sqlMapConfig.xml】資料庫相關的配置如下:
修改之後的【sqlMapConfig.xml】如下,注意不要忘了<properties resource="db.properties"></properties>
修改之後 在【db.properties】設定檔內容如下
【MyBatis載入屬性的順序】
1.在properties元素體內定的屬性首先被載入。
2.然後會讀取properties元素中resource或url載入的屬性,它會覆蓋已讀取的同名屬性(所以命名要注意)
3.最後會讀取parameterType傳遞的屬性,它會覆蓋已讀取的同名屬性。
【注意】
不要在<properties>元素體內添加任何屬性值,只將屬性值定義在db.properties檔案中。
在db.properties檔案中定義屬性檔案名稱要有一定的特殊性,例如jdbc.xxx.xxx
(2)settings全域參數配置
MyBatis架構在運行時可以調整一些運行參數
比如:開啟二級緩衝、開啟消極式載入。。。。
(3)typeAliases(別名)(學習重點!)
需求:
在mapper.xml中,定義了很多的statement
【預設的一些別名的源碼】
registerAlias("string", String.class);registerAlias("byte", Byte.class);registerAlias("long", Long.class);registerAlias("short", Short.class);registerAlias("int", Integer.class);registerAlias("integer", Integer.class); //可以發現,parameter="int"或者parameterType="integer"最後都會指向IntegerregisterAlias("double", Double.class);registerAlias("float", Float.class);registerAlias("boolean", Boolean.class);registerAlias("byte[]", Byte[].class);registerAlias("long[]", Long[].class);registerAlias("short[]", Short[].class);registerAlias("int[]", Integer[].class);registerAlias("integer[]", Integer[].class);registerAlias("double[]", Double[].class);registerAlias("float[]", Float[].class);registerAlias("boolean[]", Boolean[].class);registerAlias("_byte", byte.class);registerAlias("_long", long.class);registerAlias("_short", short.class);registerAlias("_int", int.class);registerAlias("_integer", int.class);registerAlias("_double", double.class);registerAlias("_float", float.class);registerAlias("_boolean", boolean.class);registerAlias("_byte[]", byte[].class);registerAlias("_long[]", long[].class);registerAlias("_short[]", short[].class);registerAlias("_int[]", int[].class);registerAlias("_integer[]", int[].class);registerAlias("_double[]", double[].class);registerAlias("_float[]", float[].class);registerAlias("_boolean[]", boolean[].class);registerAlias("date", Date.class);registerAlias("decimal", BigDecimal.class);registerAlias("bigdecimal", BigDecimal.class);registerAlias("biginteger", BigInteger.class);registerAlias("object", Object.class);registerAlias("date[]", Date[].class);registerAlias("decimal[]", BigDecimal[].class);registerAlias("bigdecimal[]", BigDecimal[].class);registerAlias("biginteger[]", BigInteger[].class);registerAlias("object[]", Object[].class);registerAlias("map", Map.class);registerAlias("hashmap", HashMap.class);registerAlias("list", List.class);registerAlias("arraylist", ArrayList.class);registerAlias("collection", Collection.class);registerAlias("iterator", Iterator.class);registerAlias("ResultSet", ResultSet.class);
【針對自己定義的pojo類型的別名】
在【sqlMapper.xml中】,針對單個別名的定義
在【sqlMapper.xml中】,針對批量別名的定義(批量的方式較為常用)
指定包名,MyBatis自動掃描包中的pojo類,自動定義別名,別名就是類名(首字母大小寫均可)
(4)typeHandlers(類型處理器)
MyBatis中通過typeHandlers完成jdbc類型和java類型的轉換。
通常情況下,mybatis提供的類型處理器滿足日常需求,無需自訂。
(5)mappers(映射配置)
1.例子【sqlMapConfig.xml】中通過resource載入單個對應檔的方式:
2.通過mapper介面載入單個對應檔
需要遵循一些規範:需要將XXXmapper.java和XXXmapper.xml映射檔案名稱保持一致,且在一個目錄中。
上邊的規範前提:使用mapper代理方法
3.批量載入
批量載入mapper介面的包名。myBatis自動掃描包下邊的所有的mapper介面進行載入
遵循的規範:需要將XXXmapper.java和XXXmapper.xml映射檔案名稱保持一致,且在一個目錄中。
上邊的規範前提:使用mapper代理方法
11_關於SqlMapperConfig.xml