MyBatis —— 映射配置之select


查询语句是使用 MyBatis 时最常用的元素之一。
select 元素有很多属性允许你配置,来决定每条语句的作用细节。

 

简单的查询

<select id="selectPerson" parameterType="int" resultType="hashmap">

SELECT * FROM PERSON WHERE ID = #{id}

</select>

这个语句被称作 selectPerson, 使用一个 int ( Integer) 类型的参数, 并返回一个 HashMap 类型的对象,其中的键是列名,值是列对应的值。

就像这样:

String selectPerson = "SELECT * FROM PERSON WHERE ID=?";

PreparedStatement ps = conn.prepareStatement(selectPerson);

ps.setInt(1, id);

 

可配置的属性

<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

在命名空间中唯一的标识符,可以被用来引用这条语句。

parameterType

将会传入这条语句的参数类的完全限定名或别名(参见:内置的别名)。

parameterMap

这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数 映射和 parameterType 属性。

resultType

从这条语句中返回的期望类型的类的完全限定名或别名。注意集合情形,那应该是集合可以包含的类型,而不能是集合本身。使 resultType resultMap,但不能同时使用。

resultMap

命名引用外部的 resultMap 返回 map MyBatis 最具力量的特性, 对其有一个很好的理解的话, 许多复杂映射的情形就能被解决了。 使用 resultMap resultType,但不能同时使用。

flushCache

此设置为true,每当执行这个语句,将导致本地和二级缓存被刷新。默认值:false

useCache

将其设置为 true, 将会导致本条语句的结果被缓存。 默认值: true

timeout

这个设置驱动程序等待数据库返回请求结果,并抛出异常时间的最大等待值。默认不设置(驱动自行处理)

fetchSize

这是暗示驱动程序每次批量返回的结果行数。默认不设置(驱动自行处理)

statementType

STA, TEMENT, PREPARED CALLABLE 的一种。 这会让 MyBatis 使用选择使用 Statement,PreparedStatement CallableStatement 默认值: PREPARED

resultSetType

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值