Mybatis核心!!!映射器,ORM对象关系映射模型

映射器

  • 半自动化的体现:配置 SQL 语句,体现了半自动化和灵活性
  • ORM的体现:对象关系映射的实现,数据库表和 POJO 类的映射关系

映射器的引入、接口

在这里插入图片描述

映射器的组成

在这里插入图片描述

select元素(多参传递)

在这里插入图片描述

转义字符代替

  • Mapper.java

    // 找到学生编号小于某个sid的学生
    public List<Student> selectStuBiggerSid(int sid);
    
  • Mapper.xml

    <select id="selectStuBiggerSid" resultType="student" parameterType="int">
    select * from student where  sid &lt; #{v}
    </select>
    

    select语句中的小于号(<)会被当成是select标签的<,所以select中小于号必须要用转义字符"&lt;"代替,其余不影响

多个参数传递

  1. javabean

    推荐使用JavaBean的方式进行多参传递

  2. Map

    缺点:耦合度高,对其他层有影响,不推荐使用

    在这里插入图片描述

  3. param1 param2 param3 …(新版本):从1开始

    Mapper.java

    // 找到某性别的学生分页
    // param1 param2 param3 ...
    public List<Student> selectStuPageBySex(String sex,int curpage,int sizepage);
    

    Mapper.xml

    <select id="selectStuPageBySex" resultType="student">
    select * from student where ssex=#{param1} limit #{param2},#{param3}
    </select>
    
  4. arg0 arg1 arg2 …(新版本):从0开始

    Mapper.java

    // 找到某性别的学生分页
    // arg0 arg1 arg2 ...
    public List<Student> selectStuPageBySsex(String sex,int curpage,int sizepage);
    

    Mapper.xml

    <select id="selectStuPageBySsex" resultType="student">
    	select * from student where ssex=#{arg0} limit #{arg1},#{arg2}
    </select>
    

    注意:方式3和方式4不能混合使用

insert元素(主键回填)

在这里插入图片描述

  • 主键回填

    当主键在数据库中为自增字段时,新增成功后,回填主键

    insert插入数据时,当事务没提交,主键也被占用掉了,这样会造成根本不能查到本次插入主键,所以必须用主键回填的方式获取获取主键

    <insert id="insertStu" parameterType="Student" keyProperty="sid" useGeneratedKeys="true" >
    insert into student(sname,birthday,ssex,classid) values(#{sname},#{birthday},#{ssex},#{classid})
    </insert>
    

    以上案例将新增后的将主键回填给student的sid属性

update和delete元素

  • update

    在这里插入图片描述

  • delete

    在这里插入图片描述

resultMap元素

在这里插入图片描述

resultMap的作用

当字段和属性名字不相同时,映射不上,导致查询拿不到值

解决方法:1:字段起别名,保持跟属性名一致

2:resultMap

单表可以只写映射不上的

对于主键字段来说可以使用id 也可以使用 result

  1. 定义映射规则

    ORM 的特性,POJO 类和数据库的映射关系

  2. 级联操作

    多表存在主外键关系时,主表和从表之间的关联操作

  3. 类型转换

    数据库字段的类型和 POJO 类属性的类型转换

resultMap元素结构

在这里插入图片描述

<resultMap type="SMaster" id="aaa">
		<id column="smid" property="smid" />
		<result column="sm_name" property="smname" />
		<result column="smsex" property="smsex" />
</resultMap>
	
<select id="selectAll" resultMap="aaa" >
		select 	* from schoolmaster	
</select>

MySQL中的字段名是sm_name,实体类中的属性名是smname

对于主键字段来说可以使用id 也可以使用 result

单表中可以只写映射不上的

多表联查

级联

级联(cascade),是指多个对象之间的映射关系,建立数据之间的级联关系提高管理效率

  • 一对一

    一个对象对应唯一的对象,举例:中国公民和身份证

  • 一对多

    一个对象对应多个对象,举例:班级和学生;

  • 多对多

    多个对象对应多个对象,举例:公司角色和公司员工

一对一

在这里插入图片描述

Mapper.xml

<resultMap type="Student" id="stu_class_Map">
		<result column="sid" property="sid" />
		<result column="sname" property="sname" />
		<result column="birthday" property="birthday" />
		<result column="ssex" property="ssex" />
		<result column="classid" property="classid" />
		
		<association property="bj">
			<result column="classid" property="classid" />
			<result column="classname" property="classname"/>
		</association>
	</resultMap>
	
<select id="findAllStudentAndClass" resultMap="stu_class_Map">
select * from student left join class on student.classid =class.classid
</select>

一对一使用association

一对多

在这里插入图片描述

<resultMap type="Banji" id="banji_class_Map">
	<result column="classid" property="classid" />
	<result column="classname" property="classname" />
		
	<collection property="slist" ofType="Student">
		<result column="sid" property="sid" />
		<result column="sname" property="sname" />
		<result column="birthday" property="birthday" />
		<result column="ssex" property="ssex" />
		<result column="classid" property="classid" />
	</collection>
</resultMap>
	
<select id="findBanjiAndStu" resultMap="banji_class_Map">
select * from student left join class on student.classid =class.classid
</select>

一对多使用collection

级联的缺陷

  • 性能缺陷

    级联操作会降低性能,增加程序的执行时间

  • 复杂度缺陷

    关联较多造成复杂度的增加,不利于他人的理解和维护

使用建议:

  1. 根据实际情况增加级联关系
  2. 多层关联式,建议超过三层关联时尽量少用级联
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeMonkey-D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值