tomcat java_tomcat和maven的区别

tomcat java_tomcat和maven的区别复杂的sql查询环境搭建CREATE TABLE `teacher` ( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8INSERT INTO teacher(id, name) VALUES (1,’秦老师’); CREATE TABLE `student` ( `id` INT(10) NOT NULL


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

复杂的sql查询

环境搭建

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,'秦老师'); 

CREATE TABLE `student` (
  `id` INT(10) NOT NULL,
  `name` VARCHAR(30) DEFAULT NULL,
  `tid` INT(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fktid` (`tid`),
  CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (1, '小明', 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (2, '小红', 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (3, '小张', 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (4, '小李', 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (5, '小王', 1);

测试环境搭建

  1. 新建实体类Teacher Student
  2. 建立Mapper接口
  3. 简历Mapper.xml
  4. 核心配置文件中绑定注册Mapper接口
  5. 测试查询是否能够成功
    在这里插入图片描述
package com.kuang.pojo;

public class Student { 
   
    private int id;
    private String name;
    //学生需要关联一个老师!
    private Teacher teacher;
}
package com.kuang.pojo;

public class Teacher { 
   
    private int id;
    private String name;
    //一个老师有多个学生
    private List<Student> students;
}

多对一实例

public interface StudentMapper { 
   
    //查询所有的学生信息,以及对应的老师的信息
    public List<Student> getStudent();
}

按照查询嵌套处理

<mapper namespace="com.kuang.dao.StudentMapper">
    <!-- 1.查询所有的学生信息 2.根据查询出来的学生的id,寻找对应的老师 -->
    <select id="getStudent" resultMap="StudentTeacher">
        select  s.id,s.name,t.name from student s,teacher t where s.tid = t.id;
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--复杂的属性,我们需要单独处理 对象:association 集合:collection -->
        <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="getStudent" resultMap="StudentTeacher">
        select  s.id sid,s.name sname,t.name tname
        from student s,teacher t
        where s.tid = t.id;
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" column="tname" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

一对多

按照结果嵌套查询


public interface TeacherMapper { 
   
    //获取老师
    List<Teacher> getTeacher();
}
<mapper namespace="com.kuang.dao.TeacherMapper">
    <!--按照查询嵌套处理-->
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from mybatis.student s,mybatis.teacher t
        where s.id = t.id and t.id = #{tid}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
</mapper>

按照查询嵌套处理

<mapper namespace="com.kuang.dao.TeacherMapper">


    <select id="getTeacher" resultMap="TeacherStudent">
        select * from mybatis.teacher where id = #{tid}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacher" column="id"/>
    </resultMap>
    <select id="getStudentByTeacherId" resultType="Student">
        select * from mybatis.student where tid = #{tid}
    </select>
</mapper>

小结

  1. 关联-association[多对一]
  2. 集合-collection[-对多]
  3. javaType & ofType
    • javaType 用来指定
    • ofType 用来指定映射到List或者集合中的pojo类型,泛型中的类型

动态SQL

动态SQL就是根据不同的条件生成不同的SQL语句

<select id="findActiveBlogLike" resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</select>

foreach
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/168810.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 上海java培训哪家教育机构比较好_上海it培训机构排名前十

    上海java培训哪家教育机构比较好_上海it培训机构排名前十学习Java编程不在是校内学生们的专利了,随着职场上竞争越来越激烈,毫无基础的或想要转行的求职者对于学习Java的积极性也是越来越高,因此,市面上涌现了大批的成人Java培训机构,为了不让大家纠结,小编花费了一周时间给大家专门整理了上海Java培训机构排名榜单,主要从口碑,师资,专一性,就业率等多方面得出的上海Java培训机构排名,仅供正在选择Java培训机构的小伙们参考。1.上海动力节点评价:动力节点大家学Java的话肯定也都听说过,单科教学,一直是秉承着从学员角度,全心全意保障学员利益.

    2022年10月3日
  • freehosting申请空间和ssh -D设置

    freehosting申请空间和ssh -D设置前段时间申请了website.org的免费空间,可是有广告.在这时向大家推荐freehosting.com.Freehosting.com是一家创建于1996年的美国网站,国内在2006年有介绍过它的免费PHP空间,不过没能找到演示,目前免费空间的主机放在德国,提供1G存储空间,月流量为10G,采用CPanel控制管理面板(有简体中文版),支持FTP和Web在线文件管理(可在线解压缩),…

    2022年10月8日
  • 河北对口计算机录取分数线_河北对口计算机专科院校名单

    河北对口计算机录取分数线_河北对口计算机专科院校名单技校网专门为您推荐的类似问题答案问题1:2009年河北对口计算机高考分数线360问题2:谁能告诉我湖南职高计算机专业对口升学本科和专科分数线湖南省2010年普通高校职高对口招生录取控制分数线代码专业门类本科专科71师范44820072种植54473养殖52374机电56675电子电工50676计算机55877建筑48278旅游48981…

    2022年9月13日
  • settime和setinterval_setinterval是异步还是同步

    settime和setinterval_setinterval是异步还是同步setTimeout和setInterval的语法相同。它们都有两个参数,一个是将要执行的代码字符串,还有一个是以毫秒为单位的时间间隔,当过了那个时间段之后就将执行那段代码。这两个函数的区别是:setInterval在执行完一次代码之后,经过了那个固定的时间间隔,它还会自动重复执行代码,而setTimeout只执行一次那段代码。有一次我没有分清,使用了setInterval,导致电脑崩溃了。setTimeout表面上是只执行一次,只是起到延迟作用。但是也可以通过创建一个函数循环重复调用…

    2022年10月4日
  • arping指令linux,arping

    arping指令linux,arping例a,指定IP发送ARP请求[root@Blackghost~]arping192.168.1.11ARPING192.168.1.11from192.168.1.6eth0Unicastreplyfrom192.168.1.11[08:00:27:7e:b8:08]2.780msUnicastreplyfrom192.168.1.11[08:00:27:7e:b8…

    2022年6月10日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号