MyBatis之一对多映射查询sql配置文件。

简介: 学生---文章的模型一对多模型 学生student.java类 1 package com.bjsxt.sxf.po; 2 3 import java.util.Date; 4 import java.

学生---文章的模型
一对多模型

学生student.java类

 1 package com.bjsxt.sxf.po;  2  3 import java.util.Date;  4 import java.util.List;  5 import java.util.Set;  6  7 /**  8  * 学生  9 * @ClassName: Student 10 * @Description: TODO(这里用一句话描述这个类的作用) 11 * @author 尚晓飞 12 * @date 2014-11-4 上午10:41:19 13 * 14 */ 15 public class Student { 16 private Integer studId;//主键id 17 private String name;//姓名 18 private String email;//email 19 private Date dob;//入学时间 20 private List<Article> articles;//文章集合 21 //set get 方法 空构造 22 } 
View Code

文章Article.java类

 1 package com.bjsxt.sxf.po;  2 /**  3  * 文章  4 * @ClassName: Article  5 * @Description: TODO(这里用一句话描述这个类的作用)  6 * @author 尚晓飞  7 * @date 2014-11-4 上午10:41:07  8 *  9 */ 10 public class Article { 11 private Integer id;//id 12 private String title;//标题 13 private String content;//内容 14 private Student student;//该文章是哪个学生写的 15 16 //set get 方法 空构造 17 }
View Code

student.xml中映射器。此映射器,主要针对一对多模型查询。当查询一方时,将一方拥有的多方也查出来。

 1 <?xml version="1.0" encoding="utf-8"?>  2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  4 <!-- sql集的命名空间,是于数据库交互的桥梁.namespace对应的是映射器的java接口 -->  5 <mapper namespace="com.bjsxt.sxf.mapper.StudentMapper">  6  7  8  9 10 <!--根据id获取学生的基本信息 --> 11 <!-- 当用resultType时返回单个对象,数据库列名和java类属性名必须一致,如不一致,则在sql语句中起别名,使得列名和属性名一致 --> 12 <select id="findStudentById" parameterType="int" resultType="Student"> 13  SELECT students.stud_id as studId,students.`name`,students.email,students.dob FROM students WHERE students.stud_id=#{id} 14 </select> 15 16 17 <!-- 一对多查询 --> 18 19 <!-- resultMap嵌套。查询学生信息时,并将每个学生所写文章的集合也查询出来,赋值在学生对象中的文章集合类中 --> 20 <!-- 由于resultMap中已经将java类中的属性名和表中的列名,进行映射配置。此处不可为列名起别名 --> 21 <!-- 文章模型的映射结果 --> 22 <resultMap type="Article" id="wenzhang"> 23 <id property="id" column="id" /> 24 <result property="title" column="title" /> 25 <result property="content" column="content" /> 26 </resultMap> 27 <!-- 学生的映射结果 --> 28 <resultMap type="Student" id="StudentResult"> 29 <id property="studId" column="stud_id" /> 30 <result property="name" column="name" /> 31 <result property="email" column="email" /> 32 <result property="dob" column="dob" /> 33 <collection property="articles" javaType="ArrayList" column="stud_id" ofType="Article" resultMap="wenzhang"></collection> 34 <!-- ofType是文章集合中的文章类型,column用查处出来的那个列的值,作为外键去查集合结果 --> 35 </resultMap> 36 <!-- 根据id获取学生对象,包含该id学生所写的文章集合,可能有的学生没有写文章,因此多表连接查询用左连接--> 37 <select id="findByIdContentArticle" parameterType="int" resultMap="StudentResult"> 38  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} 39 </select> 40 <!-- 查询出学生的集合,每个学生对象中包含该学生的文章集合 --> 41 <select id="findByQT" parameterType="int" resultMap="StudentResult"> 42  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) 43 </select> 44 45 46 47 <!-- select嵌套查询 --> 48 <!-- 文章的映射 --> 49 <resultMap type="Article" id="wen"> 50 <id property="id" column="id" /> 51 <result property="title" column="title" /> 52 <result property="content" column="content" /> 53 </resultMap> 54 <!-- 根据学生id查询出文章集合 --> 55 <select id="findByStudId" parameterType="int" resultMap="wen"> 56  SELECT ar.id,ar.title,ar.content FROM article ar WHERE ar.stuid=#{id} 57 </select> 58 <resultMap type="Student" id="studentsd"> 59 <id property="studId" column="stud_id" /> 60 <result property="name" column="name" /> 61 <result property="email" column="email" /> 62 <result property="dob" column="dob" /> 63 <collection property="articles" column="stud_id" javaType="ArrayList" ofType="Article" select="findByStudId"></collection> 64 <!-- select当执行完查询学生的sql后再执行根据学生id查文章的sql --> 65 </resultMap> 66 <!-- select嵌套查询,查询出指定id的学生(包含学生的文章集合) --> 67 <select id="findByStudIdS" parameterType="int" resultMap="studentsd"> 68  SELECT st.stud_id,st.`name`,st.email,st.dob FROM students st where st.stud_id=#{id} 69 </select> 70 <!-- select嵌套查询 ,查询出所有的学生集合(每个学生对象中包含该学生的文章集合)--> 71 <select id="findBySelectList" resultMap="studentsd"> 72  SELECT st.stud_id,st.`name`,st.email,st.dob FROM students st 73 </select> 74 75 </mapper>
View Code

Article.xml文章映射器。此处查询的内容为。当查询多方的时候,并把一方查询出来。一对一也是这样查询的。

 1 <?xml version="1.0" encoding="utf-8"?>  2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  4 <!-- sql集的命名空间,是于数据库交互的桥梁.namespace对应的是映射器的java接口 -->  5 <mapper namespace="com.bjsxt.sxf.mapper.ArticleMapper">  6  7 <!-- 查询出指定id的文章 -->  8 <select id="findArticleById" parameterType="int" resultType="Article">  9  SELECT article.id,article.title,article.content FROM article where article.id=#{id} 10 </select> 11 <!-- MyBatis的灵活处,也在此。需要什么样的结果集,就配置什么样的映射 --> 12 <resultMap type="Article" id="ArticleOnly"> 13 <id property="id" column="id" /> 14 <result property="title" column="title" /> 15 <result property="content" column="content" /> 16 </resultMap> 17 <!-- 查询出文章的集合,只是基本信息 --> 18 <select id="findArticleOnly" resultMap="ArticleOnly"> 19  SELECT id,title,content FROM article 20 </select> 21 22 23 <!-- 多对一的查询 --> 24 25 <!--ResultMap嵌套查询 --> 26 <!-- 查询文章时,并将文章的作者学生信息也查询出来 --> 27 <resultMap type="Student" id="studentRe"> 28 <id property="studId" column="stud_id" /> 29 <result property="name" column="name" /> 30 <result property="email" column="email" /> 31 <result property="dob" column="dob" /> 32 </resultMap> 33 <resultMap type="Article" id="ArticleResult"> 34 <id property="id" column="id" /> 35 <result property="title" column="title" /> 36 <result property="content" column="content" /> 37 <association property="student" resultMap="studentRe"></association> 38 </resultMap> 39 <!-- 根据文章id获取文章信息,并将该文章的作者也查询出来 --> 40 <select id="findArticleWithStudentById" parameterType="int" resultMap="ArticleResult"> 41  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} 42 </select> 43 44 <!-- 查询出文章的集合,并将每篇文章的作者也查询出来 --> 45 <select id="findAllArticle" resultMap="ArticleResult"> 46  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 47 </select> 48 49 50 51 52 <!-- select嵌套查询 --> 53 <!-- 根据学生id查询出学生的信息 --> 54 <select id="findStudentByStuid" parameterType="int" resultType="Student"> 55  SELECT st.stud_id as studId,st.`name`,st.email,st.dob FROM students st WHERE st.stud_id=#{id} 56 </select> 57 <resultMap type="Article" id="as"> 58 <id property="id" column="id" /> 59 <result property="title" column="title" /> 60 <result property="content" column="content" /> 61 <association property="student" javaType="Student" column="stuid" select="findStudentByStuid"></association> 62 </resultMap> 63 <!-- 根据select嵌套查询出指定id的文章,并将文章的信息也查询出来 --> 64 <select id="findArticleBySelect" parameterType="int" resultMap="as"> 65  SELECT * FROM article ar where ar.id=#{id} 66 </select> 67 <!-- 根据select嵌套查询出文章的集合,每篇文章的作者也查询出来 --> 68 <select id="findAllBySelect" resultMap="as"> 69  SELECT * FROM article 70 </select> 71 </mapper>
View Code

 

相关文章
|
2月前
|
SQL Java 数据库连接
MyBatis 的映射关系
MyBatis 核心功能之一是映射关系,支持一对一、一对多和多对多三种 ORM 映射。通过实体类与配置文件结合,开发者可灵活实现数据关联,提升数据库操作效率。
235 4
|
8月前
|
SQL Java 数据库连接
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
|
3月前
|
SQL Java 数据库连接
MyBatis的配置文件中定义类型别名(type aliases)的技巧。
类型别名提供了一种便捷的方式来引用复杂的全限定类名。通过使用 `<package>`标签进行自动扫描或使用 `<typeAlias>`标签手动指定,可以在整个MyBatis配置中提高清晰度和维护性。无论是简化mapper文件中的配置,还是提高整体的配置可读性,类型别名都是一个非常有用的配置工具。
176 0
|
6月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
542 5
|
8月前
|
SQL Java 数据库连接
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
|
8月前
|
XML Java 数据库连接
三、MyBatis核心配置文件详解
三、MyBatis核心配置文件详解
183 15
|
8月前
|
SQL 缓存 Java
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
8月前
|
Java 数据库连接 mybatis
MyBatis篇-映射关系(1-1 1-n n-n)
本文介绍了MyBatis中四种常见关系映射的配置方法,包括一对一、一对多、多对一和多对多。**一对一**通过`resultMap`实现属性与字段的映射;**一对多**以用户-角色为例,使用`&lt;collection&gt;`标签关联集合数据;**多对一**以作者-博客为例,利用`&lt;association&gt;`实现关联;**多对多**则通过引入第三方类(如UserForDept)分别在User和Dept类中添加集合属性,并配置对应的`&lt;collection&gt;`标签完成映射。这些方法解决了复杂数据关系的处理问题,提升了开发效率。
|
8月前
|
SQL XML Java
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
227 0
|
5月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
933 1
Spring boot 使用mybatis generator 自动生成代码插件
下一篇