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

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. CTF中的AWD套路

    今年参加了三起CTF比赛,属于初学者,基本除了web其他的不会,但分赛场AWD相对就没什么难度,基本都是技巧性.其中一场进入复赛了,本月底再次比赛(ctf题做的这么渣还能进也是绝了~),参照前人经验补 ...

  2. 2018年分享的Spring Cloud 2.x系列文章

    还有几个小时2018年就要过去了,盘点一下小编从做做公众号以来发送了273篇文章,其中包含原创文章90篇,虽然原创的有点少,但是2019年小编将一如既往给大家分享跟多的干货,分享工作中的经验,让大家在 ...

  3. 我的黑苹果之路 9400f + msi b360 mortar + 1050ti

    硬件配置: CPU:i5 9400f 主板:微型msi迫击炮B360 内存:威刚 ddr4 8g 2133 *2 显卡:技嘉1050ti 4g 显示器:koiso 4K (使用的dp接口连接,hdmi ...

  4. 总结下awk基本用法

    命令格式: awk '{commands} [{other commands}]' awk 'condition{commands} [{other commands}]' 如:awk '$4==&q ...

  5. Codeforces 343E 最小割树

    题意及思路:https://www.cnblogs.com/Yuzao/p/8494024.html 最小割树的实现参考了这篇博客:https://www.cnblogs.com/coder-Uran ...

  6. MySql为某个表增加rownumber

    在mySql中,假设表名为tblA,则 select @x:=@x+1 as rownumber, a.* from (select @x:=0) b, tblA a 此语句执行后,则每行数据之前都会 ...

  7. mysql-一行分隔成多行数据

    mysql将某个字段有分隔符号分隔成多行数据 SELECT a.id, a. NAME, substring_index( substring_index( a.name, ',', b.help_t ...

  8. vue中使用v-if判断数组长度是出现length报错

    在vue中使用v-if判断数组的长度时出现报错 <el-collapse-item :key="index" v-if="data.childrens.length ...

  9. 数学思维——cf1244C

    可惜cf不能用int128,不然这个题就是个exgcd的板子题 这是exgcd的解法,但是只用ll的话会溢出 #include<bits/stdc++.h> using namespace ...

  10. NX二次开发-Block UI C++界面Toggle(开关)控件的获取(持续补充)

    NX9+VS2012 public: void SetBlockUIShow(); void ToggleInt::SetBlockUIShow() { //获取开关控件 //获取枚举控件 Prope ...