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 ...
随机推荐
- 面向对象-PHP面向对象的特性
1.类和公有化 class Computer { //什么叫做类内,就是创建类的花括号内的范围叫做类内,其他地方则类外. //public 是对字段的公有化,这个字段类外即可访问,赋 ...
- spring mvc: rss(xml)输出
准备: rss包插件 Rome 库及其依赖项rome-utils,jdom和slf4j <!-- rss源依赖 --> <!-- https://mvnrepository.com/ ...
- Jenkins的安装和使用
1.可以参考W3C----https://www.w3cschool.cn/jenkins/jenkins-5h3228n2.html 两种方式安装Jenkins a.安装包 b.Jenkins.wa ...
- echarts在vue中使用的感悟
echarts在vue中使用的感悟 echarts作为图表展示的强大存在,每当使用后台系统,或多或少都会使用到,但是作为菜鸟的我,则是一路采坑,各种头大,比比皆是,为了避免下次再犯同样的错误,特意记录 ...
- Django开发BUG "Model class WH_auth.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS."
当进行数据库迁移的时候发生问题,报错如下:RuntimeError: Model class WH_auth.models.User doesn't declare an explicit app_l ...
- html中引入另一个html文件
https://segmentfault.com/q/1010000002954318
- C++复习13.虚析构函数知识
C++ 虚析构函数 20131010 在C++中的虚函数作用是实现基于继承机制的多态,但是我们好像忽略了一种情况,就是虚析构函数.在C++继承机制中,虽然构造函数是不可以使用虚函数声明,但是析构函数是 ...
- 分布式锁-基于ZK和Redis实现
一.基于zookeeper实现分布式锁 1.1 Zookeeper的常用接口 package register; import java.util.List; import java.util.con ...
- java与mysql时间类型对应的问题
项目中遇到一个问题,从后台给出的json字符串中取得的时间,之后通过方法转换成 yyyy-MM-dd hh:mm:ss 的时候,转换后的得到的竟然是1969年...之后排查问题: 发现了在mayba ...
- Hibernate常见配置详细解释
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?xml version='1. ...