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

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. 力扣算法题—143ReorderList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

  2. px2rem-loader(Vue:移动端自适应,px自动转化)

    1.下载lib-flexible npm i lib-flexible --save 2.引入lib-flexible import 'lib-flexible/flexible' 3.设置meta标 ...

  3. 学习JS基本数据类型与对象的valueOf方法

    https://blog.csdn.net/licheng11403080324/article/details/60128090 https://yq.aliyun.com/articles/399 ...

  4. android 数据异步加载

    public class MainActivity extends Activity { ListView listView; File cache; //访问其他线程在当前线程中存放的数据 Hand ...

  5. 【牛客网-剑指offer】旋转数组的最小数字

    题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...

  6. 遇到的css问题

    1.上下两个div高度重叠:原因是上面的div中存在浮动,且没有设置高度,解决方案:外面再套一个div,设置高度或overflow:hidden 2.上下两个div存在间隙:原因是有div的displ ...

  7. mysql-视图及索引简介

    一.视图的创建.作用及注意事项 1.创建:create view 视图名 as select 语句: 2.删除:drop view 视图名 3.作用: 数据库视图允许简化复杂查询 数据库视图有助于限制 ...

  8. 【Luogu】【关卡2-3】排序(2017年10月) 【AK】

    任务说明:将杂乱无章的数据变得有规律.有各种各样的排序算法,看情况使用. 这里有空还是把各种排序算法总结下吧.qsort需要会写.. P1177 [模板]快速排序 这个题目懒得写了,直接sort了.. ...

  9. hightchart 报错 Error in mounted hook: "Error: Highcharts error #17: www.highcharts.com/errors/17"

    这个错误是应该导入hightchart 使用的相关的 东西 ,这里是worldcloud import  Wordcloud    from  'highcharts/modules/wordclou ...

  10. 决策树(Decision Tree)算法 python简单实现

    "" """ import numpy as np from math import log import operator import json ...