Mybatis核心配置文件介绍

2.1.3Mybatis核心文件分析
①配置文件层级:
Mybatis核心配置文件层级关系
    ·configurtion配置
        ·properties属性
        ·settings设置
        ·typeAliases类型别名
        ·typeHandlers类型处理器
        ·objectFactory对象工厂
        ·plugins 插件
        ·environments环境
           ·environment环境变量
                ·transactionManager事务管理器
                ·DataSource数据源
        ·databaseIdProvider数据库厂商标识
        ·mappers映射器
②Mybatis常用配置解析

1)environments标签

 <environments default="development"> 
    <environment id="development"> 
        <transactionManager type="JDBC"/> 
        <dataSource type="POOLED"> 
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql:///test"/> 
            <property name="username" value="root"/>
            <property name="password" value="root"/> 
        </dataSource> 
 </environment> 
 </environments> 

在这里插入图片描述


① transactionManager事务管理器有两种:

JDBC:这个配置是直接使用JDBC的提交和回滚设置,它依赖于从数据源得到的连接来管理事务作用域;
MANAGED:几乎不做处理,它从来都不提交或回滚一个连接,而是让容器来管理事务的整个生命周期(比如JEE应用服务器的上下文),默认情况它会关闭连接,然⽽⼀些容器并不希望这样,因此需要将 closeConnection 属性设置为 false 来阻⽌它默认的关闭⾏为。

②:数据源(dataSource)有三个类型

•UNPOOLED:这个数据源的实现只是每次被请求时打开和关闭连接。
•POOLED:这种数据源的实现利⽤“池”的概念将 JDBC 连接对象组织起来。
•JNDI:这个数据源的实现是为了能在如 EJB 或应⽤服务器这类容器中使⽤,容器可以集中或在外部配置数据源,然后放置⼀个 JNDI 上下⽂的引⽤。

2)mepper标签

该标签的作用是做加载映射,加载方式有:

•使⽤相对于类路径的资源引⽤,例如:
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
•使⽤完全限定资源定位符(URL),例如:
<mapper url="file:///var/mappers/AuthorMapper.xml"/>
•使⽤映射器接⼝实现类的完全限定类名,例如:
<mapper class="org.mybatis.builder.AuthorMapper"/>
•将包内的映射器接⼝实现全部注册为映射器,例如:
<package name="org.mybatis.builder"/>

3)Properties标签

实际开发中习惯将数据源信息单独抽取成一个properies文件,properties标签可以加载额外的配置文件

<!--加载外部的properties-->
<properties resource = "jdbc.properties"></properties>
<environments default="development"> 
    <environment id="development"> 
        <transactionManager type="JDBC"/> 
        <dataSource type="POOLED"> 
            <property name="driver" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/> 
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/> 
        </dataSource> 
</environment> 
 </environments> 

4)typeAliases标签

类型别名,为java类型设置一个短的名字

<!--配置typeAliases,为com.lagou.domain.User定义别名为user-->
<typeAliases>
    <typeAlias type="com.lagou.domain.User" alias = "user"></typeAlias>
</typeAliases>

<select id =""findAll resultType="user">
    select * from User
</select>
③映射配置文件mapper.xml

1)动态sql之where if标签

<select id="findByCondition" parameterType="user" resultType="user">
    select * from User
    <where>
        <if test="id!=0">
            and id=#{id}
        </if>
        <if test="username!=null">
            and username=#{username}
        </if>
    </where>
</select>

2)动态sql之 foreach 标签

<select id="findByIds" parameterType="list" resultType="user">
    select * from User
    <where>
        <foreach collection="list" open="id in(" close=")" item="id"separator=",">
            #{id}
        </foreach>
    </where>
</select>

foreach(遍历)标签属性含义:

属性含义
collection要遍历的集合元素,注意不需要写#{}
open代表语句的开始部分
close代表结束部分
item集合中单个元素生成的变量名
sperator分隔符

3)sql片段抽取 提取重复的sql,使用include引用

<!--抽取sql⽚段简化编写-->
<sql id="selectUser" select * from User</sql>

<select id="findById" parameterType="int" resultType="user">
    <include refid="selectUser"></include> where id=#{id}
</select> 

<select id="findByIds" parameterType="list" resultType="user">
    <include refid="selectUser"></include>
    <where>
        <foreach collection="array" open="id in(" close=")" item="id"separator=",">
            #{id}
        </foreach>
    </where>
</select>

日拱一卒,功不唐捐;小伙伴们一起学习起来

领取源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员进击中

你的鼓励是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值