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

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. python学习笔记:接口开发——flask Demo实例

    举例1,返回当前时间接口 ''' 初始化:所有的Flask都必须创建程序实例, web服务器使用wsgi协议,把客户端所有的请求都转发给这个程序实例 程序实例是Flask的对象,一般情况下用如下方法实 ...

  2. python学习笔记:接口开发——PythonWEB框架之Flask

    Flask是一个使用 Python 编写的轻量级 Web 应用框架,安装命令如下 pip install flask 一.服务端接口是怎么开发的? 1.启动一个服务 2.接收到客户端传过来的数据3.登 ...

  3. PAT甲级——A1149DangerousGoodsPackaging【25】

    When shipping goods with containers, we have to be careful not to pack some incompatible goods into ...

  4. jwt token校验获取用户数据

    /** * 用户登录成功之后,获取用户信息 * 1.获取用户id * 2.根据id查询用户 * 3.构建返回值对象 * 4.返回数据 * @return */@RequestMapping(value ...

  5. emacs-w3m查看html帮助手册

    emacs-w3m查看html帮助手册 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #83949 ...

  6. Window/Linux + Vim + MingW 配置

    Windows: 首先安装Vim , MinGW,假设安装目录为D:\MinGW. 配置环境变量: LIBRARY_PATH=D:\MinGW\lib C_INCLUDE_PATH=D:\MinGW\ ...

  7. Stm32 控制1.44寸液晶显示图片 基于stm32f051k8u6

    在线图片转BMP格式:https://app.xunjiepdf.com/img2bmp 一.使用工具对图片生成字符数组 1. 使用1.44寸的液晶其像素是128*128的16位真彩,则需要把图片调整 ...

  8. JS window对象 返回浏览历史中的其他页面 go()方法,根据当前所处的页面,加载 history 列表中的某个具体的页面。 语法: window.history.go(number);

    返回浏览历史中的其他页面 go()方法,根据当前所处的页面,加载 history 列表中的某个具体的页面. 语法: window.history.go(number); 参数: 浏览器中,返回当前页面 ...

  9. 【LeetCode】BFS || DFS [2017.04.10--2017.04.17]

    [102] Binary Tree Level Order Traversal [Medium-Easy] [107] Binary Tree Level Order Traversal II [Me ...

  10. vue css动画原理

    从隐藏到显现 从显现到隐藏 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...