mybatis的实际应用
简单基本的增删改查语句就不说了,直接从一对一,一对多的关系开始:
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的实际应用的更多相关文章
- 【分享】标准springMVC+mybatis项目maven搭建最精简教程
文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...
- Java MyBatis 插入数据库返回主键
最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程
本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...
- mybatis plugins实现项目【全局】读写分离
在之前的文章中讲述过数据库主从同步和通过注解来为部分方法切换数据源实现读写分离 注解实现读写分离: http://www.cnblogs.com/xiaochangwei/p/4961807.html ...
- MyBatis基础入门--知识点总结
对原生态jdbc程序的问题总结 下面是一个传统的jdbc连接oracle数据库的标准代码: public static void main(String[] args) throws Exceptio ...
- Mybatis XML配置
Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- MyBatis源码分析(一)开篇
源码学习的好处不用多说,Mybatis源码量少.逻辑简单,将写个系列文章来学习. SqlSession Mybatis的使用入口位于org.apache.ibatis.session包中的SqlSes ...
- (整理)MyBatis入门教程(一)
本文转载: http://www.cnblogs.com/hellokitty1/p/5216025.html#3591383 本人文笔不行,根据上面博客内容引导,自己整理了一些东西 首先给大家推荐几 ...
- MyBatis6:MyBatis集成Spring事物管理(下篇)
前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...
随机推荐
- 【React】react&&redux调试工具
一.React调试工具 1.安装 react-developer-tools,在chrome应用商店进行下载,因为网络限制原因,我们可以使用火狐浏览器进行安装下载 a.打开火狐浏览器的附加组件 b.搜 ...
- CentOS 7 64位虚拟机安装过程
第一步:新建一个虚拟机,选择典型安装,点击下一步.
- 自动化运维Shell入门
运维shell 作用 项目部署 项目监控 什么是shell shell是一个程序,/bin/bash/,是一个命令解释器所有linux命令都由他来执行,打开终端就进入了 shell的交互式命令 运行方 ...
- mac 卸载编辑器卸不干净
Configuration ~/Library/Preferences/ Caches ~/Library/Caches/ Plugins ~/Library/Application Support/ ...
- NOPI Excel 数据导入到数据库
/// <summary> /// 上传excel文件 并将文件数据导入到数据库 /// </summary> /// <param name="file&qu ...
- LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...
- 关于 webpack 的研究
webpack的官网:http://webpack.github.io/ 安装webpack 演示项目安装,使用全局安装:npm install webpack -g 真正的项目使用依赖式安装,保证在 ...
- Python删除文件夹
import os os.rmdir('OS-Demo-2') os.removedirs('OS-Demo-3/sub-Dir-1') os.removedirs()会自动将上一级文件夹也删除,谨慎 ...
- html根标签设置font-size为100px,使用rem,body没设置字体大小
今天使用了rem来写样式,前提是要给html设置font-size:100px;这样在计算比例时相对方便点:结果在将一个span标签设置为display:inline-block;时发现span不能能 ...
- Dart编程实例 - 类型测试操作符is
Dart编程实例 - 类型测试操作符is void main() { int n = 2; print(n is int); } 本文转自:http://codingdict.com/article/ ...