(1) mybatis數據源的源碼解析 xml(environment) > java Environmenthtml
>org.apache.ibatis.session.SqlSessionFactoryBuilder.build(java.io.InputStream)
>org.apache.ibatis.builder.xml.XmlConfigBuilder
>org.apache.ibatis.builder.xml.XmlConfigBuilder.parseConfiguration
>org.apache.ibatis.builder.xml.XmlConfigBuilder.environmentsElement
>org.apache.ibatis.builder.xml.XmlConfigBuilder.dataSourceElement
>org.apache.ibatis.datasource.DataSourceFactory.getDataSource
複製代碼
(2) mybatis sql源碼解析 sql > MappedStatementjava
>org.apache.ibatis.builder.xml.XmlConfigBuilder.mapperElement
>org.apache.ibatis.builder.xml.XmlMapperBuilder
>org.apache.ibatis.builder.xml.XmlMapperBuilder.parse
>org.apache.ibatis.builder.xml.XmlMapperBuilder.buildStatementFoemContext
>org.apache.ibatis.builder.xml.XmlStatementBuilder
>org.apache.ibatis.builder.xml.XmlStatementBuilder.parseStatementNode
複製代碼
(3) mybatis 如何操做數據庫 SimpleExecutor 執行器mysql
>org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession
>org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFormDataSource
>org.apache.ibatis.session.Configuration.newExcetor
>org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne
>org.apache.ibatis.session.Configuration.getMappedStatement
>org.apache.ibatis.executor.BaseExecutor.queryFormDatabase
>org.apache.ibatis.executor.SimpleExecutor.doQuery
複製代碼
① SimpleExecutor是Mybatis執行Mapper語句時默認使用的Executor。它提供最基本的Mapper語句執行功能,沒有過多的封裝的sql
② ReuseExecutor,顧名思義,是能夠重用的Executor。它重用的是Statement對象,它會在內部利用一個Map把建立的Statement都緩存起來,每次在執行一條SQL語句時,它都會去判斷以前是否存在基於該SQL緩存的Statement對象,存在並且以前緩存的Statement對象對應的Connection尚未關閉的時候就繼續用以前的Statement對象,不然將建立一個新的Statement對象,並將其緩存起來。由於每個新的SqlSession都有一個新的Executor對象,因此咱們緩存在ReuseExecutor上的Statement的做用域是同一個SqlSession。數據庫
③ BatchExecutor的設計主要是用於作批量更新操做的。其底層會調用Statement的executeBatch()方法實現批量操做。apache
(1) 屬性配置緩存
<properties resource="app.properties">
<property name="jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
複製代碼
三種設置方式:bash
①構建sessionFactory 時傳遞 (優先級:高)session
②基於resource 屬性加載 或 url 加載 (優先級:中)mybatis
③基於 屬性設置 (優先級:低)
(2) 全局參數設置
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
複製代碼
具體參考:www.mybatis.org/mybatis-3/z…
(3) 環境配置
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED" >
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
複製代碼
事物管理器:JDBC|MANAGED
(4) 數據源
unpooled 普通鏈接,每次獲取時都會從新創建一個新的鏈接.屬性下以下:
• driver :數據庫驅動類
• url: URL地址
• username:用戶名
• password :登陸數據庫的密碼
pooled: 鏈接池模式,全部鏈接從鏈接池當中獲取,由鏈接池來來進行鏈接的創建與回收關於等操做,除支持unpooled屬性外還支持屬性以下:
• poolMaximumActiveConnections : 最大活躍數,默認值:10
• poolMaximumIdleConnections :最大空閒鏈接數
• poolMaximumCheckoutTime :獲取鏈接超時等待最大(checked out)時間,默認值:20000 毫秒
• poolTimeToWait : 單次獲取鏈接 最大等待時間 默認:20000 毫秒(即 20 秒)。
• poolMaximumLocalBadConnectionTolerance 獲取鏈接重試次數 默認:3
• poolPingQuery 用於檢測鏈接是否斷開的測試 語句
• poolPingEnabled 是否經過執行poolPingQuery 語句作檢測,默認值:false。
• poolPingConnectionsNotUsedFor 鏈接檢測間隔時間 ,默認60000。
(5) typeAliases 別名配置
<typeAliases>
<typeAlias type="com.niuh.dao"/>
<typeAlias type="com.niuh.dao.UserInfo" alias="UserInfo"/>
</typeAliases>
複製代碼
(6) mappers 文件引入
<mappers>
<mapper resource="userInfo.xml" />
<package name="com.niuh.dao"/>
</mappers>
複製代碼
基於 mapper 引入指定資源文件: resource| url |class 基於package 引入:掃描指定包路徑當下的url
(1) mapper 經常使用元素
select – 映射查詢語
insert – 映射插入語句
update – 映射更新語句
sql – 可被其餘語句引用的可重用語句塊
delete – 映射刪除語句
resultMap 用來描述如何從數據庫結果集中來加載對象
cache – 給定命名空間的緩存配置
cache-ref – 其餘命名空間緩存配置的引用
複製代碼
(2) <select>
查詢標籤 其表示一個查詢語句映謝,其簡單示例以下: 示例
<select id="selectUser" resultType="com.niuh.mybatis.test.UserInfo">
select * from user_info where id = #{id}
</select>
複製代碼
其支持屬性以下:
<select
id="selectUser" // statement id
parameterType="int" // 參數類型
resultType="hashmap"// 返回結果類別
resultMap="personResultMap" // 返回結果映射
flushCache="false" //
useCache="true"
timeout="10000"
fetchSize="256"
statementType="PREPARED">
複製代碼
參數的引用的辦法
#{id, mode=in, jdbcType=INT, jdbcTypeName=MY_TYPE}
複製代碼
(3) 示例 標籤
<insert id="addUser"
parameterType="com.niuh.mybatis.test.UuserInfo">
INSERT INTO user_info (user_name,nick_name,password) VALUES
(#{userName},#{nickName},#{password})
</insert>
複製代碼
<update id="updateUser"
parameterType="com.niuh.mybatis.test.UuserInfo" >
update user_info set user_name=#{userName} where id=#{id}
</update>
複製代碼
<delete id="deleteUser" parameterType="int">
DELETE from user_info where id=#{id}
</delete>
複製代碼
屬性說明:
<insert
id="insertUuser"
parameterType="com.niuh.mybatis.test.UuserInfo"
flushCache="true"
statementType="PREPARED"
keyProperty=""
keyColumn=""
useGeneratedKeys=""
timeout="20">
複製代碼
<update
id="updateUuser"
parameterType="com.niuh.mybatis.test.UuserInfo"
flushCache="true"
statementType="PREPARED"
timeout="20">
複製代碼
<delete
id="deleteUuser"
parameterType="com.niuh.mybatis.test.UuserInfo"
flushCache="true"
statementType="PREPARED"
timeout="20">
複製代碼
(4) 標籤 將重複的sql 語句定文爲一個字段
<sql id="base_colume"> id,user_name,nick_name</sql>
複製代碼
可經過 進行引入 如:
<include refid="base_colume"/>
複製代碼
(5) 標籤 resultMap 是myBatis 對象的映射
標籤 trim (where, set) 標籤 foreach 標籤
myBatis 專一於sql 自己,其爲sql 映謝而非完整的ORM,須要本身編寫sql 語句,這是其優勢也是缺點。優勢是:優化方便,可更好利用sql編寫經驗。缺點是當數據修改以後調整麻煩耗費時間長.
試用場景:適用於對性能要求較高,有大批量的查詢修改,而且業務實現沒有過多依懶數據關係模型,好比:電商、O2O等互聯網項目。
互聯網項目對DAO層的要求:
一、對數據庫的訪問更新純粹
二、儘量不要使用數據庫作運算
三、SQL語句能夠針對性的優化(減小查詢字段、查條件排序例 、查詢條件儘量命中索引)