简单基本的增删改查语句就不说了,直接从一对一,一对多的关系开始:

association联合:联合元素用来处理“一对一”的关系;

collection聚集:聚集元素用来处理“一对多”的关系;

MyBatis 可以用两种方式加载:

1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活;
2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

实例:

<resultMap id="resultMap" type="***.vo.Article">
  <id column="id" property="id" />
  <result column="user_id" property="userId" />

  //...
  <result column="create_time" property="createTime" />
  <result column="modify_time" property="modifyTime" />
  <result column="delete_time" property="deleteTime" />
  <association property="category" column="id" select="getCategory" />
  <collection property="tags" column="id" javaType="ArrayList"
    ofType="com.zhaozhi.writing.service.vo.Tag" select="getTags" />
</resultMap>

<resultMap id="categoryMap" type="***.vo.Category">
  <id column="id" property="id" />
  <result column="name" property="name" />

  //..
  <result column="create_time" property="createTime" />
  <result column="modify_time" property="modifyTime" />
  <result column="delete_time" property="deleteTime" />
</resultMap>

<resultMap id="tagMap" type="***.vo.Tag">
  <id column="id" property="id" />
  <result column="name" property="name" />
  <result column="create_time" property="createTime" />
  <result column="modify_time" property="modifyTime" />
  <result column="delete_time" property="deleteTime" />
</resultMap>

<select id="getCategory" resultMap="categoryMap" parameterType="java.lang.Integer">
  select c1.* from category as c1,article_categories a2,article a3 where
  c1.id=a2.category_id and c1.user_id = a3.user_id
  and a2.article_id=a3.id and a3.id=#{id} and c1.delete_time is null and
  a2.delete_time is null and a3.delete_time is null
</select>

<select id="getTags" resultMap="tagMap" parameterType="java.lang.Integer">
  select t1.* from tag as t1,article_tags a2,article a3 where t1.id=a2.tag_id
  and a2.article_id=a3.id and a3.id=#{id} and a2.delete_time is null
</select>

最新文章

  1. iOS7时代我们用什么来追踪和识别用户?
  2. 【练习】数据移动---导出(EXPDP)
  3. 收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation
  4. jQuery - 9.Ajax
  5. Ext Js【Hello World】 ——4.1 beta 1
  6. 2748: [HAOI2012]音量调节
  7. Mysql --分区(4)List分区
  8. Androidi性能优化之Java代码优化(摘自Android性能优化一书)
  9. Linux查看文件以及文件夹的大小
  10. 给VMware的虚拟机设置静态地址
  11. js通过class获取元素时的兼容性解决方案
  12. 用命令行撤销工作区的所有更改(修改文件&amp;&amp;新增文件)
  13. crontab 误删恢复
  14. 【PHP篇】运算及流程控制
  15. C# WinForm开发系列 - 文章索引
  16. [react002] component基本用法
  17. seek()和tell()在文件里转移
  18. 【Django】pip 安装和卸载 Django
  19. c# var的含义与用法
  20. GIT团队合作探讨之二--Pull Request

热门文章

  1. Cococs2d-x学习路线
  2. 程序性能优化之APK大小优化(六)下篇
  3. 2018-2-13-win10-安装Mpi
  4. 【学术篇】NOI2015 品酒大会 后缀数组+并查集
  5. codeforces 1182E Product Oriented Recurrence 矩阵快速幂
  6. Codeforces 343D 线段树
  7. 【LeetCode】Math
  8. 39th 迷迷糊糊 二豆玩不转了
  9. 在Eclipse上安装Spring Tool Suite
  10. leetcode-按奇偶排序数组II