参考文章:
Mybatis中文网
1.基础知识
- 每个maper接口对应一个XML文件,它们之间联系的桥梁是mapper接口中的方法名,在XML中是标签的id属性
- mybatis的工作流:执行时对参数进行处理,执行完之后还对结果集进行处理,最后可能有缓存结果集的处理
- XML中写CRUD标签来将标签id对应的方法映射为其具体实现
- 标签属性用来配置每条语句的行为细节,标签内容是sql语句,sql语句的入参格式为**#{参数名}**
- 标签必不可少的的属性是id,特别对于select标签还要指明resultType(resultMap)
- resultType必须是类的全限定名(可以在配置中映射一个短的类型别名)
- mybatis为常用的 Java 类型内建了类型别名。
- 参数映射一般可以省略,MyBatis的类型处理器可以自动将参数以合适的方式转换成 Java 类型
- resultMap的作用是将数据库中的字段映射为Java中的对象属性或者HashMap 的键上
- resultMap一般不需要显示配置,除非字段名与属性名不匹配,否则只需要指定resultType,名字相同的自动匹配
- MyBatis 是通过配置中的映射器(mappers)找到映射文件的,再根据标签id找到其要实现的mapper接口中的方法
- 保证mapper接口与它对应的XML在同一目录结构下以及文件名相同,就可以使他们自动匹配起来
- 处理字段名与实体属性名不匹配的两种方法:1.改resultType为resultMap;2.sql的as语法:字段名 as 别名
- 结果映射说白了就是把一个实体类中的属性(构造方法的参数也可以,不过我的实体都是无参构造)和数据库中的字段一一对应起来,不过实体类中的属性可能不仅仅是简单的数据类型,对于对象和集合要进行特殊的匹配映射
- 结果映射相当于在使用XML的方式定义查询结果对象的实体类
- 一对一 association, 一对多 collection,多对多 discrimination
2.配置
类型别名(typeAliases)
类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。
- 直接指定
Author可以用在任何使用 domain.blog.Author 的地方
<typeAliases>
<typeAlias alias="Author" type="domain.blog.Author"/>
</typeAliases>
- 包名指定
指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean
<typeAliases>
<package name="domain.blog"/>
</typeAliases>
实体类前无注解,类名首字母小写的非限定类名来作为它的别名
若有注解,则别名为其注解值:@Alias(“author”)
- 常见的 Java 类型内建的类型别名。它们都是不区分大小写的
别名------------java类型
hashmap------------HashMap
list------------List
映射器(mappers)
<!-- 使用映射器接口实现类的完全限定类名 -->
<mappers>
<mapper class="org.mybatis.builder.AuthorMapper"/>
</mappers>
<!-- 将包内的映射器接口实现全部注册为映射器 -->
<mappers>
<package name="org.mybatis.builder"/>
</mappers>
3.XML 映射器
CRUD
- 代码示例
<select id="selectPerson" parameterType="int" resultType="hashmap">
SELECT * FROM PERSON WHERE ID = #{id}
</select>
<insert id="insertAuthor">
insert into Author (id,username,password,email,bio)
values (#{id},#{username},#{password},#{email},#{bio})
</insert>
<update id="updateAuthor">
update Author set
username = #{username},
password = #{password},
email = #{email},
bio = #{bio}
where id = #{id}
</update>
<delete id="deleteAuthor">
delete from Author where id = #{id}
</delete>
- 标签属性
id 在命名空间中唯一的标识符,可以被用来引用这条语句。
parameterType 将会传入这条语句的参数的类全限定名或别名。这个属性是可选的,因为 MyBatis 可以通过类型处理器(TypeHandler)推断出具体传入语句的参数
resultType 期望从这条语句中返回结果的类全限定名或别名。 注意,如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型。 resultType 和 resultMap 之间只能同时使用一个。
resultMap 对外部 resultMap 的命名引用。结果映射是 MyBatis 最强大的特性,如果你对其理解透彻,许多复杂的映射问题都能迎刃而解。 resultType 和 resultMap 之间只能同时使用一个。
flushCache 将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:false。
useCache 将其设置为 true 后,将会导致本条语句的结果被二级缓存缓存起来,默认值:对 select 元素为 true。
useGeneratedKeys=“true” keyProperty=“id” 仅适用于 insert 和 update,指定id是一个自增组件
结果映射
- 简单地将所有的查询结果字段映射到 HashMap 的键上
<select id="selectUsers" resultType="hashmap">
select id, username, hashedPassword
from some_table
where id = #{id}
</select>
- 对于设置为实体类的结果类型,MyBatis 会在幕后自动创建一个 ResultMap,再根据属性名来映射列到 JavaBean 的属性上
<!-- mybatis-config.xml 中 -->
<typeAlias type="com.someapp.model.User" alias="User"/>
<!-- SQL 映射 XML 中 -->
<select id="selectUsers" resultType="User">
select id, username, hashedPassword
from some_table
where id = #{id}
</select>
- 显式使用外部的 resultMap
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="user_name"/>
<result property="password" column="hashed_password"/>
</resultMap>
<select id="selectUsers" resultMap="userResultMap">
select user_id, user_name, hashed_password
from some_table
where id = #{id}
</select>
resultMap标签
- 标签属性
- 内标签
resultMap的内标签
- id & result
共同点:将一个列的值映射到一个简单数据类型(String, int, double, Date 等)的属性或字段
不同点:id 元素对应的属性会被标记为对象的标识符,在比较对象实例时使用。 这样可以提高整体的性能,尤其是进行缓存和嵌套结果映射(也就是连接映射)的时候。
两个元素都有一些属性:
- 关联(association)
类A的一个属性是类B,将数据库的字段映射到类A的属性时,在映射到属性类B时,该如何映射呢?
答案是使用association,即A关联B,适合一对一连接查询
某个对象的一个对象属性的部分属性映射:
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
</association>
关联的嵌套 Select 查询:
<resultMap id="blogResult" type="Blog">
<association property="author" column="author_id" javaType="Author" select="selectAuthor"/>
</resultMap>
<select id="selectBlog" resultMap="blogResult">
SELECT * FROM BLOG WHERE ID = #{id}
</select>
<select id="selectAuthor" resultType="Author">
SELECT * FROM AUTHOR WHERE ID = #{id}
</select>
分析:根据resultMap我们知道,它是将数据库查询结果字段映射为类型为 Blog的对象属性中,其中一个属性author是Author对象类型,而这个属性是通过一个selectAuthor查询得到的,对于这个查询我们传递数据库查询结果的author_id字段来保证其最多返回一个对象,author_id应该是Blog的一个外键
关联的嵌套结果映射:
<select id="selectBlog" resultMap="blogResult">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio
from Blog B left outer join Author A on B.author_id = A.id
where B.id = #{id}
</select>
对以上查询语句的查询结果进行结果映射如下(可复用):
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author" column="blog_author_id" javaType="Author" resultMap="authorResult"/>
</resultMap>
<resultMap id="authorResult" type="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
</resultMap>
以上resultMap与下面的等价(可读性好)
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
</association>
</resultMap>
- 集合(collection)
类A的一个属性是LIst<类B>,在数据库字段映射类A属性时,对于LIst<类B>,该如何映射呢?
答案是使用collection,即A关联多个B,适合一对多连接查询
集合的嵌套 Select 查询:
<resultMap id="blogResult" type="Blog">
<collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
</resultMap>
<select id="selectBlog" resultMap="blogResult">
SELECT * FROM BLOG WHERE ID = #{id}
</select>
<select id="selectPostsForBlog" resultType="Post">
SELECT * FROM POST WHERE BLOG_ID = #{id}
</select>
可以这样阅读:
Blog对象中有一个ArrayList类型的属性为posts,它是一个Post的集合,向selectPostsForBlog传递一个blogResult的查询结果列id
集合的嵌套结果映射:
<select id="selectBlog" resultMap="blogResult">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
P.id as post_id,
P.subject as post_subject,
P.body as post_body,
from Blog B
left outer join Post P on B.id = P.blog_id
where B.id = #{id}
</select>
对以上查询结果做映射:
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
</collection>
</resultMap>
- 鉴别器(discriminator )