多对一
1.图形介绍
对于学生这边而言, 关联 ;多个学生,关联一个老师 【多对一】
2.环境搭建及测试
- 建立一个数据库
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
package pojo;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private Teacher teacher;
}
package pojo;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private int id;
private String name;
}
- 建立Mapper接口
package mapper;
import pojo.Student;
import java.util.List;
public interface StudentMapper {
List<Student> getStudentList();
}
package mapper;
import pojo.Teacher;
import java.util.List;
public interface TeacherMapper {
List<Teacher> getTeacherList();
}
- 建立Mapper.XML文件
<mapper namespace="mapper.StudentMapper">
<select id="getStudentList" resultMap="Userq">
select s.id sid,t.id tid,s.name sname,t.name tname
from student s ,teacher t
where s.tid=t.id
</select>
<resultMap id="Userq" type="pojo.Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<association property="teacher" javaType="Teacher">
<result property="id" column="tid"></result>
<result property="name" column="tname"></result>
</association>
</resultMap>
</mapper>
- 在核心配置文件中绑定注册我们的Mapper接口或者文件!
<mappers>
<!--<mapper resource="Mapper.xml"/>-->
<mapper resource="StudentMapper.xml"></mapper>
<mapper resource="TeacherMapper.xml"></mapper>
</mappers>
- 测试查询是否能够成功!
测试类的编写以及工具类和之前的一样
一对多
对于老师而言, 集合 , 一个老师,有很多学生 【一对多】
环境搭建
环境搭建和上面的一样(在实体类和mapper.xml编写有区别)
- 实体类
@Data
public class Student {
private int id;
private String name;
private int tid;
}
@Data
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生
private List<Student> students;
}
- 建立Mapper.XML文件
<!--按结果嵌套查询-->
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid, s.name sname, t.name tname,t.id tid
from student s,teacher t
where s.tid = 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>
注意: 复杂的属性,我们需要单独处理 对象
JavaType 用来指定实体类中属性的类型
ofType 用来指定映射到List或者集合中的 pojo类型,泛型中的约束类型!
动态SQL
1.什么是动态SQL
动态SQL:动态SQL就是指根据不同的条件生成不同的SQL语句
动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似。
- if
- choose(when, otherwise)
- trim (where, set)
- foreach
2.if标签(以作者和标题查询)
<select id="getBlogbyif" parameterType="map" resultType="blog">
select * from mybatis.blog where id=#{id}
<if test="title!= null">
and title = #{title} </if>
<if test="author != null">
and author = #{author}
</if>
</select>
运行结果:
3.if 和 where 标签
<select id="getBlogbyif" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
运行结果:
注意:如果标签返回的内容是以 AND 或 OR 开头的,则它会剔除掉。
4.set和if标签
<update id="updateBlogbyif" parameterType="map">
update mybatis.blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author}
</if>
</set>
where id = #{id}
</update>
运行结果:
注意:set 元素会动态前置 SET 关键字,同时也会消除无关的逗号,因为用了条件语句之后很可能就会在生成的赋值语句的后面留下这些逗号。
5.choose标签(相当于switch)
类似于 Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。
<select id="queryBlogChoose" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<choose>
<when test="title != null">
title = #{title}
</when>
<when test="author != null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
运行结果之一:
6.foreach标签
<select id="getBlogbyif" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
属性类型:
- collection:collection 属性的值有三个分别是 list、array、map 三种,分别对应的参数类型为:List、数组、map 集合。
- item :表示在迭代过程中每一个元素的别名
- index :表示在迭代过程中每次迭代到的位置(下标)
- open :前缀
- close :后缀
- separator :分隔符,表示迭代时每个元素之间以什么分隔
运行结果:
7.trim标签
set标签
<trim prefix="SET" suffixOverrides=",">
...
</trim>
where标签
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
set标签和where标签本质上就是trim标签,可以用trim替换