mybatis多对一与一对多
步骤:
1.创建maven项目
2.编写工具类
3.编写实体类
4.编写mapper接口
5.配置xml
6.测试
多对一:多个学生关联一个老师
工具类:
//sqlSessionFactory -->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory; static {
try {//获取sqlSession对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
} public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
}
其中
String resource = "mybatis-config.xml";
mybatis-config.xml中需要注册mapper和连接数据库等操作
实体类:
public class Student {
private int id;
private String name;
private Teacher teacher;
}
//省略get/set方法
public class Teacher {
private int id;
private String name;
}
//省略get/set方法
mapper:
public interface TeacherMapper {
Teacher getTeacher(@Param("tid") Integer id);
Teacher getTeacher2(@Param("tid") Integer id);
}
xml:
方式一(嵌套查询)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.StudentMapper">
<select id="getStudent" resultMap="StudentTeacher">
select * from mybatis.student
</select>
<resultMap id="StudentTeacher" type="Student">
<id property="id" column="id"></id>
<id property="name" column="name"></id>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"></association>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{id}
</select>
</mapper>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"></association>
中的select要与下面的select标签中的id一致,相当于在里面在嵌套一个查询语句
方式二(子查询)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.StudentMapper">
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname,t.id teid
from mybatis.student s,mybatis.teacher t where s.tid = t.id
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<association property="teacher" javaType="Teacher">
<result property="id" column="teid"></result>
<result property="name" column="tname"></result>
</association>
</resultMap>
</mapper>
对以上标签解释:
namespace是命名空间 对应的是一个Dao/Mapper接口(注意要写的是全限定类名!!)
select标签中的id名要与相关mapper中的方法名保持一致!!
resultMap是对复杂类型的结果集映射(resultMap中的id要与select中的resultMap中的名称一致!!)
type属性也可以直接写全限定类名,若想简洁一点必须在xml文件中配置别名!!!
resultMap中的id字段是对主键进行映射,而result是对普通字段进行映射
result标签中的property对应的是实体类中的属性名,colum对应的是数据库中的字段名
javaType 用来指定实体类的属性 ofType 是指泛型中的约束类型(一对多会用到)
一对多:一个老师教多名学生
实体类中的属性有所改变
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生
private List<Student> students;
}
public class Student {
private int id;
private String name;
private int tid;
}
这里返回的是一个集合List 而上面的多对一返回的是一个对象Teacher
<select id="getTeacher" resultMap="StudentTeacher">
SELECT s.id sid,s.name sname,t.name tname,t.id tid
FROM teacher t,student s where t.id = s.tid and t.id=#{tid}
</select>
<resultMap id="StudentTeacher" type="Teacher">
<result property="id" column="tid"></result>
<result property="name" column="tname"></result>
<collection property="students" ofType="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<result property="tid" column="tid"></result>
</collection>
</resultMap>
ofType 是指泛型中的约束类型 老师实体中有的属性是
List<Student> students
其中List是实体类的属性 Student就是泛型约束类型 总结:
对于复杂的属性 我们需要单独处理 对象:association 集合:collection 多对一中是使用association(因为在学生实体类中Teacher是一个对象) 而一对多中是使用到collection(因为查询到的是一个集合类型) 嵌套查询sql语句会简单一点,但是映射时会复杂一些,而子查询是sql语句复杂一些,映射会相对简单和容易理解
mybatis多对一与一对多的更多相关文章
- Mybatis 多对一和一对多 学习总结04
1.Mybatis 组件的声明周期 声明周期是组件的重要问题,Mybatis也常用语多线程环境,错误使用会造成多线程并发问题,为正确编写Mybatis应用程序,我们要掌握Mybatis组件的声明周 ...
- MyBatis多对一,一对多,多对多,一对多关联查询
一.Person实体类 1 public class Person { 2 private Integer personId; 3 private String name; 4 private Int ...
- java web(六):mybatis之一对一、一对多、多对多映射
前言: 百度百科: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
- 后端框架的学习----mybatis框架(9、多对一处理和一对多处理)
9.多对一处理和一对多处理 #多对一 <!--按照结果集嵌套查询--> <select id="getAllStudent1" resultMap="S ...
- Java基础-SSM之mybatis多对多关联
Java基础-SSM之mybatis多对多关联 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表) 1>.创建teas,stus,links表 u ...
- mybatis多对一关联
mybatis多对一关联查询实现 1.定义实体 定义实体的时候需要注意,若是双向关联,就是说双方的属性中都含有对方对象作为域属性出现, 那么在写toString()方法时需要注意,只让某一方输出即可, ...
- Hibernate关联映射(单项多对一和一对多、双向一对多)
最近总是接触着新的知识点来扩展自己的知识面:不停的让自己在原地接触天空的感觉真的很美好!!!革命没有成功,程序员的我们怎么能不努力呢...... 一.用员工和部门来剖析关联映射的原理. 1)从这张截图 ...
- 一步步学习NHibernate(5)——多对一,一对多,懒加载(2)
请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们建立了Student和Clazz之间的关联属性,并从Student(many)的一方查看了Clazz的信 ...
- MyBatis多对多查询
-------------------siwuxie095 MyBatis 多对多查询 以订单和商品为例,即 一个订单可 ...
随机推荐
- MySQL为某字段加前缀、后缀
在开发过程中,可能会遇到加前缀或者后缀的情况.比如为视频添加路径时,如果手动加起来肯定慢,而且比较不符合程序员的特点,我们就应该能让程序跑就不会手动加. 使用UPDATE sql 语句:update ...
- opencv-5-图像遍历与图像改变
opencv-5-图像遍历与图像改变 opencvc++qt 目录 目录 开始 图像的像素点访问与遍历 opencv 座标定义 下标访问 指针访问 迭代器法访问 遍历访问时间对比 图像操作 图像叠加 ...
- Linux发送邮件命令mail,mutt
邮件常常是Linux下监控报警手段之一.Linux下的mail命令可以方便,快速的完成发送邮件. 1. Linux发邮件2种常见客户端命令 1.1 mail命令(推荐) 语法: 无邮件正文 mail ...
- 【Linux常见命令】wc命令
wc - print newline, word, and byte counts for each file wc命令用于计算字数. 利用wc指令我们可以计算文件的Byte数.字数.或是列数,若不指 ...
- 常用的CSS小技巧
实际开发过程中会遇到一些需要用CSS小技巧处理的布局问题,现在分享几个个人工作中遇到的小问题和解决方案. 1.inline元素间的空白间隙 这里要介绍一个神器font-size:0. 如果你写了个列表 ...
- CultureInfo 类中需要的【区域性名称】查询
2019独角兽企业重金招聘Python工程师标准>>> 提供有关特定区域性的信息(对于非托管代码开发,则称为"区域设置"). 这些信息包括区域性的名称.书写系统. ...
- fullpage.js禁止滚动
http://www.wenjiangs.com/doc/fullpage-method 转载于:https://www.cnblogs.com/hzz-/p/8268771.html
- 数学--数论--整除分块(巨TM详细,学不会,你来打我)
1.概念 从一道例题说起 在介绍整除分块之前,我们先来看一道算数题:已知正整数n,求∑i=1n⌊ni⌋已知正整数n,求∑i=1n⌊ni⌋在介绍整除分块之前,我们先来看一道算数题: 已知正整数n,求∑i ...
- 无向图双连通分量BCC(全网最好理解)
不是标题党,之前我也写过一篇比较全的,但是对于初学者不友好.传送门? 双连通分量(Biconnected component): 1.边双联通 E-BCC 2.点双连通 V-BCC 双 ...
- Unity 游戏框架搭建 2019 (四十二、四十三) MonoBehaviour 简化 & 定时功能
MonoBehaviour 简化 在前两篇,我们完成了第九个示例.为了完善第九个示例,我们复习了类的继承,又学习了泛型和 params 关键字. 我们已经接触了类的继承了.接触继承之前,把类仅仅当做是 ...