MyBatis框架中,主要核心部分之一就是Mapper映射文件。因为MyBatis的DAO组件只是通过接口,而不需要实现类,因此,Mapper映射文件显得十分重要,因为接口中的对数据库的原子方法的实现都在Mapper映射文件中进行使用原生的SQL语句实现,所以对于Mapper.xml的编写也十分重要。本文主要讲解一下Mapper文件编写常见的语法以及常见的情况,从而对其进行的一个整理总结。
1.<mapper.../>元素
<mapper namespace="com.mybatis.mapper.UserMapper">
以上的配置,就是指明该Mapper映射文件对应的接口,即说明哪些接口可以使用当前配置文件。上面则指明该配置文件为UserMapper接口。这里可以具体到某个接口,也可以具体到某个包。
2.parameterType
<insert id="insertUser" parameterType="com.mybatis.model.impl.User" >
insert into tb_user(id,name,sex,age)
values(#{id},#{name},#{sex},#{age})
</insert>
上面这段配置则是表示插入,因为插入没有输出但是需要输入,所以该段不需要配置resultType属性,但是需要配置parameterType属性,该属性的值表示该操作输入参数的类型,即该输入参数映射的po类。
<select id="selectOneUser" parameterType="java.lang.String"
resultType="com.mybatis.model.impl.User" useCache="false">
select * from tb_user where username like "%${value}%"
</select>
3.resultType
<select id="selectAllUser" resultType="com.mybatis.model.impl.User">
select * from tb_user
</select>
以上是数据库语言实现的操作,其中<select.../>表示查找,同理<insert.../>表示插入、<delete..../>表示删除、<update.../>表示更新等等。
4.resultMap
<select id="getBookInfo" resultMap="bookOrder">
select tb_book.*,tb_user.name,tb_user.sex from tb_user,tb_book where tb_user.id=tb_book.user_id
</select>
<resultMap type="com.mybatis.model.impl.Book1" id="bookOrder">
<id property="user_id" column="user_id"/>
<result property="book_id" column="book_id"/>
<result property="book_name" column="book_name"/>
<association property="user" javaType="com.mybatis.model.impl.User">
<id property="book_id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
</association>
</resultMap>
resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。
如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。一般涉及到了关联查询(输出对象类中有对象属性或者对象集合属性)一般都需要以resultMap形式输出。如果需要使用resultMap,则应该如上所示,首先在<select...>元素中添加resultMap属性,该值就是resultMap的名字,对应着<resultMap id="">的id。
3.如果是关联属性,即输出类中包含的对象属性,则需要使用<association....></association>元素来实现,其中<assciation property="" javaType="">中property则表示该关联属性在输出类中的属性名,javaType的值表示该对象属性所对应的类名,需要将具体包表示出来。至于<association....></association>之间的内容则也是有由<id/>和<result/>组成,如果关联属性中仍然有关联属性,则需要在<association/>中使用<association/>嵌套,依次类推。
4.如果是对象集合属性,即输出类中有类似List<Book> book属性,则book就是对象集合属性,则需要使用集合元素<collection/>,其中<collection property="" ofType>中property表示该集合属性的属性名,而ofType则表示集合中元素所属类型。比如List<Book> book则对应property为book,而ofType表示"xx.xx.x.Book"。
对于<resultMap/>中的<collection../>和<association../>元素不仅可以自身嵌套还能相互嵌套,以下就是一个稍微复杂一些的配置:
<select id="getInfo" resultMap="user1">
select user.id user_id,user.name,book.id book_id,book.name book_name,booktype.type typeid,type.name type_name
from book,booktype,user,type
where user.id=book.user_id
and book.id=booktype.book_id
and booktype.type=type.id;
</select>
<resultMap type="com.mybatis.model.impl.User1" id="user1">
<id property="id" column="user_id"/>
<result property="name" column="user_name"/>
<collection property="book" ofType="com.mybatis.model.impl.Book1">
<id property="id" column="book_id"/>
<result property="name" column="book_name"/>
<collection property="bookType" ofType="com.mybatis.model.impl.BookType">
<id property="book_id" column="book_id"/>
<result property="type" column="typeid"/>
<association property="types" javaType="com.mybatis.model.impl.Type">
<id property="id" column="typeid"/>
<result property="name" column="type_name"/>
</association>
</collection>
</collection>
</resultMap>
上面这段配置中<resultMap/>中有<collection/>元素,并在<collection/>中嵌套了一个<collection/>元素,并在嵌套进来的<collection/>元素中嵌套一个<association>元素。上面这段配置涉及四个类,假设为A、B、C、D四个类,其中A类中有B类对象集合属性List<B> b,B类对象有一个C类对象集合属性List<C> c,C类中有个D类对象属性D d。其形成的Mapper文件配置关系就如同上述配置所示。