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 ...
随机推荐
- Excel_To_DataTable
/// <summary> /// Read data in excel file to datatable /// </summary> /// <param name ...
- Java 9的JDK中值得期待的:不仅仅是模块化
在多次延期后,Java 9将于9月21日以Java开发工具包9的形式出现,这是自2014年3月以来,Java标准版的第一次重大升级.官方列出了JDK 9的大约90个新特性,模块化是最主要的一个.将Ja ...
- SPOJ-ANDROUND -线段树/与操作
ANDROUND - AND Rounds #tree You are given a cyclic array A having N numbers. In an AND round, each e ...
- Centos7 使用Dockerfile 制作自己的Dotnetcore程序镜像
准备Centos7环境及Docker环境 从Docker hub拉取 Microsoft/dotnet 基础镜像(可以使用国内加速) 向Centos7指定目录上传Dotnet Core程序,目录: / ...
- iOS安全系列之 HTTPS
作者:Jaminzzhang 如何打造一个安全的App?这是每一个移动开发者必须面对的问题.在移动App开发领域,开发工程师对于安全方面的考虑普遍比较欠缺,而由于iOS平台的封闭性,遭遇到的安全问题相 ...
- ASP.NET MVC 路由系统类
RouteData public class RouteData { private RouteValueDictionary _dataTokens; private IRouteHandler _ ...
- 如何让history显示时间
linux和unix上都提供了history命令,可以查询以前执行的命令历史记录但是,这个记录并不包含时间项目因此只能看到命令,但是不知道什么时间执行的如何让history记录时间呢? 解决方案 注意 ...
- AndroidStudio构建常见错误解答解决思路
一.Error:Configuration with name 'default' not found.解决思路 出现这问题的原因是你依赖的工程没有make project,意思是你导入项目的工程没有 ...
- Flask数据库常见关系模板代码
常见关系模板代码 以下罗列了使用关系型数据库中常见关系定义模板代码 一对多 示例场景: 用户与其发布的帖子(用户表与帖子表) 角色与所属于该角色的用户(角色表与多用户表) 示例代码 class Rol ...
- 2017.10.24 A test error about ATE device
1 A misunderstands on E-mail Customer: The initial red blink just means theXXX unit has not yet s ...