深入浅出Mybatis(一):映射器

版权声明:本文为许晨原创文章,未经本人允许不得转载。 https://blog.csdn.net/Mrxuchen/article/details/82798115

映射器是Mybatis最复杂且最重要的组件,它有一个接口加上xml文件(或者注解)组成,在映射器中可以配置参数、各类的SQL语句、存储过程、缓存、级联等复杂的内容,并且通过简易的映射规则映射到指定的pojo或者其他对象上,映射器能有效消除JDBC底层的代码。

在Mybatis应用程序开发中,映射器的开发工作量占全部工作量的80%。在MyBatis中映射器的配置顶级元素不多,但是里面的一些细节,比如缓存、级联、#和$字符的替换、参数、存储过程、映射规则需要我们进一步学习。

目前Mybatis映射器有两种方式:

  • xml映射器
    xml映射器是MyBatis原生支持的方式,功能非常强大。
  • 接口映射器

定义xml映射器

xml映射器支持将SQL语句编写在xml格式的文件中。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.chench.test.mybatis.mapper">
    <select id="selectOneTest" resultType="org.chench.test.mybatis.model.Test">
        select * from test where id = #{id}
    </select>
</mapper>

配置xml映射器

对于MyBatis是独立使用还是与Spring框架集成这2种不同的场景,可以使用2种可选的方式注册xml映射器。

独立使用MyBatis
独立使用时注册xml映射器只能在MyBatis配置文件中(如:mybatis-config.xml)通过mapper节点实现。

<configuration>
    <mappers>
        <!-- 注册xml映射器: 2种方式 -->
        <!-- 方式一: 使用相对于类路径的资源引用 -->
        <mapper resource="org/chench/test/mybatis/mapper/xml/TestMapper.xml"/>

        <!-- 方式二: 使用完全限定资源定位符(URL) -->
        <!--<mapper url="file:///var/config/TestMapper.xml" />-->
    </mappers>
</configuration>

在Spring框架中集成MyBatis
在Spring框架中集成MyBatis时,注册xml映射器有2种可选的方式:既可以在MyBatis配置文件中(如:mybatis-config.xml)配置,也可以直接在SqlSessionFactoryBean中通过属性mapperLocations进行注册。
(1)将xml映射器注册放在MyBatis配置文件中(如:mybatis-config.xml),但是此时必须在SqlSessionFactoryBean中通过属性configLocation指定MyBatis配置文件的位置。

<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!-- 指定MyBatis配置文件(只支持类路径,典型的值为"WEB-INF/mybatis-configuration.xml") -->
    <property name="configLocation" value="mybatis-config.xml"/>
</bean>
(2)在SqlSessionFactoryBean中通过属性mapperLocations进行注册xml映射器。

<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!-- 注册xml映射器 -->
    <property name="mapperLocations" value="classpath*:org/chench/test/mybatis/mapper/xml/**/*.xml"/>
</bean>

使用xml映射器

对于xml映射器的使用方式,如果使用SqlSession进行调用,独立使用或者在Spring框架中集成基本上是一致的。需要注意的是:当MyBatis在Spring框架中集成使用时,不需要直接从sqlSessionFactory中获取sqlSession对象,而是可以使用spring管理的sqlSession对象。另外当在Spring框架中集成MyBatis时,还可以直接通过接口使用xml映射器。

独立使用MyBatis
独立使用MyBatis时,对于xml映射器只能使用SqlSession进行调用。

// 从类路径下的xml配置中构建SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
// 从sqlSessionFactory中获取sqlSession
// SqlSession的作用域最好是请求或方法域,且在使用完毕之后及时释放资源,而且一定要确保资源得到释放
SqlSession sqlSession = sqlSessionFactory.openSession();
// 从xml映射配置中查询
Test test = sqlSession.selectOne("org.chench.test.mybatis.mapper.selectOneTest", 1);
sqlSession.close();

在Spring框架中集成MyBatis
(1)使用SqlSession调用xml映射器,方式与独立使用MyBatis时基本一致,只是获取SqlSession实例的方式不同。

// 启动spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("mybatis-spring.xml");
// 使用xml映射器
// 不需要直接从sqlSessionFactory中获取sqlSession对象,而是可以使用spring管理的sqlSession对象
//  SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) context.getBean("sqlSessionFactory");
//  SqlSession sqlSession = sqlSessionFactory.openSession();
//  Test test = sqlSession.selectOne("org.chench.test.mybatis.mapper.selectOneTest", 1);

// 直接使用spring提供的sqlSession
SqlSession sqlSession = (SqlSession) context.getBean("sqlSession");
Test test = sqlSession.selectOne("org.chench.test.mybatis.mapper.selectOneTest", 1);

此时,需要在Spring框架中注入SqlSession实例。

**使用接口调用xml映射器**

当在Spring框架中集成MyBatis时,对于xml映射器的使用除了可以通过SqlSession实例进行调用,还可以直接通过接口进行调用。注意:此时在定义Java接口和注册xml映射器时需要遵循一定的约定。
首先,定义的Java接口必须在org.mybatis.spring.mapper.MapperScannerConfigurer的属性basePackage指定的包或者子包下,如下所示:

org.mybatis.spring.mapper.MapperScannerConfigurer由Spring框架注册,并设置basePackage属性值为org.chench.test.mybatis.mapper.impl,那么对应的Java接口就只能定义在Java包org.chench.test.mybatis.mapper.impl下,并通过Spring框架注册Bean,如下所示:

// Jav接口所在包位置
package org.chench.test.mybatis.mapper.impl;
// 接口通过Spring框架注册Bean
@Repository
public interface DemoMapper {
public Demo selectOne(long id);
}
其次,注册xml映射器时需要将namespace属性设置为上述Java接口的完整类名称,同时设置操作语句元素的id属性为接口内的指定方法名称。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 将xml映射器的namespace属性设置为完整的接口类名称 -->
<mapper namespace="org.chench.test.mybatis.mapper.impl.DemoMapper">
    <!-- 将操作语句元素的id属性设置为接口方法名称 -->
    <select id="selectOne" resultType="org.chench.test.mybatis.model.Demo">
        SELECT * FROM demo WHERE id=#{id}
    </select>
</mapper>

在遵守上述约定注册对应的xml映射器之后,就可以直接通过对应的Java接口调用xml映射器了。

// 启动spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("mybatis-spring.xml");
// 使用接口调用xml映射器
DemoMapper demoMapper = context.getBean(DemoMapper.class);
Demo demo = demoMapper.selectOne(1);

xml映射器使用方法的比较
xml映射器的使用方式根据MyBatis的使用场景而不同,总结如下:
(1)独立使用MyBatis时,只能通过SqlSession使用xml映射器,调用时必须指定xml映射器中的操作语句id,比较繁琐。

Test test = sqlSession.selectOne(“org.chench.test.mybatis.mapper.selectOneTest”, 1);
(2)在Spring框架中集成MyBatis时,使用xml映射器比较灵活。除了可以通过SqlSession使用,还可以通过Java接口直接调用。对于开发者来说,直接调用接口方法会更加简洁;同时还能使用xml映射器的灵活与强大功能,可谓一举多得。

猜你喜欢

转载自blog.csdn.net/Mrxuchen/article/details/82798115
今日推荐