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>
最新文章
- iOS7时代我们用什么来追踪和识别用户?
- 【练习】数据移动---导出(EXPDP)
- 收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation
- jQuery - 9.Ajax
- Ext Js【Hello World】 ——4.1 beta 1
- 2748: [HAOI2012]音量调节
- Mysql --分区(4)List分区
- Androidi性能优化之Java代码优化(摘自Android性能优化一书)
- Linux查看文件以及文件夹的大小
- 给VMware的虚拟机设置静态地址
- js通过class获取元素时的兼容性解决方案
- 用命令行撤销工作区的所有更改(修改文件&;&;新增文件)
- crontab 误删恢复
- 【PHP篇】运算及流程控制
- C# WinForm开发系列 - 文章索引
- [react002] component基本用法
- seek()和tell()在文件里转移
- 【Django】pip 安装和卸载 Django
- c# var的含义与用法
- GIT团队合作探讨之二--Pull Request