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

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>

mybatis的实际应用的更多相关文章

  1. 【分享】标准springMVC+mybatis项目maven搭建最精简教程

    文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...

  2. Java MyBatis 插入数据库返回主键

    最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...

  3. [原创]mybatis中整合ehcache缓存框架的使用

    mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...

  4. 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程

    本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...

  5. mybatis plugins实现项目【全局】读写分离

    在之前的文章中讲述过数据库主从同步和通过注解来为部分方法切换数据源实现读写分离 注解实现读写分离: http://www.cnblogs.com/xiaochangwei/p/4961807.html ...

  6. MyBatis基础入门--知识点总结

    对原生态jdbc程序的问题总结 下面是一个传统的jdbc连接oracle数据库的标准代码: public static void main(String[] args) throws Exceptio ...

  7. Mybatis XML配置

    Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  8. MyBatis源码分析(一)开篇

    源码学习的好处不用多说,Mybatis源码量少.逻辑简单,将写个系列文章来学习. SqlSession Mybatis的使用入口位于org.apache.ibatis.session包中的SqlSes ...

  9. (整理)MyBatis入门教程(一)

    本文转载: http://www.cnblogs.com/hellokitty1/p/5216025.html#3591383 本人文笔不行,根据上面博客内容引导,自己整理了一些东西 首先给大家推荐几 ...

  10. MyBatis6:MyBatis集成Spring事物管理(下篇)

    前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...

随机推荐

  1. upc组队赛5 Hunter’s Apprentice 【判断多边形边界曲线顺逆时针】

    Hunter's Apprentice 题目描述 When you were five years old, you watched in horror as a spiked devil murde ...

  2. camunda流程实例启动的一些简单操作

    public class ZccRuntimeService { RuntimeService runtimeService; RepositoryService repositoryService; ...

  3. 爬虫(三)—— BeautifulSoup模块获取元素

    目录 BeautifulSoup 一.BeautifulSoup简介 二.安装模块 三.解析器 四.Beautiful Soup的使用 五.查找元素 1.遍历文档树 2.搜索文档树 Beautiful ...

  4. 使用element-ui 组件动态合并table的行/列(第二次修改)

    这是第二次修改,在通过调用后台接口返回来的时候,发现了代码中的问题:现在将博客中错误的地方改过来,添加备注 文章需求:动态实现table表格中行/列的自动合并 使用框架及UI类库:Vue+Elemen ...

  5. windows下虚拟环境的安装与创建

    使用虚拟环境的好处: 可以方便的解决不同项目对类库的依赖问题. 可以在系统中Python解释器中避免包的混乱和版本的冲突. 为每个程序单独创建虚拟环境,可以保证程序只能访问虚拟环境中的包,保持全局解释 ...

  6. Recall,Precision,ROC曲线的介绍

    https://www.jianshu.com/p/f154237924c4 (ROC讲解) https://blog.csdn.net/saltriver/article/details/74012 ...

  7. 爬虫(十二):图形验证码的识别、滑动验证码的识别(B站滑动验证码)

    1. 验证码识别 随着爬虫的发展,越来越多的网站开始采用各种各样的措施来反爬虫,其中一个措施便是使用验证码.随着技术的发展,验证码也越来越花里胡哨的了.最开始就是几个数字随机组成的图像验证码,后来加入 ...

  8. classmethod和staticmethod

    假设有这么一个 class class Date(object): def __init__(self, day=0, month=0, year=0): self.day = day self.mo ...

  9. Java Collection - PriorityQueue 优先队列

    总结 优先队列的作用是能保证每次取出的元素都是队列中权值最小的(Java的优先队列每次取最小元素,C++的优先队列每次取最大元素).这里牵涉到了大小关系,元素大小的评判可以通过元素本身的自然顺序(na ...

  10. Delphi 滚动条的使用

    DELPHI的滚动条默认发送消息格式: function TControl.Perform( Msg: Cardinal; WParam: WPARAM; LParam: LPARAM): LRESU ...