4-Mybatis配置⽂件深⼊

4.1 核⼼配置⽂件SqlMapConfig.xml

4.1.1 MyBatis核⼼配置⽂件层级关系

4.1.2 MyBatis常⽤配置解析

1)environments标签

数据库环境的配置,⽀持多环境配置

其中,事务管理器(transactionManager)类型有两种:

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

其中,数据源(dataSource)类型有三种:

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

2)mapper标签

该标签的作⽤是加载映射的,加载⽅式有如下⼏种:

  • 使⽤相对于类路径的资源引⽤,例如:
    <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标签

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

4)typeAliases标签

类型别名是为Java 类型设置⼀个短的名字。原来的类型名称配置如下:

配置typeAliases,为com.lagou.domain.User定义别名为user

上⾯我们是⾃定义的别名,mybatis框架已经为我们设置好的⼀些常⽤的类型的别名

4.2 映射配置⽂件mapper.xml

4.2.1 动态sql语句

1. 动态sql语句概述

Mybatis 的映射⽂件中,前⾯我们的 SQL 都是⽐较简单的,有些时候业务逻辑复杂时,我们的 SQL是动态变化的,此时在前⾯的学习中我们的 SQL 就不能满⾜要求了。

2. 动态 SQL 之 if 标签

我们根据实体类的不同取值,使⽤不同的 SQL语句来进⾏查询。⽐如在 id如果不为空时可以根据id查询,如果username 不同空时还要加⼊⽤户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。

<!--多条件组合查询:演示if-->
<select id="findByCondition" parameterType="user" resultType="user">
select * from user
<where>
<if test="id !=null">
and id = #{id}
</if>
<if test="username !=null">
and username = #{username}
</if>
</where>
</select>

当查询条件id和username都存在时,控制台打印的sql语句如下:

… … …
//获得MyBatis框架⽣成的UserMapper接⼝的实现类
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User condition = new User();
condition.setId(1);
condition.setUsername("lucy");
User user = userMapper.findByCondition(condition);
… … …

当查询条件只有id存在时,控制台打印的sql语句如下:

… … …
//获得MyBatis框架⽣成的UserMapper接⼝的实现类
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User condition = new User();
condition.setId(1);
User user = userMapper.findByCondition(condition);
… … …

3. 动态 SQL 之 foreach

循环执⾏sql的拼接操作,例如:SELECT * FROM USER WHERE id IN (1,2,5)。

<!--多值查询:演示foreach-->
<select id="findByIds" parameterType="list" resultType="user">
select * from user
<where>
<foreach collection="array" open="id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>

测试代码⽚段如下:

… … …
//获得MyBatis框架⽣成的UserMapper接⼝的实现类
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int[] ids = new int[]{2,5};
List<User> userList = userMapper.findByIds(ids);
System.out.println(userList);
… … …

foreach标签的属性含义如下:

标签⽤于遍历集合,它的属性:

  • collection:代表要遍历的集合元素,注意编写时不要写#{}
  • open:代表语句的开始部分
  • close:代表结束部分
  • item:代表遍历集合的每个元素,⽣成的变量名
  • sperator:代表分隔符

4. SQL⽚段抽取

Sql 中可将重复的 sql 提取出来,使⽤时⽤ include 引⽤即可,最终达到 sql 重⽤的⽬的


<!--抽取sql⽚段简化编写-->
<sql id="selectUser">
select * from user
</sql>
<!--多值查询:演示foreach-->
<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>