Spring和MyBatis整合

前言:在前面一篇文章中,介绍了单独使用MyBatis连接orace的例子,在这里分享学习下Spring和MyBatis是如何整合的,以具体工程为例子

阅读目录:

  • 1.环境准备
  • 2.搭建工程
    • 2.1.applicationContext.xml
    • 2.2.userMapper.xml
    • 2.3.测试类

MyBatis和Spring整合的官方地址,http://www.mybatis.org/spring/zh/index.html

1.环境准备

   所需环境  Eclipse+maven

   所需jar包    mybatis-3.4.5.jar

                      spring-context-4.3.1.RELEASE.jar spring相关jar包

                     mybatis-spring-1.3.1.jar

2.搭建工程

工程结构如下图所示

2.1 spring框架的配置文件applicationContext.xml

   

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--1 引入属性文件,在配置中占位使用 -->
    <context:property-placeholder location="classpath:db.properties" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="otatransuser" />
        <property name="password" value="${password}" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- sql映射文件路径 -->
          <property name="mapperLocations" value="classpath:mapper/*.xml"></property>  
    </bean>
    
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.ota.mapper.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    
    <!--5 声明式事务管理 -->
    <!--定义事物管理器,由spring管理事务 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

db.properties文件放在类路径下,内容为:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@(DESCRIPTION\=(ADDRESS\=(PROTOCOL\=TCP)(HOST\=10.221.129.208)(PORT\=1523))(CONNECT_DATA\=(SERVICE_NAME\=otatransuser)))
username=otatransuser
password=otatransuser123

要注意:(1)sqlSessionFactory的属性dataSource是必输项,用来指定数据源

                   sqlSessionFactory的属性configLocation,是用来指定 MyBatis 的 XML 配置文件路径的。这个属性是可选项

                  sqlSessionFactory的属性mapperLocations 属性使用一个资源位置的 list。 这个属性可以用来指定 MyBatis 的 XML 映射器文件的位置。比如:

<property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />表示这会从类路径下加载在 sample.config.mappers 包和它的子包中所有的 MyBatis 映射器 XML 文件。

             (2)使用MapperFactoryBean把映射器接口userMapper加入到spring中,如果你有多个映射器接口,需要一一的注入到spring。

                      其实没有必要在spring的xml文件中注册所有的映射器,可以采用这样一种方法,

你可以使用一个 MapperScannerConfigurer , 它 将 会 查 找 类 路 径 下 的 映 射 器 并 自 动 将 它 们 创 建 成 MapperFactoryBean。

要创建 MapperScannerConfigurer,可以在 Spring 的配置中添加如下代码:

<!-- 指定要自动扫描接口的基础包,实现接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ota.mapper" />
    </bean>

注 意 , 没 有 必 要 去 指 定 SqlSessionFactory 或 SqlSessionTemplate , 因 为 MapperScannerConfigurer 将会创建 MapperFactoryBean,之后自动装配

2.2 userMapper.xml文件

userMapper.xml映射器内容如下,user还有userMapper这里也不再一一详细介绍,在前面一篇mybatis入门中有提到

<mapper namespace="com.ota.mapper.UserMapper">
  <select id="findUserByName" parameterType="java.lang.String" resultType="com.ota.pojo.User">
   select * from ota_user where username=#{username}
  </select>
</mapper>

2.3测试类

public class MybatisTest {

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = null;
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = (UserMapper) ctx.getBean("userMapper");
        User user = userMapper.findUserByName("wanghaixia");
        System.out.println(user);
    }

运行OK

           

 

posted @ 2017-10-23 14:20  hxwang  阅读(189)  评论(0编辑  收藏  举报