1. 核⼼配置⽂件SqlMapConfig.xml

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

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/21859640/1635172860486-3c4c2f6c-1988-4929-b2a1-cf0184d5b4e9.png#clientId=uc960a666-bb2a-4&from=paste&height=287&id=u261ebf17&margin=%5Bobject%20Object%5D&name=image.png&originHeight=287&originWidth=457&originalType=binary&ratio=1&size=33110&status=done&style=none&taskId=u688ce103-f638-46c5-9f23-f817f0b2f99&width=457)

2. MyBatis常⽤配置解析

2.1 environments标签

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

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

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

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

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

2.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"/>

2.3 Properties标签

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

2.4 typeAliases标签

类型别名是为Java 类型设置⼀个短的名字。原来的类型名称配置如下
image.png
配置typeAliases,为com.lagou.domain.User定义别名为user
image.png

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

3. 映射配置⽂件mapper.xml

3.1 动态sql语句

动态sql语句概述
Mybatis 的映射⽂件中,前⾯我们的 SQL 都是⽐较简单的,有些时候业务逻辑复杂时,我们的 SQL是动态变化
的,此时在前⾯的学习中我们的 SQL 就不能满⾜要求了。
参考的官⽅⽂档,描述如下:
image.png

3.1.1 动态 SQL 之

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

<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>
  • 当查询条件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);
    … … …
    

    image.png

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

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

    image.png

3.1.2 动态 SQL 之

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

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

测试代码⽚段如下:

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

image.png

foreach标签的属性含义如下:
标签⽤于遍历集合,它的属性:

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

3.2 SQL⽚段抽取

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

<!--抽取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>