<?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"><mappernamespace="com.dao.PersonDao"><!-- 一对一 根据id查询个人信息:第一种方法(嵌套查询) --><resultMaptype="com.po.Person"id="cardAndPerson1"><idproperty="id"column="id"/><resultproperty="name"column="name"/><resultproperty="age"column="age"/><!-- 一对一关联查询 --><associationproperty="card"column="idcard_id"javaType="com.po.Idcard"select="com.dao.IdCardDao.selectCodeById"/></resultMap><selectid="selectPersonById1"parameterType="Integer"resultMap="cardAndPerson1">
select * from person where id=#{id}
</select><!-- 一对一 根据id查询个人信息:第二种方法(嵌套结果) --><resultMaptype="com.po.Person"id="cardAndPerson2"><idproperty="id"column="id"/><resultproperty="name"column="name"/><resultproperty="age"column="age"/><!-- 一对一关联查询 --><associationproperty="card"javaType="com.po.Idcard"><idproperty="id"column="idcard_id"/><resultproperty="code"column="code"/></association></resultMap><selectid="selectPersonById2"parameterType="Integer"resultMap="cardAndPerson2">
select p.*,ic.code
from person p, idcard ic
where p.idcard_id = ic.id and p.id=#{id}
</select><!-- 一对一 根据id查询个人信息:第三种方法(使用POJO存储结果) --><selectid="selectPersonById3"parameterType="Integer"resultType="com.pojo.SelectPersonById">
select p.*,ic.code
from person p, idcard ic
where p.idcard_id = ic.id and p.id=#{id}
</select></mapper>
一对多
<!-- 一对多 根据uid查询用户及其关联的订单信息:第一种方法(嵌套查询) --><resultMaptype="com.po.MyUser"id="userAndOrder1"><idproperty="uid"column="uid"/><resultproperty="uname"column="uname"/><resultproperty="usex"column="usex"/><!-- 一对多关联查询,ofType表示集合中的元素类型,将uid传递给selectOrderById--><collectionproperty="orderList"ofType="com.po.Order"column="uid"select="com.dao.OrderDao.selectOrderById"/></resultMap><selectid="selectUserOrdersById1"parameterType="Integer"resultMap="userAndOrder1">
select * from user where uid = #{id}
</select><!-- 一对多 根据uid查询用户及其关联的订单信息:第二种方法(嵌套结果) --><resultMaptype="com.po.MyUser"id="userAndOrder2"><idproperty="uid"column="uid"/><resultproperty="uname"column="uname"/><resultproperty="usex"column="usex"/><!-- 一对多关联查询,ofType表示集合中的元素类型 --><collectionproperty="orderList"ofType="com.po.Order"><idproperty="id"column="id"/><resultproperty="ordersn"column="ordersn"/></collection></resultMap><selectid="selectUserOrdersById2"parameterType="Integer"resultMap="userAndOrder2">
select u.*,o.id,o.ordersn from user u, order1 o where u.uid = o.user_id and u.uid=#{id}
</select><!-- 一对多 根据uid查询用户及其关联的订单信息:第三种方法(使用POJO存储结果) --><selectid="selectUserOrdersById3"parameterType="Integer"resultType="com.pojo.SelectUserOrdersById">
select u.*,o.id,o.ordersn from user u, order1 o where u.uid = o.user_id and u.uid=#{id}
</select>```