深入理解Mybatis中的resultType和resultMap的区别。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wang.mapper.PersonMapper">
<select id="findPersonById" parameterType="Integer"
resultMap="IdCardWithPersonResult">
<!--编写多表关联查询SQL语句,将获取数据库中的数据映射到Person实体类中-->
select*from t_person p,t_idcard idcard
where p.id=idcard.id and p.id=#{id}
</select>
<resultMap type="com.wang.po.Person"
id="IdCardWithPersonResult">
<!--Person属性和t_person表字段一一对应-->
<id property="id" column="id" />
<result property="name" column="name" />
<!--association元素用于实现一对一关联映射-->
<association property="card" javaType="com.wang.po.IdCard">
<!--IdCard属性和t_idcard表字段一一对应-->
<id property="id" column="card_id" />
<result property="code" column="code" />
</association>
</resultMap>
</mapper>
元素中的属性使用
属性 说明
property POJO实体类的属性,与表字段一一对应
column 数据表字段名,与POJO实体类的属性一一对应
javaType 指实体对象属性的数据类型,比如card属于IdCard类型
select 引入嵌套查询的子SQL语句
fetchType 指定关联查询是否启用延迟加载。fetchType属性有lazy和eager两个值,默认为lazy表示延迟加载
03.在UserMapper.xml编写SQL语句(开发大部分使用:嵌套结果查询)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wang.mapper.UserMapper">
<!-- 一对多:查看某一用户及其关联的订单信息 注意:当关联查询出的列名相同,则需要使用别名区分 -->
<!-- user表中id字段名和order中的id字段同名,必须为一个字段取别名-->
<select id="findUserById" parameterType="Integer"
resultMap="UserWithOrdersResult">
select u.*,
o.id as order_id,
o.number
from t_user u,t_order o
where u.id=o.user_id and u.id=#{id}
</select>
<resultMap type="com.wang.po.User" id="UserWithOrdersResult">
<!--数据表字段与User属性对应 -->
<id property="id" column="id" />
<result property="name" column="name" />
<result property="address" column="address" />
<result property="pnumber" column="pnumber" />
<!-- 一对多关联映射:collection ofType表示属性集合中元素的类型,
List<Order>属性即Order类 -->
<collection property="ordersList"
ofType="com.wang.po.Order">
<!-- 使用数据表字段别名和Order属性对应 -->
<id property="id" column="order_id" />
<result property="number" column="number" />
</collection>
</resultMap>
</mapper>
03.在OrderMapper.xml编写SQL语句,把数据库中的数据封装到Order.java类
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wang.mapper.OrderMapper">
<select id="findOrderandProductById" parameterType="integer"
resultMap="OrderWithProduct">
SELECT
p.*, o.id AS order_id,
o.number
FROM
t_product p,
t_order o
WHERE
o.id =#{id}
</select>
<!-- 映射到的类型是一个普通Java类 -->
<resultMap type="com.wang.po.Order" id="OrderWithProduct">
<id property="id" column="order_id" />
<result property="number" column="number" />
<collection property="productlist"
ofType="com.wang.po.Product">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="price" column="price" />
</collection>
</resultMap>
</mapper>
04.mybatis-config.xml中引入OrderMapper.xml
<mappers>
<mapper resource="com/wang/mapper/OrderMapper.xml"/>
</mappers>