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 ...
随机推荐
- python脚本6_打印菱形
#输入菱形最宽的行所在的行数,打印菱形 m = int(input(">>>")) for n in range(m): print(" "* ...
- DataTable 操作
public void CreateTable() { //创建表 DataTable dt = new DataTable(); //1.添加列 dt.Columns.Add("Name& ...
- 转载:Object的create方法文档
源地址:https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/Object/create#.E4.B ...
- VMWare虚拟机网络配置
Bridged(桥接模式) 桥接模式相当于虚拟机和主机在同一个真实网段,VMWare充当一个集线器功能(一根网线连到主机相连的路由器上),所以如果电脑换了内网,静态分配的ip要更改.图如下: NAT( ...
- LabVIEW之安装队列工具包AMC安装问题解决
LabVIEW之安装队列工具包AMC安装问题解决--VIPM无法连接LabVIEW 彭会锋 参考资料: http://www.labviewpro.net/forum_post_detail.php? ...
- SpringBoot自动配置的实现原理
之前一直在用SpringBoot框架,一直感觉SpringBoot框架自动配置的功能很强大,但是并没有明白它是怎么实现自动配置的,现在有空研究了一下,大概明白了SpringBoot框架是怎么实现自动配 ...
- 搞懂分布式技术10:LVS实现负载均衡的原理与实践
搞懂分布式技术10:LVS实现负载均衡的原理与实践 浅析负载均衡及LVS实现 原创: fireflyc 写程序的康德 2017-09-19 负载均衡 负载均衡(Load Balance,缩写LB)是一 ...
- MFC sendmessage实现进程间通信
用sendmessage实现进程间通信. 1.WM_COPYDATA实现进程间通信 实现方式是发送WM_COPYDATA消息. 发送程序: LRESULT copyDataResult; //copy ...
- 【Python】什么是闭包
文章转载自:点这里 在 Python 中很多教材都没有提及什么是闭包,但在定义一个 Decorator 时,就已经用到闭包了.如果不理解什么是闭包,则不可能清晰掌握Decorator 装饰器. 要形成 ...
- java开发中beancopy比较
在java应用开发过程中不可避免的会使用到对象copy属性赋值. 1.常用的beancopy工具 组织(包) 工具类 基本原理 其他 apache PropertyUtils java反射 B ...