Mybatis
mybatis 提供了高级的关联查询功能,可以很方便地将数据库获取的结果集映射到定义的Java Bean 中。下面通过一个实例,来展示一下Mybatis对于常见的一对多和多对一关系复杂映射是怎样处理的。
对一关联
MyBatis中使用association标签来解决一对一的关联查询,association标签可用的属性如下:
- property:对象属性的名称
- javaType:对象属性的类型
- column:所对应的外键字段名称
- select:使用另一个查询封装的结果
<mapper namespace="com.mucfc.model.TicketMapper">
<!-- 定义数据库字段与实体对象的映射关系 -->
<resultMap type="Ticket" id="ticketBean">
<id column="ticketId" property="ticketId" />
<result column="ticketAddress" property="ticketAddress" />
<result column="ticketPrice" property="ticketPrice" />
<result column="ticketCId" property="ticketCId" />
<!-- 一对一的关系 -->
<!-- property: 指的是属性的值, javaType:指的是元素的类型 -->
<association property="customer" javaType="Customer">
<id column="customerId" property="customerId" />
<result column="customerName" property="customerName" />
<result column="customerTel" property="customerTel" />
</association>
</resultMap>
<!-- 根据id查询ticket, 关联将Customer查询出来 -->
<select id="selectTicketById" parameterType="int" resultMap="ticketBean">
select c.*,t.* from t_customer c,t_ticket t where
c.customerId=t.ticketCId and t.ticketId =#{ticketId}
</select>
</mapper>
对多关联
MyBatis中使用collection标签来解决一对一的关联查询,collection标签可用的属性如下:
- property:指的是集合属性的值
- ofType:指的是集合中元素的类型
- column:所对应的外键字段名称
- select:使用另一个查询封装的结果
<mapper namespace="com.mucfc.model.CustomerMapper">
<!-- 定义数据库字段与实体对象的映射关系 -->
<resultMap type="Customer" id="customerBean">
<id column="customerId" property="customerId"/>
<result column="customerName" property="customerName"/>
<result column="customerTel" property="customerTel"/>
<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="tickets" ofType="Ticket">
<id column="ticketId" property="ticketId"/>
<result column="ticketAddress" property="ticketAddress"/>
<result column="ticketPrice" property="ticketPrice"/>
<result column="ticketCId" property="ticketCId"/>
</collection>
</resultMap>
<!-- 根据id查询Person, 关联将Orders查询出来 -->
<select id="selectCustomerByName" parameterType="string" resultMap="customerBean">
select c.*,t.* from t_customer c,t_ticket t where c.customerId=t.ticketCId and c.customerName =#{customerName};
</select>
</mapper>
总配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 这是根标签 -->
<configuration>
<!-- 设置别名 -->
<typeAliases>
<typeAlias alias="Customer" type="com.mucfc.model.Customer"/>
<typeAlias alias="Ticket" type="com.mucfc.model.Ticket" />
</typeAliases>
<!-- 配置数据源相关的信息 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="christmas258@"/>
</dataSource>
</environment>
</environments>
<!-- 列出映射文件 -->
<mappers>
<mapper resource="com/mucfc/model/CustomerMapper.xml" />
<mapper resource="com/mucfc/model/TicketMapper.xml" />
</mappers>
</configuration>
ResultMap标签
* MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。在MyBatis进行查询映射的时候,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性,而当我们提供的返回类型是resultMap的时候,因为Map不能很好表示领域模型,我们就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。 *
resultMap 是MyBatis 中最重要最强大的元素了。你可以让你比使用JDBC 调用结果集省掉90%的代码,也可以让你做许多JDBC 不支持的事。现实上,要写一个等同类似于交互的映射这样的复杂语句,可能要上千行的代码。ResultMaps 的目的,就是这样简单的语句而不需要多余的结果映射,更多复杂的语句,除了只要一些绝对必须的语句描述关系以外,再也不需要其它的。
resultMap属性:type为java实体类;id为此resultMap的标识。