Mybatis 配置文件详解 以及配置文件中的延迟加载

框架是什么:框架就是一个半成品,对于java语言来说,框架就是封装了别人的代码.
框架解决的问题:解决技术整合问题.
什么时候使用框架:企业级大型项目开发.

Mybatis框架
mybatis是一个优秀的持久层框架,他是对jdbc的封装,使得开发者只需要关注Sql语句本身,无需开发者处理加载驱动,获取连接,创建statement等繁琐的过程.
Mybatis最大的特点是把Sql语句写在配置文件中,而且mybatis执行完sql语句之后可以以对象的形式返回pojo对象或者pojo集合.
Mubatis 是一个实现了ORM思想的持久层框架.
ORM:object/Relation Mapping 对象/关系映射

ORM思想:将数据库中的关系数据表映射为JAVA中的对象,把对数据表的操作转换为对对象的操作,实现面向对象编程。因此ORM的目的是使得开发人员以面向对象的思想来操作数据库

SqlMapperCongig.xml

是带DTD约束的xml
根标签 configuration 表示配置
environments 标签表示运行环境,配置的是数据源环境
该标签的属性 default 表示开关的意思,使用的是哪个数据源
比如:< environments default=“development”> 表示开发环境数据源配置
envirment 标签 用来进行数据源环境配置 可以有多个
例如 < envirment id=“development”> 表示开发环境数据源配置 id=“development” 唯一标识 开发环境数据源
< envirment id=“test”> 表示测试环境数据源配置
id=“test” 唯一标识 表示测试环境数据源
< envirment id=“produce”> 表示生产环境数据源
id=“produce” 唯一标识 生产环境数据源

transactionManger 标签配置的是数据库的事务管理 type 属性 type=“JDBC” 表示使用JDBC中的事务管理器 Connection接口中的方法

type=“MANAGERED” mybatis 不去管理事务,交给第三方框架进行管理
Spring 框架进行事务管理 AOP

dataSource 标签 配置的连接池 javax.sql.DataSource接口,是一切连接池的标准接口
他的属性 type=“POOLED” 表示使用连接池
type=“UNPOOLED” 表示连接池交给第三方处理
]

SqlMapConfig.xml 中的其它标签
< mappers> 该标签 用来注册Sql映射文件的
1,resourse属性加载sql映射文件
2,针对Mapper动态代理进行一个增强(增强两种用法)
3.mapper class 单个注册
4. package 批量扫描注册
5. 以上两种方式有规范要求
< mappers>
< mapper resource=“sqlmapper/UserMapper.xml” /> < mapper resource=“mapper/UserMapper.xml” /> < mapper class=“com.itheima.mapper.UserMapper”></ mapper>
< package name=“com.itheima.mapper”>< /package>< /mappers>

全局typeAliases配置
别名 映射的类型

_byte byte

_long long

_short short

_int int

_integer int

_double double

_float float

_boolean boolean

string String

byte Byte

long Long

short Short

int Integer

integer Integer

double Double

float Float

boolean Boolean

date Date

decimal BigDecimal

bigdecimal BigDecimal

map Map

结果集数据类型自定义别名 别名不区分大小写
< typeAliases>

< typeAlias type=“com.itheima.pojo.User” alias=“user”></ typeAlias>
</ typeAliases>

<!-- 使用别名即可-->
<select id="queryUserById" parameterType="int" resultType="User">
   select * from user where id=#{id}
</select>

扫描所有pojo包下的类。注意:不可以出现相同的类名
< typeAliases>


< package name=“com.itheima.pojo” ></ package>
</ typeAliases>

在xxxMapper.xml文件中 在查询结果的字段与pojo中的类字段名不同时,我们就要进行手动映射
< resoultMap >
column 指的是查询结果的列名 property指的是pojo包中的对应javabean类中的属性名
先配置主键 然后再配置其它列
< id column= property= >
<resoult column= property= ></ resoult>
一对多配置
在一个JavaBean类中含有另一个JavaBean类作为其属性,查询的结果是集合
property 配置属性名 oftype 配置 集合的泛型
< collection property="" oftype=>
先配置主键
< id column= property= ></ id>
然后再配置其它字段
< result column= property= />
</ resoultMap>

如果查询的结果不是集合 则使用
property 配置属性名 Javatype 配置该属性的类型
< association property="" javatype= “”>
先配置主键
< id column= property= ></ id>
然后再配置其它字段
< result column= property= />

延迟加载
在sqlmapconfig.xml中配置setting标签
< settings>

< setting name=“lazyLoadingEnabled” value=“true”/>

< setting name=“aggressiveLazyLoading” value=“false”/>

< setting name=“lazyLoadTriggerMethods” value=“true”/>
</ settings>
原先的sql语句需要分开写
比如
延迟加载前:
< select id=“queryUserVoList” resultMap=“resoult”>
select u.id uid,o.number,o.createtime,o.note,u.username,u.address ,u.birthday,u.sex from user u left join orders o on u.id=o.user_id
</ select>
< resultMap id=“resoult” type=“Uservo”>
< id column=“uid” property=“id”></ id>
< result column=“username” property=“username”></ result>
< result column=“sex” property=“sex”></ result>
< result column=“birthday” property=“birthday”></ result>
< result column=“address” property=“address”></ result>
< collection property=“orders” ofType=“orders” >
< result column=“createtime” property=“createtime”></ result>
< result column=“number” property=“number”></ result>
< result column=“note” property=“note”></ result>
</ collection>
</ resultMap>

延迟加载
< select id=“queryOrderByUser” resultMap=“resoult” >
select u.id uid,u.username,u.address ,u.birthday,u.sex from user u
< /select>
< resultMap id=“resoult” type=“Uservo”>
< id column=“uid” property=“id”></ id>
< result column=“username” property=“username”></ result>
< result column=“sex” property=“sex”></ result>
< result column=“birthday” property=“birthday”></ result>
< result column=“address” property=“address”></ result>
column 配置的是第二次查询需要用到的字段
< collection property=“orders” column=“uid” select=“queryOrderByUseryanchi”>

</ collection>
</ resultMap>
< select id=“queryOrderByUseryanchi” resultType=“orders”>
select o.number,o.createtime,o.note from orders o where user_id=#{uid}
</ select>

mybaits的一级缓存与二级缓存
一级缓存
一级缓存:是SqlSession级别的,也就是同一个SqlSession内执行相同select语句的时候,不再去查询数据库,而是从Mybatis内部的缓存内存结构去直接拿到数据。

  • 缓存失效时机:
    • sqlSession关闭
    • sqlSession提交事务(意味着可能是一个增删改的动作,需要更新缓存,那么这个时候Mybatis就会把已有的一级缓存给清理掉)

二级缓存
首先在sqlmapconfig.xml 中的setting标签中添加
< setting name=“cacheEnabled” value=“true” />
然后 具体Mapper.xml配置
< cache></ cache>
两次都会去查询二级缓存,但是第一次的时候缓存中没有数据,第二次才有,所以命中率是0.5

  • 注意:Mybatis的二级缓存在使用的时候有很多限制,在实际开发的时候Mybatis的二级缓存应用很少
    比如在其他mapper.xml文件中有对user表的更新,那么UserMapper.xml的二级缓存就可能错误,我们很难要求对一张表的操作全部放到同一个mapper.xml中。
    在增删改标签中会有 flushcatch属性 将值设置为true 表示刷新二级缓存
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值