步骤:

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多对一与一对多的更多相关文章

  1. Mybatis 多对一和一对多 学习总结04

    1.Mybatis 组件的声明周期 ​ 声明周期是组件的重要问题,Mybatis也常用语多线程环境,错误使用会造成多线程并发问题,为正确编写Mybatis应用程序,我们要掌握Mybatis组件的声明周 ...

  2. MyBatis多对一,一对多,多对多,一对多关联查询

    一.Person实体类 1 public class Person { 2 private Integer personId; 3 private String name; 4 private Int ...

  3. java web(六):mybatis之一对一、一对多、多对多映射

    前言: 百度百科: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...

  4. 后端框架的学习----mybatis框架(9、多对一处理和一对多处理)

    9.多对一处理和一对多处理 #多对一 <!--按照结果集嵌套查询--> <select id="getAllStudent1" resultMap="S ...

  5. Java基础-SSM之mybatis多对多关联

    Java基础-SSM之mybatis多对多关联 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表) 1>.创建teas,stus,links表 u ...

  6. mybatis多对一关联

    mybatis多对一关联查询实现 1.定义实体 定义实体的时候需要注意,若是双向关联,就是说双方的属性中都含有对方对象作为域属性出现, 那么在写toString()方法时需要注意,只让某一方输出即可, ...

  7. Hibernate关联映射(单项多对一和一对多、双向一对多)

    最近总是接触着新的知识点来扩展自己的知识面:不停的让自己在原地接触天空的感觉真的很美好!!!革命没有成功,程序员的我们怎么能不努力呢...... 一.用员工和部门来剖析关联映射的原理. 1)从这张截图 ...

  8. 一步步学习NHibernate(5)——多对一,一对多,懒加载(2)

    请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们建立了Student和Clazz之间的关联属性,并从Student(many)的一方查看了Clazz的信 ...

  9. MyBatis多对多查询

    -------------------siwuxie095                                 MyBatis 多对多查询         以订单和商品为例,即 一个订单可 ...

随机推荐

  1. CSS中“~”(波浪号)、“,”(逗号)、“+”(加号)、“>”(大于号)、“ ”(空格)详解

    “~”:$('pre ~ brother')表示获取pre节点的后面的所有兄弟节点,相当于nextAll()方法: “+”:$('pre + nextbrother')表示获得pre节点的下一个兄弟节 ...

  2. 关于php抑错方法

    在循环里,如果@不能用的话,就使用try catch,是可以的

  3. Linux系统curl获取公网ip

    收集了几个查询当前公网ip的网址,可以通过curl命令方便的查看 curl cip.cc curl ipinfo.io curl myip.ipip.net curl http://members.3 ...

  4. Google Play商店为预注册的游戏和应用提供自动安装功能

    谷歌 Play 商店一直在准备一项功能,它可以自动安装用户预先注册的应用程序和游戏.似乎该功能现已开始向第一批用户推出.有些人在预注册时会看到一个新选项,使他们能够利用发布时自动安装的功能. 用户在 ...

  5. MySQL简介和安装

    一.关系型数据库初识 1.1 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我 ...

  6. 【已解决】React项目中按需引入ant-design报错TypeError: injectBabelPlugin is not a function

    react项目中ant-design按需加载,使用react-app-rewired的时候报错 运行npm start或者yarn start报如下错误: TypeError: injectBabel ...

  7. Spark学习笔记(一)

    概念: Spark是加州大学伯克利分校AMP实验室,开发的通用内存并行计算框架. 支持用scala.java和Python等语言编写应用程序.相较于Hdoop,往往有更好的运行效率. Spark包括了 ...

  8. Error: Can't find Python executable "G:\Python27"

    错误如题,node-gyp官网介绍不够详细,应设置python.exe的具体绝对路径,如下所示: npm config set python G:\Python27\python.exe 转载于:ht ...

  9. iOS9.2.1 App从AppStore上下载闪退问题

    首先这是小编的第一篇文章,我是一名做iOS开发的小白,出于爱好会更新发表些相关的技术文章,偶尔也会发些视频.恳请大家不要去嘲笑一个努力的人,要是做的不好请多多评论,反正我也不改. 好了!敲黑板!!说正 ...

  10. 关于通过Date.getTime()得到1970年01月1日0点零分问题验证

     public static String getTimestamp_1970() throws Exception {   java.text.SimpleDateFormat formater = ...