Mybatis 文檔篇 3.1:Mapper XML 之 select

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市攘须,隨后出現(xiàn)的幾起案子漆撞,更是在濱河造成了極大的恐慌,老刑警劉巖于宙,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浮驳,死亡現(xiàn)場離奇詭異,居然都是意外死亡捞魁,警方通過查閱死者的電腦和手機(jī)至会,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谱俭,“玉大人奉件,你說我怎么就攤上這事±ブ” “怎么了县貌?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長凑懂。 經(jīng)常有香客問我煤痕,道長,這世上最難降的妖魔是什么接谨? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任摆碉,我火速辦了婚禮,結(jié)果婚禮上脓豪,老公的妹妹穿的比我還像新娘巷帝。我一直安慰自己,他們只是感情好扫夜,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布楞泼。 她就那樣靜靜地躺著,像睡著了一般笤闯。 火紅的嫁衣襯著肌膚如雪现拒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天望侈,我揣著相機(jī)與錄音,去河邊找鬼勋桶。 笑死脱衙,一個胖子當(dāng)著我的面吹牛侥猬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播捐韩,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼退唠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了荤胁?” 一聲冷哼從身側(cè)響起瞧预,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎仅政,沒想到半個月后垢油,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡圆丹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年滩愁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辫封。...
    茶點(diǎn)故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡硝枉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出倦微,到底是詐尸還是另有隱情妻味,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布欣福,位于F島的核電站责球,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏劣欢。R本人自食惡果不足惜棕诵,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望凿将。 院中可真熱鬧校套,春花似錦、人聲如沸牧抵。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽犀变。三九已至妹孙,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間获枝,已是汗流浹背蠢正。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留省店,地道東北人嚣崭。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓笨触,卻偏偏與公主長得像,于是被迫代替她去往敵國和親雹舀。 傳聞我的和親對象是個殘疾皇子芦劣,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,346評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,573評論 0 23
  • 【喝火令】約茶8 文/二擺 陋巷階前顧,新朋約到時说榆。 解開心結(jié)話都癡虚吟。 欲笑我心清淡,小火自烹滋签财。 歷歷無空性...
    二擺閱讀 278評論 0 1
  • 畢業(yè)串慰,工作,結(jié)婚荠卷,生子模庐,繼續(xù)工作。一路走來油宜,這個陌生的都市見證了我在這里的努力掂碱,彷徨,歡笑和淚水慎冤。從那個長時間漂在...
    張小棠的簡書閱讀 342評論 0 1
  • 1疼燥、atom手動安裝插件 cd ~/.atom/packagesgit clone <你想安裝的 Package ...
    閑魚尼克閱讀 830評論 0 0