首页 > 其他 > 详细

mybatis 一对多 多对一

时间:2021-06-13 19:09:09      阅读:24      评论:0      收藏:0      [点我收藏+]

表与表之间 一对多 ,多对一的 关系

  1. 按结果嵌套处理 ------- sql会复杂些 -----------相对简单
  2. 按查询嵌套处理 -------- sql 会好理解----------- resultMap 结果集隐射会痛苦写

teacher 表

 teacher | CREATE TABLE `teacher` (
  `id` int(10) NOT NULL,
  `name` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
insert into teacher (`id`,`name`) values (1,‘秦老师‘);
insert into teacher(`id`,`name`) values (2,‘林老师‘)
insert into teacher(`id`,`name`) values (3,‘刘老师‘)

stu 表

 CREATE TABLE `stu` (
  `id` int(10) NOT NULL,
  `name` varchar(30) NOT NULL,
  `tid` int(10) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`),
  KEY `kftid` (`tid`),
  CONSTRAINT `kftid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
insert into stu (`id`,`name`,`tid`) values (1,‘小红‘,1) ;
insert into stu (`id`,`name`,`tid`) values (1,‘小吕‘,1) ;
insert into stu (`id`,`name`,`tid`) values (1,‘小白‘,1) ;
insert into stu (`id`,`name`,`tid`) values (1,‘小黑‘,1) ;
insert into stu (`id`,`name`,`tid`) values (1,‘小紫‘,1) ;

开始学习 数据表 与表之间的关系

  1. 导入 lombok jar包 , 具体用不用因人而异, 偷懒可以使用lombok 插件,
  2. 导入mybatis 的工具类, 具体就博客就不写了。 以下就是mybatis 的一些配置文件, 太繁琐,太熟了,这里就不写了 mybatis-config.xml
  3. 创建 数据库实体类 映射 ORM stu类 and teacher类
  4. 搭建好开发环境, 写一个简单的sql 执行下, 测试程序是否有问题。

业务需求 --- 编写接口

mapper接口

select s.id ,s.name,t.name from stu s,teacher t where s.tid=t.id;

这里的思维和以前不一致, 显然这里是查询两张表的数据, 那么这里的返回值类型,怎么定义?

思路: 按照查询嵌套处理

  1. 先查询 stu 的所有信息;
  2. 再根据学生表的 tid 指向 老师表的 id ;

由于teacher是一个对象, 对应的数据库字段是‘tid‘ , 是个对象就应该有个类型, 在往里面嵌套 sql

<mapper namespace="com.lsq.Mapper.StuMapper">    <!--绑定接口-->
  <!--查询所有的学生-->
   <select id="QueryStu" resultMap="StudentTeacher">

       SELECT  * from mybatis.stu
   </select>


   <resultMap id="StudentTeacher" type="stu">
       <result property="id" column="id"/>
       <result property="name" column="name"/>
       <association property="teacher" column="tid" javaType="teacher" select="getTeacher"/>  
   </resultMap>

   <!--查询所有的老师-->
   <select id="getTeacher" resultType="teacher">
       SELECT  * FROM  mybatis.teacher  WHERE  id=#{id}
   </select>

</mapper>

按照结果嵌套处理

  <!--按照结果嵌套处理-->
    <select id="QueryStu2" resultMap="stu2">
        SELECT  s.id sid  ,s.name sname ,t.name  tname
         FROM  stu s ,teacher t
         WHERE  s.tid=t.id
    </select>
    
    <resultMap id="stu2" type="stu">
         <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

一对多关系 ,

? 一个老师拥有多个学生

? teacher.class

 private  int id;        //老师工号id
     private  String name;   //老师姓名

    // 一对多 关系     一个老师拥有多个学生
     private List<Stu> stus;

stu.class

   private  int id;       //学生id
    private  String  name; //学生姓名
    private  int tid;      //关联老师表  外键id

TeacherMapper 查询指定老师 的 学生及其老师信息

 Teacher getteacher(@param"tid" int id);

TeacherMapper.xml

<select id="getteacher" resultMap="map">
  select s.id sid , s.name sname ,t.name tname , t.id tid
  from stu s , teacher t
  where s.tid=t.id and t.id=#{tid}
<select>

<resultMap id="map" type="teacher">
    <result property="id" column="tid" />
        <result property="name" column="tname"/>
        <!-- 集合用 collection -->
        <collection property="stus" ofType="stu">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
</resultMap>

测试

  // 查询指定老师 id 的所有学生及老师的信息
    @Test
    public void test2(){
        SqlSession sqlSession = MyabtisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacher(2);
        System.out.println(teacher);

        sqlSession.close();

    }

子查询 方式

teacherMapper

 Teacher getteacher2(@param"tid" int id);

TeacherMapper.xml

 <!--嵌套语句查询 -->
    <select id="getTeacher2" resultMap="map2">
        select * from mybatis.teacher  where  id = #{tid}
    </select>

    <resultMap id="map2" type="teacher">
        <result property="id" column="id"/>                  <这两个没有变可以省略>
        <result property="name" column="name"/>
        <collection property="stus" javaType="ArrayList" ofType="stu" select="stu2" column="id"/>
    </resultMap>

    <select id="stu2" resultType="stu">
        SELECT * FROM  mybatis.stu  WHERE  tid=#{id}
    </select>

小结

的属性

关联 [多对一] 多个学生关联一个老师

集合 [一对多] 一个老师拥有多个学生

javaType 用来指定实体类中属性的类型

ofType 用来指定映射到list 或者集合中的pojo类型, 泛型中的约束类型

1.保证sql的可读性,尽量保证通俗易懂

  1. 注意一对多和多对一中,属性名和字段的问题
    1. 使用lo4j 日志

面试须知

mysql引擎

innoDB 底层原理

索引

索引优化

mybatis 一对多 多对一

原文:https://www.cnblogs.com/lsqjava/p/14880551.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!