文章目录
Mybatis
九、Lombok
使用步骤:
-
在IDEA中安装Lombok插件!
-
在项目中导入lombok的jar包
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency>
-
在实体类上加注解即可!
@Data @AllArgsConstructor @NoArgsConstructor
@Getter and @Setter
@FieldNameConstants
@ToString
@EqualsAndHashCode
@AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor
@Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger
@Data
@Builder
@Singular
@Delegate
@Value
@Accessors
@Wither
@SneakyThrows
说明:
@Data:无参构造,get、set、tostring、hashcode,equals
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@ToString
@Getter
十、多对一处理
- 多个学生,对应一个老师
- 对于学生这边而言, 关联 … 多个学生,关联一个老师 【多对一】
- 对于老师而言, 集合 , 一个老师,有很多学生 【一对多】
1.测试环境搭建
- 导入lombok
- 新建实体类 Teacher,Student
- 建立Mapper接口
- 建立Mapper.XML文件
- 在核心配置文件中绑定注册我们的Mapper接口或者文件!【方式很多,随心选】
- 测试查询是否能够成功!
2.多表查询方法一:按照查询嵌套处理
<!--
在做多对一的时候,思路:
1.查询所有的学生信息
2.根据查询出来的学生的tid,寻找对应的老师
-->
<select id="getStudent" resultMap="map">
select * from student s,teacher t where s.tid=t.id;
</select>
<!--本质最后返回的是一个学生,id是学生的id,name是学生的name,association则是最后的对象属性-->
<resultMap id="map" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--复杂 的属性,我们需要单独处理
对象:association
集合:collection
-->
<!--property的值是指类中的字段,column的值是指数据库中的字段,javaType(=resultType)的值为对象的类型-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{tid}
</select>
3.多表查询方法二:按照结果嵌套处理☆
<select id="getStudent2" resultMap="map2">
select s.id sid,s.name smame,t.name tname
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="map2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<!--这里的name是Teacher类中的name-->
<result property="name" column="tname"/>
</association>
</resultMap>
十一、一对多处理
比如:一个老师拥有多个学生!
对于老师而言,就是一对多的关系!
1.实体类的搭建
@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;
}
2.按照结果嵌套处理☆
//接口中
List<Teacher> getTeacher(@Param("tid") int 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="dao.TeacherMapper">
<select id="getTeacher" resultMap="map">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid = t.id and t.id = #{tid};
</select>
<resultMap id="map" type="Teacher">
<result property="name" column="tname"/>
<!--复杂的属性,我们需要单独处理 对象: association 集合: collection
javaType="" 指定属性的类型!
集合中的泛型信息,我们使用ofType获取
-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
</collection>
</resultMap>
</mapper>
//测试类
@Test
public void testGetTeacer(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
List<Teacher> teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
3.按照查询嵌套
<select id="getTeacher2" resultMap="map2">
select * from mybatis.teacher where id = #{tid};
</select>
<resultMap id="map2" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getTeacherByStudentId" column="id"/>
</resultMap>
<select id="getTeacherByStudentId" resultType="Teacher">
select * from mybatis.student where tid = #{id};
</select>
4.小结
- 关联 - association 【多对一】
- 集合 - collection 【一对多】
- javaType & ofType
- JavaType 用来指定实体类中属性的类型
- ofType 用来指定映射到List或者集合中的 pojo类型,泛型中的约束类型!
注意点:
- 保证SQL的可读性,尽量保证通俗易懂
- 注意一对多和多对一中,属性名和字段的问题!
- 如果问题不好排查错误,可以使用日志 , 建议使用 Log4j
慢SQL 1s 1000s
面试高频
-
Mysql引擎
-
InnoDB底层原理
-
索引
-
索引优化!
- ofType 用来指定映射到List或者集合中的 pojo类型,泛型中的约束类型!
注意点:
- 保证SQL的可读性,尽量保证通俗易懂
- 注意一对多和多对一中,属性名和字段的问题!
- 如果问题不好排查错误,可以使用日志 , 建议使用 Log4j
慢SQL 1s 1000s
面试高频
- Mysql引擎
- InnoDB底层原理
- 索引
- 索引优化!