大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
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);
测试环境搭建
- 新建实体类Teacher Student
- 建立Mapper接口
- 简历Mapper.xml
- 核心配置文件中绑定注册Mapper接口
- 测试查询是否能够成功
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>
小结
- 关联-association[多对一]
- 集合-collection[-对多]
- 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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/168960.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...