mybatis文档摘要(提高篇)
xml映射文件
sql
这个元素可以被用来定义可重用的 SQL 代码段,这些 SQL 代码可以被包含在其他语句中。它可以(在加载的时候)被静态地设置参数。 在不同的包含语句中可以设置不同的值到参数占位符上。比如:
<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>
这个 SQL 片段可以被包含在其他语句中,例如:
<select id="selectUsers" resultType="map">
select
<include refid="userColumns"><property name="alias" value="t1"/></include>,
<include refid="userColumns"><property name="alias" value="t2"/></include>
from some_table t1
cross join some_table t2
</select>
sql也可以嵌套使用
参数映射的功能
1: 像 MyBatis 的其他部分一样,参数也可以指定一个特殊的数据类型。
#{property,javaType=int,jdbcType=NUMERIC}
要更进一步地自定义类型处理方式,你也可以指定一个特殊的类型处理器类(或别名),比如:
#{age,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler}
对于数值类型,还有一个小数保留位数的设置,来指定小数点后保留的位数。
#{height,javaType=double,jdbcType=NUMERIC,numericScale=2}
字符串替换
默认情况下,使用 #{} 格式的语法会导致 MyBatis 创建 PreparedStatement 参数占位符并安全地设置参数(就像使用 ? 一样)。 这样做更安全,更迅速,通常也是首选做法,不过有时你就是想直接在 SQL 语句中插入一个不转义的字符串。 比如,像 ORDER BY,你可以这样来使用:
ORDER BY ${columnName}
(#{} 与 ${} 的区别)
结果映射
resultMap 元素是 MyBatis 中最重要最强大的元素。对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了。
resultType=“map”
<select id="selectUsers" resultType="map">
select id, username, hashedPassword
from some_table
where id = #{id}
</select>
resultType为map是,列名对应键名
resultType=“com.someapp.model.User”
<select id="selectUsers" resultType="com.someapp.model.User">
select id, username, hashedPassword
from some_table
where id = #{id}
</select>
resultType为pojo类时,列名对应字段名(可以进行别名配置)
<typeAlias type="com.someapp.model.User" alias="User"/>
这个情况下,MyBatis 会在幕后自动创建一个 ResultMap,再基于属性名来映射列到 JavaBean 的属性上。如果列名和属性名没有精确匹配,可以在 SELECT 语句中对列使用别名(as)来匹配标签
高级结果映射
1:association
关联(association)元素处理“有一个”类型的关系。 比如,在我们的示例中,一个博客有一个用户。关联结果映射和其它类型的映射工作方式差不多。 你需要指定目标属性名以及属性的javaType。
用法:
引用 resultMap,直接嵌套或者间接引用(只有一条sql语句,需求复杂时用多表关联查询)(内联映射)(多表查询)
<resultMap type="Student" id="studentResultMap">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="gender" property="gender" />
<result column="major" property="major" />
<result column="grade" property="grade"/>
<!-- 直接嵌套ResultMap ***************************-->
<association properties="teacher" javaType="Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
<result property="gender" column="t_gender"/>
<result property="researchArea" column="research_area"/>
</association>
</resultMap>
<resultMap type="Student" id="studentResultMap">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="gender" property="gender" />
<result column="major" property="major" />
<result column="grade" property="grade"/>
<!-- 间接引用teacherResultMap *********************-->
<association property="supervisor" resultMap="teacherResultMap"/>
</resultMap>
引用额外的sql语句(有两条sql语句)(N+1)(额外sql)
<resultMap type="Student" id="studentResultMap">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="gender" property="gender" />
<result column="major" property="major" />
<result column="grade" property="grade"/>
<!-- 引用select语句*******************************-->
<association property="supervisor" column="supervisor_id" select="com.xxxMapper.selectSupervisor"/>
</resultMap>
注意:在开发中,多对一的关联关系,一般在列表中显示,通常使用多表查询,即内联映射。如果在当前页面不显示数据,需要跳转到另一个页面再显示的数据,适合使用额外sql。
2:coolection
集合元素和关联元素几乎是一样的
用法:
使用方法与association相同
区别:
“ofType” 属性,它用来将 JavaBean(或字段)属性的类型和集合存储的类型区分开来。 所以你可以按照下面这样来阅读映射:
<collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
读作: “posts 是一个存储 Post 的 ArrayList 集合”
在一般情况下,MyBatis 可以推断 javaType 属性,因此并不需要填写。所以很多时候你可以简略成:
<collection property="posts" column="id" ofType="Post" select="selectPostsForBlog"/>
3:association与coolection的区别
对象关联关系为一对一时,使用association
对象关联关系为一对多时,使用coolection
待补充······
动态SQL
if
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
choose, when, otherwise
有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。
还是上面的例子,但是这次变为提供了“title”就按“title”查找,提供了“author”就按“author”查找的情形,若两者都没有提供,就返回所有符合条件的 BLOG(实际情况可能是由管理员按一定策略选出 BLOG 列表,而不是返回大量无意义的随机结果)。
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
where trim set
where
自动添加where标签,并且在每个and之间加逗号。
set
自动添加set标签,但不会自动加逗号,所以需要自己添加。
trim
//where
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
prefixOverrides 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)。它的作用是移除所有指定在 prefixOverrides 属性中的内容,并且插入 prefix 属性中指定的内容。
//set
<trim prefix="SET" suffixOverrides=",">
...
</trim>
foreach
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
item: 表示集合中每一个元素进行迭代时的别名
index: 指定一个名字,用于表示在迭代过程中,每次迭代到的位置
collection:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可。此时coolection为map的第一个key