一对一查询

第一种方法:

<!-- 查询所有订单信息 -->
<select id="findOrdersList" resultType="cn.itcast.mybatis.po.OrdersCustom">
SELECT
orders.*,
user.username,
user.address
FROM
orders, user
WHERE orders.user_id = user.id
</select>

第二种方法:
<!-- 查询订单关联用户信息使用resultmap -->
<resultMap type="cn.itheima.po.Orders" id="orderUserResultMap">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<!-- 一对一关联映射 -->
<!--
property:Orders对象的user属性
javaType:user属性对应 的类型
-->
<association property="user" javaType="cn.itcast.po.User">
<!-- column:user表的主键对应的列 property:user对象中id属性-->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="address" property="address"/>
</association>
</resultMap>
<select id="findOrdersWithUserResultMap" resultMap="orderUserResultMap">
SELECT
o.id,
o.user_id,
o.number,
o.createtime,
o.note,
u.username,
u.address
FROM
orders o
JOIN `user` u ON u.id = o.user_id
</select>

一对多查询

<resultMap type="cn.itheima.po.user" id="userOrderResultMap">
<!-- 用户信息映射 -->
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<result property="address" column="address"/>
<!-- 一对多关联映射 -->
<collection property="orders" ofType="cn.itheima.po.Orders">
<id property="id" column="oid"/>
<!--用户id已经在user对象中存在,此处可以不设置-->
<!-- <result property="userId" column="id"/> -->
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</collection>
</resultMap>
<select id="getUserOrderList" resultMap="userOrderResultMap">
SELECT
u.*, o.id oid,
o.number,
o.createtime,
o.note
FROM
`user` u
LEFT JOIN orders o ON u.id = o.user_id
</select>
 

最新文章

  1. Twitter面试题蓄水池蓄水量算法(原创 JS版,以后可能会补上C#的)
  2. 如何使用JS来检测游览器是什么类型,或android是什么版本号- 转载
  3. svn提交报e200007错误
  4. 改变Android ProgressBar样式颜色
  5. 使用dig查询dns解析
  6. iOS开发代码规范
  7. Codewars编辑题--今天升到了7段
  8. 新型信用卡MasterPass
  9. ZOJ(ZJU) 1002 Fire Net(深搜)
  10. Linux下MySQL 5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)
  11. Yii的URL助手
  12. 初探linux子系统集之timer子系统(二)
  13. 聊聊 Spring Boot 2.x 那些事儿
  14. ios 开发视图界面动态渲染
  15. spring boot 整合js css 静态文件
  16. 部署KVM
  17. Python pickle模块
  18. 洛谷P4147 玉蟾宫 单调栈/悬线法
  19. Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))
  20. CodeIgniter 安装指导

热门文章

  1. 求n的因子个数与其因子数之和
  2. Hibernate通过实体对象对应数据库表信息
  3. js删除最后一个字符
  4. sql子查询的例子
  5. 学习ASP.NET MVC5的一个轻量级的NinJect框架学习的第二天
  6. chrome inspect出现白屏的解决方案
  7. 关于Android发送短信获取送达报告的问题
  8. CentOS 6.5下安装Python 3.5.2(与Python2并存)
  9. (转)关于treap的板子理解
  10. easyUI-datagrid属性设置display:none,表头不显示