MyBatis之一对多映射查询sql配置文件。
学生---文章的模型
一对多模型
学生student.java类
package com.bjsxt.sxf.po; import java.util.Date;
import java.util.List;
import java.util.Set; /**
* 学生
* @ClassName: Student
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 尚晓飞
* @date 2014-11-4 上午10:41:19
*
*/
public class Student {
private Integer studId;//主键id
private String name;//姓名
private String email;//email
private Date dob;//入学时间
private List<Article> articles;//文章集合
//set get 方法 空构造
}
文章Article.java类
package com.bjsxt.sxf.po;
/**
* 文章
* @ClassName: Article
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 尚晓飞
* @date 2014-11-4 上午10:41:07
*
*/
public class Article {
private Integer id;//id
private String title;//标题
private String content;//内容
private Student student;//该文章是哪个学生写的 //set get 方法 空构造
}
student.xml中映射器。此映射器,主要针对一对多模型查询。当查询一方时,将一方拥有的多方也查出来。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- sql集的命名空间,是于数据库交互的桥梁.namespace对应的是映射器的java接口 -->
<mapper namespace="com.bjsxt.sxf.mapper.StudentMapper"> <!--根据id获取学生的基本信息 -->
<!-- 当用resultType时返回单个对象,数据库列名和java类属性名必须一致,如不一致,则在sql语句中起别名,使得列名和属性名一致 -->
<select id="findStudentById" parameterType="int" resultType="Student">
SELECT students.stud_id as studId,students.`name`,students.email,students.dob FROM students WHERE students.stud_id=#{id}
</select> <!-- 一对多查询 --> <!-- resultMap嵌套。查询学生信息时,并将每个学生所写文章的集合也查询出来,赋值在学生对象中的文章集合类中 -->
<!-- 由于resultMap中已经将java类中的属性名和表中的列名,进行映射配置。此处不可为列名起别名 -->
<!-- 文章模型的映射结果 -->
<resultMap type="Article" id="wenzhang">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
</resultMap>
<!-- 学生的映射结果 -->
<resultMap type="Student" id="StudentResult">
<id property="studId" column="stud_id" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="dob" column="dob" />
<collection property="articles" javaType="ArrayList" column="stud_id" ofType="Article" resultMap="wenzhang"></collection>
<!-- ofType是文章集合中的文章类型,column用查处出来的那个列的值,作为外键去查集合结果 -->
</resultMap>
<!-- 根据id获取学生对象,包含该id学生所写的文章集合,可能有的学生没有写文章,因此多表连接查询用左连接-->
<select id="findByIdContentArticle" parameterType="int" resultMap="StudentResult">
SELECT st.stud_id ,st.`name`,st.email,st.dob,ar.id,ar.title,ar.content FROM students st LEFT JOIN article ar ON (st.stud_id=ar.stuid) WHERE st.stud_id=#{id}
</select>
<!-- 查询出学生的集合,每个学生对象中包含该学生的文章集合 -->
<select id="findByQT" parameterType="int" resultMap="StudentResult">
SELECT st.stud_id ,st.`name`,st.email,st.dob,ar.id,ar.title,ar.content FROM students st LEFT JOIN article ar ON(st.stud_id=ar.stuid)
</select> <!-- select嵌套查询 -->
<!-- 文章的映射 -->
<resultMap type="Article" id="wen">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
</resultMap>
<!-- 根据学生id查询出文章集合 -->
<select id="findByStudId" parameterType="int" resultMap="wen">
SELECT ar.id,ar.title,ar.content FROM article ar WHERE ar.stuid=#{id}
</select>
<resultMap type="Student" id="studentsd">
<id property="studId" column="stud_id" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="dob" column="dob" />
<collection property="articles" column="stud_id" javaType="ArrayList" ofType="Article" select="findByStudId"></collection>
<!-- select当执行完查询学生的sql后再执行根据学生id查文章的sql -->
</resultMap>
<!-- select嵌套查询,查询出指定id的学生(包含学生的文章集合) -->
<select id="findByStudIdS" parameterType="int" resultMap="studentsd">
SELECT st.stud_id,st.`name`,st.email,st.dob FROM students st where st.stud_id=#{id}
</select>
<!-- select嵌套查询 ,查询出所有的学生集合(每个学生对象中包含该学生的文章集合)-->
<select id="findBySelectList" resultMap="studentsd">
SELECT st.stud_id,st.`name`,st.email,st.dob FROM students st
</select> </mapper>
Article.xml文章映射器。此处查询的内容为。当查询多方的时候,并把一方查询出来。一对一也是这样查询的。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- sql集的命名空间,是于数据库交互的桥梁.namespace对应的是映射器的java接口 -->
<mapper namespace="com.bjsxt.sxf.mapper.ArticleMapper"> <!-- 查询出指定id的文章 -->
<select id="findArticleById" parameterType="int" resultType="Article">
SELECT article.id,article.title,article.content FROM article where article.id=#{id}
</select>
<!-- MyBatis的灵活处,也在此。需要什么样的结果集,就配置什么样的映射 -->
<resultMap type="Article" id="ArticleOnly">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
</resultMap>
<!-- 查询出文章的集合,只是基本信息 -->
<select id="findArticleOnly" resultMap="ArticleOnly">
SELECT id,title,content FROM article
</select> <!-- 多对一的查询 --> <!--ResultMap嵌套查询 -->
<!-- 查询文章时,并将文章的作者学生信息也查询出来 -->
<resultMap type="Student" id="studentRe">
<id property="studId" column="stud_id" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="dob" column="dob" />
</resultMap>
<resultMap type="Article" id="ArticleResult">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
<association property="student" resultMap="studentRe"></association>
</resultMap>
<!-- 根据文章id获取文章信息,并将该文章的作者也查询出来 -->
<select id="findArticleWithStudentById" parameterType="int" resultMap="ArticleResult">
SELECT ar.id,ar.title,ar.content,st.stud_id as studId,st.`name`,st.email,st.dob FROM article ar,students st WHERE ar.stuid=st.stud_id AND ar.id=#{id}
</select> <!-- 查询出文章的集合,并将每篇文章的作者也查询出来 -->
<select id="findAllArticle" resultMap="ArticleResult">
SELECT ar.id,ar.title,ar.content,st.stud_id as studId,st.`name`,st.email,st.dob FROM article ar,students st WHERE ar.stuid=st.stud_id
</select> <!-- select嵌套查询 -->
<!-- 根据学生id查询出学生的信息 -->
<select id="findStudentByStuid" parameterType="int" resultType="Student">
SELECT st.stud_id as studId,st.`name`,st.email,st.dob FROM students st WHERE st.stud_id=#{id}
</select>
<resultMap type="Article" id="as">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
<association property="student" javaType="Student" column="stuid" select="findStudentByStuid"></association>
</resultMap>
<!-- 根据select嵌套查询出指定id的文章,并将文章的信息也查询出来 -->
<select id="findArticleBySelect" parameterType="int" resultMap="as">
SELECT * FROM article ar where ar.id=#{id}
</select>
<!-- 根据select嵌套查询出文章的集合,每篇文章的作者也查询出来 -->
<select id="findAllBySelect" resultMap="as">
SELECT * FROM article
</select>
</mapper>
MyBatis之一对多映射查询sql配置文件。的更多相关文章
- mybatis 多表查询sql
在使用spring,spring mvc,mybatis时,mybatis链接数据库做多表查询的时候,sql语句中直接使用left join等链接字符就可以 链接多个表,参数类型是parameterT ...
- mybatis之模糊查询SQL
一,MySQL数据库 name like concat('%' , #{name} , '%') 二,Oracle数据库 name like '%' || #{name} || '%'
- mybatis第二天——动态SQL与关联查询
大纲摘要: 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If b) Where c) Foreach d) Sql片段 3.关联查询 a) 一对一关联 b) 一 ...
- 将复杂查询写到SQL配置文件--SOD框架的SQL-MAP技术简介
引言 今天看到一片热门的博客, .NET高级工程师面试题之SQL篇 ,要求找出每一个系的最高分,并且按系编号,学生编号升序排列.这个查询比较复杂,也比较典型,自从用了ORM后,很久没有写过SQL语句了 ...
- 【Mybatis】MyBatis之Sql配置文件的使用(四)
上一章[Mybatis]MyBatis对表执行CRUD操作(三),已经讲了基本操作,本章介绍Sql配置文件中常用功能 1.插入返回主键 2.参数值的获取方式 3.resultMap使用 插入返回主键 ...
- MyBatis是支持普通 SQL查询
MyBatis是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis 使用简单的 XML或注解用于配置 ...
- Mybatis之关联查询及动态SQL
前言 实际开发项目中,很少是针对单表操作,基本都会联查多表进行操作,尤其是出一些报表的内容.此时,就可以使用Mybatis的关联查询还有动态SQL.前几篇文章已经介绍过了怎么调用及相关内容,因此这里只 ...
- Mybatis延迟加载和查询缓存
摘录自:http://www.linuxidc.com/Linux/2016-07/133593.htm 阅读目录 一.延迟加载 二.查询缓存 一.延迟加载 resultMap可以实现高级映射(使用a ...
- mybatis中的查询缓存
一: 查询缓存 Mybatis提供查询缓存,用于减轻数据压力,提高数据库压力. Mybatis提供一级缓存和二级缓存. 在操作数据库时需要构造SqlSession对象,在对象中有一个数据结构(Hash ...
随机推荐
- js数组与字符串的相互转换
一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: var a, b,c; a = new Array(a,b,c,d,e); b = a.join('-'); //a-b-c-d ...
- IE8下使用asp.net core mvc+jquery ajaxSubmit问题
由于项目中一些特殊的地方使用了ajaxSubmit提交数据,但发现在IE8中出现问题,使用该方式提交数据后,无法返回提交结果,而是直接下载该方法名的一个文件,翻阅了园子,终于找到了最简单的解决办法,特 ...
- SSM的Maven项目搭建过程
POM文件 父项目管理jar包,pom <modelVersion>4.0.0</modelVersion> <groupId>cn.e3mall</grou ...
- 到底啥是平台,到底啥是中台?李鬼太多,不得不说(ZT)
(1)哪些不是中台,而是应该叫平台 做开发,有所谓的三层技术架构:前端展示层.中间逻辑层.后端数据层.我们现在讲的中台不在这个维度上. 做开发,还有所谓的技术中间件.一开始我们没有中间件的概念,只有操 ...
- Linux 系统启动过程,Linux 系统目录结构
一.Linux 系统启动过程 linux启动时我们会看到许多启动信息. Linux系统的启动过程并不是大家想象中的那么复杂,其过程可以分为5个阶段: 内核的引导. 运行 init. 系统初始化. 建立 ...
- RabbitMQ消息队列(十)RPC应用2
基于RabbitMQ RPC实现的主机异步管理 地址原文:http://blog.51cto.com/baiying/2065436,作者大大,我把原文贴出来了啊.不要告我 root@ansible: ...
- 分析器错误信息: 未能加载类型“xxx.Global”。
Global.asax错误 分析器错误 说明: 在分析向此请求提供服务所需资源时出错.请检查下列特定分析错误详细信息并适当地修改源文件. 分析器错误信息: 未能加载类型“xxx.Global”. 源错 ...
- 十三、dbms_flashback(用于激活或禁止会话的flashback特征)
1.概述 作用:用于激活或禁止会话的flashback特征,为了使得普通用户可以使用该包,必须要将执行该包的权限授予这些用户,grant execute on dbms_flashback to sc ...
- Docker - 在Ubuntu 14.04 Server上的安装Docker
在 Ubuntu 14.04 Server 上安装过程是最简单的, 其满足了安装 Docker的所有要求,只需要执行如下安装脚本即可. 如果你有可能,请使用14.04版本的Ubuntu, 避免给自己挖 ...
- Hadoop学习资料整理
1.hadoop相关 hadoop 0.18文档(详细介绍Hadoop,MapReduce,FS Shell,Streaming等) hadoop资料汇总 2.实习的时候用的是streaming,非j ...