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 多对多查询 以订单和商品为例,即 一个订单可 ...
随机推荐
- 大部分人都不知道的8个python神操作
01 print 打印带有颜色的信息 大家知道 Python 中的信息打印函数 Print,一般我们会使用它打印一些东西,作为一个简单调试. 但是你知道么,这个 Print 打印出来的字体颜色是可以设 ...
- Mac安装多版本JDK
0. 配置JDK环境 安装完成之后,配置.bash_profile文件 使用source ./bash_profile激活 2. 如何切换默认的jdk? 使用java -version就可以看默认版本 ...
- dns的抓包分析
dns: 域名系统(服务)协议 dns的解析全过程: 1. 浏览器先检查自身缓存中有没有被解析过的这个域名对应的ip地址,如果有,解析结束.同时域名被缓存的时间也可通过TTL属性来设置. 2. 如果浏 ...
- ERC20 Short Address Attack
ERC20 Short Address Attack 什么是ERC20 Application Binary Interface(ABI) ERC20 Short Address Attack 开始攻 ...
- Vue移动端项目中下拉刷新和上拉加载
Vue2.0中引入Mint-UI的下拉刷新和上拉加载.简单粗暴 安装Mint-UI npm i mint-ui -S 引入 打开项目的main.js入口文件,引入并使用.注意,为了方便,这里是全部引入 ...
- mysql不同端口的连接
连接mysql3306端口命令 mysql -h58.64.217.120 -ushop -p123456 连接非3306端口(指定其他端口) 的命令 mysql -h58.64.217.120 -P ...
- PAT A1023
简单的大数问题,long long并不能容纳21位数字,这是刚开始没有注意到的 #include<iostream> #include<stdlib.h> #include&l ...
- 吞吐量(TPS)、QPS、并发数、响应时间(RT)
1. 响应时间(RT) 响应时间是指系统对请求作出响应的时间.直观上看,这个指标与人对软件性能的主观感受是非常一致的,因为它完整地记录了整个计算机系统处理请求的时间.由于一个系统通常会提供许多功能, ...
- 用两张图告诉你,为什么你的App会卡顿?
有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? 知道Android究竟是如何在屏幕上显示我们期望的画面的? 对Android的视图架构有整体把握. 学会 ...
- Clickhouse 字符串拆分 OR 一行转多行
Clickhouse 字符串拆分 OR 一行转多行 我想把 '123_456_142354_23543' 通过'_' 下划线进行拆分成