Mybatis一站式学习

(1).简介

  • Mybatis处于三层中的dao层,主要用于简化jdbc操作,实现数据的持久化
  • Mybatis是ORM(Object Relational Mapping)的一个实现,主要进行对象和表之间的映射(表名和对象名映射,表字段和对象属性映射),另外类似的还有Hibernate

(2).编程步骤

  • step1.
  • step2.添加Mybatis配置文件
  • step3.写实体类
    注意:实体类的属性名要与表的字段要求一样
  • step4.编写映射文件

(3).基本原理

Mybatis笔记

ResultType和ResultMap

关联查询

1.一对一关联查询

  • 业务扩展类(一般一脚鸡肋,适合于小型的)

-----即重新建一个类,它包含需要查询的两个泪的全部属性,然后返回ResultType类型就是这个新类(通常为了简化业务,可以直接用一个类继承一个属性多的,重写属性少的)

  • ResultMap(常用)
    -----返回类型为ResultMap(例如学生类和学生证类,可以将学生郑类作为学生的一个属性)
    在这里插入图片描述
    在这里插入图片描述

一对多关联查询

注意:在Mybatis中,多对多关联可以看成两个一对多的关联

  • 表与对象之间的关系
    在这里插入图片描述

  • 映射文件的编写在这里插入图片描述

  • 在这里插入图片描述

Mybatis整合Log4j、延迟加载

日志

  • 配置文件的编写
<configuration>
	
	<properties  resource="db.properties"/>

	<settings>
		<!-- 开启日志,并指定使用的具体日志 -->
		<setting name="logImpl" value="LOG4J"/>
	</settings>
</configuration>
	

在这里插入图片描述

  • 可以通过日志信息,详细的阅读Mybatis的执行情况(观察Mybatis执行的SQl语句、以及SQL中的参数和返回的如果)

延迟加载

  • 配置文件
<configuration>
	<properties  resource="db.properties"/>

	<settings>
		<!-- 开启日志,并指定使用的具体日志 -->
		<setting name="logImpl" value="LOG4J"/>
		
		<!-- 开启延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true"/>
		
		<!-- 关闭立即加载 -->
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>
	<!-- 
	
	<configuration>
 	-->
  • 映射文件(两个)
<?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">

<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.lanqiao.mapper.StudentClassMapper">
  
	  	<!-- 一对多,带延迟加载 -->
	<select id="queryClassAndStudents"   resultMap="class_student_lazyLoad_map">
		<!-- 11111111先查询班级 -->
		select  c.* from studentclass c
	</select>
	 
	<!---表的对应关系 -->
	<resultMap type="studentClass" id="class_student_lazyLoad_map">
			<!-- 因为 type的主类是班级,因此先配置班级的信息-->
			<id  property="classId" column="classId"/>
			<result  property="className" column="className"/>
			<!-- 配置成员属性学生,一对多;属性类型:javaType,属性的元素类型ofType -->
			<!-- 2222222再查班级对应的学生 -->
			<collection property="students" ofType="student" select="org.lanqiao.mapper.StudentMapper.queryStudentsByClassId" column="classid">
				<!-- <id  property="stuNo" column="stuNo"/>
				<result  property="stuName" column="stuName"/>
				<result  property="stuAge" column="stuAge"/>
				 -->
			</collection>
	</resultMap>
	
	
</mapper>

第二个映射文件


<?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">

<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.lanqiao.mapper.StudentMapper">
<!-- 一对多,延迟加载需要的: 查询班级中的所有学生 -->
	<select id="queryStudentsByClassId" parameterType="int" resultType="student">
		select * from student where classId = #{
    
    classId}
	</select>
	<mapper>

查询缓存

-逻辑
在这里插入图片描述

  • 作用:减少访问数据库的次数,提高性能

一级缓存

范围:同一个SqlSession对象
Mybatis默认开启了一级缓存,如果用同样的SqlSession查询相同的数据,则只会在第一次查询时向数据库发送sql语句,并将查询结果放入到SQLSESSION中(作为缓存存在);后续再次查询同样的对象时,则直接从缓存中查询该对象即可(忽略掉了数据库的访问)

二级缓存

Mybatis自带二级缓:【同一个namespace】生成的mapper对象
注意:namespace的值就是接口的全类名(包名.类名),通过接口可以产生代理对象

Mybatis的逆向操作

(后续有时间了再整理更新)

扫描二维码关注公众号,回复: 13295569 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_43050077/article/details/103568370