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>