mybatis学习——(5)Lombok、一对多、多对一

tech2026-04-14  2

mybatis学习——(5)

目录

mybatis学习——(5)

1、Lombok

2、多对一处理

  ​

2.1、测试环境搭建

2.2、按照查询嵌套处理

2.3、按照结果嵌套处理

 3、一对多处理

 ​

3.1、按照查询嵌套处理

3.2、按照结果嵌套处理


1、Lombok

使用步骤:

1、在IDEA中安装Lombok插件!

2、在项目中导入Lombok的jar包

3、在实体类上添加注解

@Getter and @Setter @FieldNameConstants @ToString @EqualsAndHashCode @AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor @Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger, @CustomLog @Data @Builder @SuperBuilder @Singular @Delegate @Value @Accessors @Wither @With @SneakyThrows @val @var experimental @var @UtilityClass

2、多对一处理

  

2.1、测试环境搭建

导入Lombok新建实体类Teacher、Student建立Mapper接口建立Mapper.xml文件在核心配置文件中绑定注册我们的Mapper接口或者文件! <mapper resource="mapper/TeacherMapper.xml"/> //有效 <mapper resource="mapper/*.xml"/> //无效

 

 测试查询是否能够成功!

2.2、按照查询嵌套处理

<resultMap id="Student" 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="getStudent" resultMap="Student"> select * from student </select> <select id="getTeacher" resultType="Teacher"> select * from teacher where id = #{id} </select>

2.3、按照结果嵌套处理

<!--按照结果嵌套--> <select id="getStudent2" resultMap="Student2"> select s.id sid,s.name sname,t.name tname from student s,teacher t where s.tid = t.id </select> <resultMap id="Student2" type="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <association property="teacher" javaType="Teacher"> <result property="name" column="tname"/> </association> </resultMap>

 3、一对多处理

 

实体类 

@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; }

3.1、按照查询嵌套处理

<!--按照查询嵌套处理--> <select id="getTeacher2" resultMap="Teacher2"> select * from mybatis.teacher where id=#{tid} </select> <resultMap id="Teacher2" type="Teacher"> <result property="id" column="id"/> <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/> </resultMap> <select id="getStudentByTeacherId" resultType="Student"> select * from mybatis.student where tid=#{tid}; </select>

3.2、按照结果嵌套处理

<!--按结果嵌套查询--> <select id="getTeacher" resultMap="Teacher"> 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="Teacher" type="Teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> <!--复杂的属性,我们需要单独处理 对象:association 集合:collection javaType="" 指定属性的类型 集合中得泛型信息,我们使用ofType获取 --> <collection property="students" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap>

小结:

1、关联 - association 【多对一】

2、集合 - collection 【一对多】

3、javaType & ofType

javaType 用来指定实体类中属性的类型ofType 用来指定映射到List或者集合中的pojo类型,泛型中的约束类型

注意点:

保证SQL的可读性,尽量保证通俗易懂注意一对多和多对一中,属性名和字段的问题如果问题不好排查错误,可以使用日志,建议使用Log4j

面试高频:

Mysql引擎InnoDB底层原理索引索引优化

 

 

 

 

最新回复(0)