1 Mapper XML
2 select
The select statement is one of the most popular elements that you'll use in MyBatis. Putting data in a database isn't terribly valuable until you get it back out, so most applications query far more than they modify the data.
在 MyBatis 中,查詢語句是最常用的語句之一筋蓖。向數(shù)據(jù)庫中放入數(shù)據(jù)并沒有多大價值颠蕴,直到你再將它取出匿醒,因此大多數(shù)的應(yīng)用中查詢比修改數(shù)據(jù)多的多熟菲。
For every insert, update or delete, there are probably many selects. This is one of the founding principles of MyBatis, and is the reason so much focus and effort was placed on querying and result mapping.
對每個插入朋贬、修改或刪除操作椭更,通常都對應(yīng)很多的查詢操作。這是 MyBatis 的一個基本原則饶深,也是 MyBatis 在查詢和結(jié)果映射方面如此關(guān)注和努力的原因餐曹。
2.1 select 元素
The select element is quite simple for simple cases.
對于簡單情況而言,select 元素是相當(dāng)簡單的敌厘。
<select id="selectPerson" parameterType="int" resultType="hashmap">
SELECT * FROM PERSON WHERE ID = #{id}
</select>
This statement is called selectPerson, takes a parameter of type int (or Integer), and returns a HashMap keyed by column names mapped to row values.
這個語句被命名為 selectPerson台猴,接收一個 int(或 Integer)類型的參數(shù),并且返回一個 HashMap 類型的對象俱两,其中饱狂,鍵是列名,值為結(jié)果行中對應(yīng)的值宪彩。
Notice the parameter notation: #{id} 休讳。This tells MyBatis to create a PreparedStatement parameter. With JDBC, such a parameter would be identified by a "?" in SQL passed to a new PreparedStatement, something like this:
注意參數(shù)標(biāo)記:#{id}。它告訴 MyBatis 創(chuàng)建一個 PreparedStatement 參數(shù)尿孔。使用 JDBC 時俊柔,這樣的一個參數(shù)在 SQL 中會由一個“?”標(biāo)識,并傳遞給一個新的 PreparedStatement活合。 就像這樣:
// 簡單的 JDBC 代碼, 不是 MyBatis…
String selectPerson = "SELECT * FROM PERSON WHERE ID=?";
PreparedStatement ps = conn.prepareStatement(selectPerson);
ps.setInt(1,id);
Of course, there's a lot more code required by JDBC alone to extract the results and map them to an instance of an object, which is what MyBatis saves you from having to do.
當(dāng)然雏婶,這需要很多單獨(dú)的 JDBC 代碼來提取結(jié)果并將它們映射到一個對象實(shí)例中,這就是 MyBatis 為你節(jié)省精力和時間的地方白指。
2.2 select 的屬性
The select element has more attributes that allow you to configure the details of how each statement should behave.
select 元素提供很多屬性來允許你配置每個語句該如何表現(xiàn)的細(xì)節(jié)留晚。
<select
id="selectPerson"
parameterType="int"
parameterMap="deprecated"
resultType="hashmap"
resultMap="personResultMap"
flushCache="false"
useCache="true"
timeout="10000"
fetchSize="256"
statementType="PREPARED"
resultSetType="FORWARD_ONLY">
id
A unique identifier in this namespace that can be used to reference this statement.
該命名空間中可以用來引用該語句的的唯一標(biāo)識。parameterType
The fully qualified class name or alias for the parameter that will be passed into this statement. This attribute is optional because MyBatis can calculate the TypeHandler to use out of the actual parameter passed to the statement. Default is unset.
傳入這條語句的參數(shù)的完全限定類名或別名告嘲。這個屬性是可選的错维,因?yàn)镸yBatis 可以自己推斷出傳入這條語句的具體參數(shù)對應(yīng)的 TypeHandler。默認(rèn)是未設(shè)置状蜗。parameterMap
This is a deprecated approach to referencing an external parameterMap. Use inline parameter mappings and the parameterType attribute.
這是一個已經(jīng)廢棄的方法需五,用來指向一個外部的 parameterMap。使用行內(nèi)參數(shù)映射和 parameterType 屬性轧坎。resultType
The fully qualified class name or alias for the expected type that will be returned from this statement. Note that in the case of collections, this should be the type that the collection contains, not the type of the collection itself. Use resultType OR resultMap, not both.
語句返回的期望類型的完全限定類名或者別名。注意如果返回的是集合泽示,那么這個類型應(yīng)該是集合包含的類型而不是集合本身缸血。使用 resultType 或 ResultMap,二者取其一械筛。resultMap
A named reference to an external resultMap. Result maps are the most powerful feature of MyBatis, and with a good understanding of them, many difficult mapping cases can be solved. Use resultMap OR resultType, not both.
外部 resultMap 的名稱引用捎泻。結(jié)果映射是 MyBatis 最強(qiáng)大的特性之一,在對它們有了很好的理解之后埋哟,很多困難的映射問題就能迎刃而解笆豁。使用 resultMap 或 resultType郎汪,二者取其一。flushCache
Setting this to true will cause the local and 2nd level caches to be flushed whenever this statement is called. Default: false for select statements.
設(shè)置該屬性為 true 會使本地緩存和二級緩存被清空闯狱,無論何時該語句被調(diào)用煞赢。默認(rèn):false。useCache
Setting this to true will cause the results of this statement to be cached in 2nd level cache. Default: true for select statements.
設(shè)置該屬性為 true 將會使該條語句的執(zhí)行結(jié)果被緩存到二級緩存中哄孤。默認(rèn):true照筑。timeout
This sets the number of seconds the driver will wait for the database to return from a request, before throwing an exception. Default is unset (driver dependent).
該設(shè)置是在拋出異常之前,驅(qū)動等待數(shù)據(jù)庫返回請求結(jié)果的秒數(shù)瘦陈。默認(rèn)未設(shè)置(依賴驅(qū)動)凝危。fetchSize
This is a driver hint that will attempt to cause the driver to return results in batches of rows numbering in size equal to this setting. Default is unset (driver dependent).
這個設(shè)置是嘗試使驅(qū)動批量返回的結(jié)果行數(shù)。默認(rèn)未設(shè)置(依賴驅(qū)動)晨逝。statementType
Any one of STATEMENT, PREPARED or CALLABLE. This causes MyBatis to use Statement, PreparedStatement or CallableStatement respectively. Default: PREPARED.
值為 STATEMENT, PREPARED 或 CALLABLE 其中的一個蛾默,這個設(shè)置使 MyBatis 使用 Statement, PreparedStatement or CallableStatement。默認(rèn):PREPARED捉貌。resultSetType
Any one of FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE. Default is unset (driver dependent).
值為 FORWARD_ONLY支鸡、SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 其中的一個。默認(rèn)未設(shè)置(賴驅(qū)動)昏翰。databaseId
In case there is a configured databaseIdProvider, MyBatis will load all statements with no databaseId attribute or with a databaseId that matches the current one. If case the same statement if found with and without the databaseId the latter will be discarded.
如果配置了一個 databaseIdProvider苍匆,MyBatis 將會加載所有不帶 databaseId 或者匹配當(dāng)前數(shù)據(jù)庫的 databaseId 的語句。如果帶或者不帶 databaseId 的相同語句都有棚菊,那么不帶的會被忽略浸踩。resultOrdered
This is only applicable for nested result select statements: If this is true, it is assumed that nested results are contained or grouped together such that when a new main result row is returned, no references to a previous result row will occur anymore. This allows nested results to be filled much more memory friendly. Default: false.
僅適用于嵌套結(jié)果查詢語句:如果設(shè)置為 true,它會假定包含了嵌套結(jié)果集或分組统求,這樣當(dāng)一個新的主結(jié)果行被返回時检碗,就不會發(fā)生對前面結(jié)果集的引用。這就使得嵌套結(jié)果集很友好地存入更多內(nèi)存(即不會內(nèi)存不夠用)码邻。默認(rèn):false折剃。resultSets
This is only applicable for multiple result sets. It lists the result sets that will be returned by the statement and gives a name to each one. Names are separated by commas.
僅適用于多個結(jié)果集。它列出了語句返回的結(jié)果集并為每個結(jié)果集命名像屋。多個名稱用逗號分隔怕犁。
最后
說明:MyBatis 官網(wǎng)提供了簡體中文的翻譯,但個人覺得較為生硬己莺,甚至有些地方邏輯不通奏甫,于是自己一個個重新敲著翻譯的(都不知道哪里來的自信...),有些地方同官網(wǎng)翻譯有出入凌受,有些倔強(qiáng)地保留了自己的阵子,有的實(shí)在別扭則保留了官網(wǎng)的,這些都會在實(shí)踐中一一更正胜蛉。鑒于個人英文能力有限挠进,文章中保留了官方文檔原英文介紹(個別地方加以調(diào)整修剪)色乾,希望有緣看到這里的朋友們能夠有自己的理解,不會被我可能錯誤或不合理的翻譯帶跑偏(〃'▽'〃)领突,歡迎指正暖璧!
當(dāng)前版本:mybatis-3.5.0
官網(wǎng)文檔:MyBatis
官網(wǎng)翻譯:MyBatis 簡體中文
項(xiàng)目實(shí)踐:MyBatis Learn