Mybatis 使用注解开发,以及处理一对多和多对一的联合表的方式
8、使用注解开发
8.1、面向接口编程
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WbadHjqh-1620140259311)(D:\ADeskTop\大三下学期\JAVA\SSM\Mybatis.assets\image-20210429160939355.png)]](https://i-blog.csdnimg.cn/blog_migrate/7f96764edab9f624d4d599943d292534.png#pic_center)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DKqWFOJV-1620140259313)(D:\ADeskTop\大三下学期\JAVA\SSM\Mybatis.assets\image-20210429161135348.png)]](https://i-blog.csdnimg.cn/blog_migrate/f1c477f3105e22d0e39d7b27cea1c77f.png#pic_center)
8.2、使用接口开发
1、 注解在接口上实现
@Select("select * from user")
List<User> getUsers();
@Select("select *from user where id=#{id}")
User getUserbyId(@Param("id") int id2); //根据Param中的“id”来取 多参数:@Param("id") int id2。@Param("name") String name
@Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
void addUser(User user);//引用类型就不需要加@Param
@Update("update user set name=#{name},pwd=#{password} where id=#{id}" )
int updateUser(User user);
@Delete("delete from user where id=#{id}")
int deleteUser(@Param("id") int id);
这种方法不需要取写一个xml文件 但是不能执行太复杂的sql语句,适合简单的sql语句
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-54TthKx3-1620140259316)(D:\ADeskTop\大三下学期\JAVA\SSM\Mybatis.assets\image-20210502140843148.png)]](https://i-blog.csdnimg.cn/blog_migrate/b69ad1825c1c899adfb4833e3124c429.png#pic_center)
2、在mybatis-config.xml上注册
<!-- 绑定接口,因为用注解,所以直接绑定类-->
<mappers>
<mapper class="ybl.dao.UserMapper"/>
</mappers>
3、测试
本质:反射机制实现
底层:动态代理
10、多对一处理
1、按照查询嵌套语句
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ybl.dao.StudentMapper">
<select id="getStudent" resultMap="StudentTeacher">
select * from student;
</select>
<resultMap id="StudentTeacher" type="Student">
<result column="id" property="id"/>
<result column="name" property="name"/>
<!--
复杂的属性我们需要单独处理
对象:association
集合:collection
-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher1"/>
</resultMap>
<select id="getTeacher1" resultType="Teacher">
select *
from teacher where id=#{id};
</select>
</mapper>
值得注意的是getTeacher1 并没有写在StudentMapper或者TeacherMapper当中,他只写在该xml中,然后select="getTeacher1"调用。 javaType=“Teacher” 表示返回的是Teacher类型(别名)的。
2、按照结果嵌套查询
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.id ttid,t.name tname from
student s,teacher t where s.tid=t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result column="sid" property="id"/>
<result column="sname" property="name"/>
<association property="teacher" javaType="Teacher">
<!-- 每个内容都要提到 否则就会显示为默认值,而且要起一个别名 tid tname 否则就GG-->
<result property="id" column="ttid"/> 这里一定要在上面启一个 别名 t.id ttid 否则就会显示错误
<result property="name" column="tname"/>
</association>
</resultMap>
11、一对多处理
一个老师有多个学生
mybatis-07
public class Student {
private int id;
private String name;
//学生需要关联一个老师
private int tid;
}
public class Teacher {
private int id;
private String name;
private List<Student> students;
}
public interface TeacherMapper {
List<Teacher> getTeacher(@Param("tid") int id );
}
1、按照结果嵌套查询
<!--按结果嵌套查询-->
<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 column="tid" property="id"/>
<result column="tname" property="name"/>
<!-- 复杂的属性我们需要单独处理
对象:association
集合:collection 集合中的泛型信息我们需要ofType获取-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
2、按照查询嵌套
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VH3fiVLG-1620140259320)(D:\ADeskTop\大三下学期\JAVA\SSM\Mybatis.assets\image-20210502173300964.png)]](https://i-blog.csdnimg.cn/blog_migrate/720787d9363ca4733324f215f33f1a65.png#pic_center)
小节
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fI4kMUac-1620140259321)(D:\ADeskTop\大三下学期\JAVA\SSM\Mybatis.assets\image-20210502174508237.png)]](https://i-blog.csdnimg.cn/blog_migrate/c8d857e782201bcf55a827289767fed8.png#pic_center)
面试高频:
- Mysql引擎
- InnoDB底层原理
- 索引
- 索引优化!
本文介绍了Mybatis中使用注解进行接口编程的方法,包括面向接口的CRUD操作,并详细讲解了多对一和一对多关系的处理,涉及嵌套查询和结果映射。涵盖了`@Param`注解的应用、配置注册、以及复杂关系的映射技巧。

2446

被折叠的 条评论
为什么被折叠?



