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

映射器是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映射器的靈活與強大功能,可謂一舉多得。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章