美文网首页
Mybatis 文档篇 3.1:Mapper XML 之 sel

Mybatis 文档篇 3.1:Mapper XML 之 sel

作者: 兆雪儿 | 来源:发表于2019-03-22 17:43 被阅读0次

    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 中,查询语句是最常用的语句之一。向数据库中放入数据并没有多大价值,直到你再将它取出,因此大多数的应用中查询比修改数据多的多。

    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.
    对每个插入、修改或删除操作,通常都对应很多的查询操作。这是 MyBatis 的一个基本原则,也是 MyBatis 在查询和结果映射方面如此关注和努力的原因。

    2.1 select 元素

    The select element is quite simple for simple cases.
    对于简单情况而言,select 元素是相当简单的。

    <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)类型的参数,并且返回一个 HashMap 类型的对象,其中,键是列名,值为结果行中对应的值。

    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:
    注意参数标记:#{id}。它告诉 MyBatis 创建一个 PreparedStatement 参数。使用 JDBC 时,这样的一个参数在 SQL 中会由一个“?”标识,并传递给一个新的 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.
    当然,这需要很多单独的 JDBC 代码来提取结果并将它们映射到一个对象实例中,这就是 MyBatis 为你节省精力和时间的地方。

    2.2 select 的属性

    The select element has more attributes that allow you to configure the details of how each statement should behave.
    select 元素提供很多属性来允许你配置每个语句该如何表现的细节。

    <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.
      该命名空间中可以用来引用该语句的的唯一标识。

    • 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.
      传入这条语句的参数的完全限定类名或别名。这个属性是可选的,因为MyBatis 可以自己推断出传入这条语句的具体参数对应的 TypeHandler。默认是未设置。

    • parameterMap
      This is a deprecated approach to referencing an external parameterMap. Use inline parameter mappings and the parameterType attribute.
      这是一个已经废弃的方法,用来指向一个外部的 parameterMap。使用行内参数映射和 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.
      语句返回的期望类型的完全限定类名或者别名。注意如果返回的是集合,那么这个类型应该是集合包含的类型而不是集合本身。使用 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 的名称引用。结果映射是 MyBatis 最强大的特性之一,在对它们有了很好的理解之后,很多困难的映射问题就能迎刃而解。使用 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.
      设置该属性为 true 会使本地缓存和二级缓存被清空,无论何时该语句被调用。默认: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.
      设置该属性为 true 将会使该条语句的执行结果被缓存到二级缓存中。默认: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).
      该设置是在抛出异常之前,驱动等待数据库返回请求结果的秒数。默认未设置(依赖驱动)。

    • 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).
      这个设置是尝试使驱动批量返回的结果行数。默认未设置(依赖驱动)。

    • statementType
      Any one of STATEMENT, PREPARED or CALLABLE. This causes MyBatis to use Statement, PreparedStatement or CallableStatement respectively. Default: PREPARED.
      值为 STATEMENT, PREPARED 或 CALLABLE 其中的一个,这个设置使 MyBatis 使用 Statement, PreparedStatement or CallableStatement。默认:PREPARED。

    • resultSetType
      Any one of FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE. Default is unset (driver dependent).
      值为 FORWARD_ONLY、SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 其中的一个。默认未设置(赖驱动)。

    • 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 或者匹配当前数据库的 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.
      仅适用于嵌套结果查询语句:如果设置为 true,它会假定包含了嵌套结果集或分组,这样当一个新的主结果行被返回时,就不会发生对前面结果集的引用。这就使得嵌套结果集很友好地存入更多内存(即不会内存不够用)。默认: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.
      仅适用于多个结果集。它列出了语句返回的结果集并为每个结果集命名。多个名称用逗号分隔。

    最后

    说明:MyBatis 官网提供了简体中文的翻译,但个人觉得较为生硬,甚至有些地方逻辑不通,于是自己一个个重新敲着翻译的(都不知道哪里来的自信...),有些地方同官网翻译有出入,有些倔强地保留了自己的,有的实在别扭则保留了官网的,这些都会在实践中一一更正。鉴于个人英文能力有限,文章中保留了官方文档原英文介绍(个别地方加以调整修剪),希望有缘看到这里的朋友们能够有自己的理解,不会被我可能错误或不合理的翻译带跑偏(〃'▽'〃),欢迎指正!

    当前版本:mybatis-3.5.0
    官网文档:MyBatis
    官网翻译:MyBatis 简体中文
    项目实践:MyBatis Learn

    相关文章

      网友评论

          本文标题:Mybatis 文档篇 3.1:Mapper XML 之 sel

          本文链接:https://www.haomeiwen.com/subject/fzervqtx.html