select
-
select标签属性属性 描述 id在命名空间中唯一的标识符,可以被用来引用这条语句。 parameterType将会传入这条语句的参数类的完全限定名或别名。这个属性是可选的,因为 MyBatis 可以通过类型处理器(TypeHandler) 推断出具体传入语句的参数,默认值为未设置(unset)。 parameterMap这是引用外部 parameterMap 的已经被废弃的方法。请使用内联参数映射和 parameterType 属性。resultType从这条语句中返回的期望类型的类的完全限定名或别名。 注意如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。可以使用 resultType 或 resultMap,但不能同时使用。 resultMap外部 resultMap 的命名引用。结果集的映射是 MyBatis 最强大的特性,如果你对其理解透彻,许多复杂映射的情形都能迎刃而解。可以使用 resultMap 或 resultType,但不能同时使用。 flushCache将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false。 useCache将其设置为 true 后,将会导致本条语句的结果被二级缓存缓存起来,默认值:对 select 元素为 true。 timeout这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为未设置(unset)(依赖驱动)。 fetchSize这是一个给驱动的提示,尝试让驱动程序每次批量返回的结果行数和这个设置值相等。 默认值为未设置(unset)(依赖驱动)。 statementTypeSTATEMENT,PREPARED 或 CALLABLE 中的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。 resultSetTypeFORWARD_ONLY,SCROLL_SENSITIVE, SCROLL_INSENSITIVE 或 DEFAULT(等价于 unset) 中的一个,默认值为 unset (依赖驱动)。 databaseId如果配置了数据库厂商标识(databaseIdProvider),MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。 resultOrdered这个设置仅针对嵌套结果 select 语句适用:如果为 true,就是假设包含了嵌套结果集或是分组,这样的话当返回一个主结果行的时候,就不会发生有对前面结果集的引用的情况。 这就使得在获取嵌套的结果集的时候不至于导致内存不够用。默认值: false。resultSets这个设置仅对多结果集的情况适用。它将列出语句执行后返回的结果集并给每个结果集一个名称,名称是逗号分隔的。 -
id
mapper配置文件中的唯一标识符。 Mapper配置文件中的id和Mapper接口中的方法名必须相同
getUserById(Long id) <select id="getUserById"> -
parameterType
输入参数类型。
-
基本数据类型:int、string、long、Date;
-
复杂数据类型:类(JavaBean)、包装类(Integer等)、List、Map等
-
基本数据类型:#{value}或KaTeX parse error: Expected 'EOF', got '#' at position 18: …alue} 获取参数中的值 。#̲{}预编译方式;{}为拼接sql,可能存在sql注入
-
复杂数据类型:#{属性名}或KaTeX parse error: Expected 'EOF', got '#' at position 14: {属性名} ,map中则是#̲{key}或{key}
示例:
-
传入Long。
<select id="getUserById" resultType="com.zm.entity.User" parameterType="Long"> SELECT id,name,password,age FROM tb_users where id = #{id} </select> -
传入list。
<select id="getUserByListId" resultType="com.zm.entity.User" parameterType="java.util.ArrayList"> select id,name,password,age FROM tb_users <where> id in( <foreach collection="list" item="id" index="index" separator=","> #{id} </foreach> ) </where> </select> -
传入数组。
<select id="getUserByArrayId" resultType="com.zm.entity.User" parameterType="arraylist"> select id,name,password,age FROM tb_users <where> id in( <foreach collection="array" item="arr" index="index" separator=","> #{arr} </foreach> ) </where> </select> -
传入map。
<select id="getUserByMap" resultType="com.zm.entity.User" parameterType="hashmap"> select id,name,password,age FROM tb_users where id=#{id} and name = #{name} </select> -
传入JavaBean
<select id="getUserByUser" resultType="com.zm.entity.User" parameterType="com.zm.entity.User"> select id,name,password,age FROM tb_users where id=#{id} and name = #{name} </select>
-
-
-
resultType返回值类型
- 基本数据类型、String
- List、Array
- Map
-
基本数据类型、String
<select id="getUserReturnName" resultType="String" parameterType="Long"> select name FROM tb_users where id=#{id} </select> -
List、Array
<select id="getUserReturnList" resultType="com.zm.entity.User" parameterType="int"> select id,name,password,age FROM tb_users where age > #{age} </select> -
Map
<!-- 返回单条记录的map,key为属性,值为属性值 --> <select id="getUserReturnMap1" resultType="map" parameterType="Long"> select id,name,password,age FROM tb_users where id = #{id} </select><!--返回多条Map 接口中要指定mapkey--> <select id="getUserReturnMap2" resultType="com.zm.entity.User" parameterType="int"> select id,name,password,age FROM tb_users where age > #{age} </select> -
Mapper接口代码
String getUserReturnName(Long id); List<User> getUserReturnList(int age); User[] getUserReturnArray(int age); Map<String,User> getUserReturnMap1(Long id); /** * 需要指定 map中的key 否则会报错 * org.apache.ibatis.exceptions.TooManyResultsException * @param id * @return */ @MapKey("name") Map<String,User> getUserReturnMap2(int id);
-
resultMap
外部 resultMap 的命名引用。结果集的映射是 MyBatis 最强大的特性,通过使用resultMap可以解决复杂的映射关系。
<resultMap id="resultUser" type="com.zm.entity.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<result property="age" column="age"/>
</resultMap>
<!--使用resultMap-->
<select id="getUserByResultMap" resultMap="resultUser">
select id,name,password,age from tb_users
</select>
-
标签
id: resultMap的唯一标识,用于其他select标签引用。
type:指定返回值的类型
-
标签 主键属性配置
property:返回类型javaBean中的主键对象
column:数据库中的主键
jdbcType:数据库中字段对应的jdbc type类型
javaType: java类属性的类型
typeHandler:类型处理器对象
jdbcType和javaType可以不用配置,mybatis会自动补充类型。
typeHandler也不用配置,mybatis提供了大量的类型转换,会自动进行选择合适的类型转换
-
标签 非主键属性配置(和id标签唯一区别是否是主键)
property:返回类型javaBean中的主键对象
column:数据库中的主键
jdbcType:数据库中字段对应的jdbc type类型
javaType: java类属性的类型
typeHandler:类型处理器对象
-
其他标签(后续章节重点讲述)
constructor:在类初始化时,用来注入结果到构造方法中association:一对一映射关系
collection :一对多映射关系
discriminator :使用结果值来决定使用哪个结果映射
本文深入解析MyBatis框架中select标签的属性与用法,包括参数类型、返回类型、结果映射等核心概念,以及如何处理复杂的数据类型和结果集。
2606

被折叠的 条评论
为什么被折叠?



