1. 查看不同MyBatis版本整合Spring时使用的适配包;
    http://www.mybatis.org/spring/
  2. 下载整合适配包
    https://github.com/mybatis/spring/releases | MyBatis-Spring | MyBatis | Spring | | —- | —- | —- | | 1.0.0 and 1.0.1 | 3.0.1 to 3.0.5 | 3.0.0 or higher | | 1.0.2 | 3.0.6 | 3.0.0 or higher | | 1.1.0 or higher | 3.1.0 or higher | 3.0.0 or higher | | 1.3.0 or higher | 3.4.0 or higher | 3.0.0 or higher |

  3. 官方整合示例,jpetstore
    https://github.com/mybatis/jpetstore-6

maven

  1. <dependencies>
  2. <!--单元测试-->
  3. <!-- https://mvnrepository.com/artifact/junit/junit -->
  4. <dependency>
  5. <groupId>junit</groupId>
  6. <artifactId>junit</artifactId>
  7. <version>4.12</version>
  8. <scope>test</scope>
  9. </dependency>
  10. <!--logging-->
  11. <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  12. <dependency>
  13. <groupId>commons-logging</groupId>
  14. <artifactId>commons-logging</artifactId>
  15. <version>1.1.1</version>
  16. </dependency>
  17. <!--spring-ioc-->
  18. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-context</artifactId>
  22. <version>5.2.8.RELEASE</version>
  23. </dependency>
  24. <!--spring-jdbc-->
  25. <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  26. <dependency>
  27. <groupId>org.springframework</groupId>
  28. <artifactId>spring-jdbc</artifactId>
  29. <version>5.2.8.RELEASE</version>
  30. </dependency>
  31. <!--spring-webmvc-->
  32. <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-webmvc</artifactId>
  36. <version>5.2.8.RELEASE</version>
  37. </dependency>
  38. <!--spring-aop-->
  39. <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-aop</artifactId>
  43. <version>5.2.8.RELEASE</version>
  44. </dependency>
  45. <!--jstl-->
  46. <!--在tomcat中加入四个jar-->
  47. <!--mybatis-->
  48. <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
  49. <dependency>
  50. <groupId>org.mybatis</groupId>
  51. <artifactId>mybatis</artifactId>
  52. <version>3.5.3</version>
  53. </dependency>
  54. <!--mysql-connector-java-->
  55. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  56. <dependency>
  57. <groupId>mysql</groupId>
  58. <artifactId>mysql-connector-java</artifactId>
  59. <version>8.0.16</version>
  60. </dependency>
  61. <!--mybatis-spring-->
  62. <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
  63. <dependency>
  64. <groupId>org.mybatis</groupId>
  65. <artifactId>mybatis-spring</artifactId>
  66. <version>2.0.5</version>
  67. </dependency>
  68. <!--c3p0数据池-->
  69. <dependency>
  70. <groupId>com.mchange</groupId>
  71. <artifactId>c3p0</artifactId>
  72. <version>0.9.5.2</version>
  73. </dependency>
  74. </dependencies>

整合关键配置

<bean id="sqlSessionFactory" class="com.mybatis.spring.SqlSessionFactoryBean">
    <!--指定mybatis全局配置文件位置-->
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
    <!--指定数据源-->
    <property name="dataSource" ref="dataSource"></property>
    <!--mapperLocations:映射所有sql映射文件所在位置-->
    <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
</bean>

<!--自定的扫描所有的mapper的实现并加入到ioc容器中-->
<bean id="configure" class="org.mybatsi.spring.mapper.MapperScannerConfigurer">
    <!--basePackage:指定包下所有的mapper接口实现自动扫描并加入到ioc容器中-->
</bean>

Spring-MVC

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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 http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--mvc配置文件-->
    <!--SpringMVC只是控制网站跳转逻辑的-->

    <!--只扫描控制器-->
    <context:component-scan base-package="com.zh.mybatis" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--开挂的功能-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:default-servlet-handler/>
</beans>

Spring配置文件

<?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" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

    <!--spring配置文件-->
    <!--spring希望管理所有的业务逻辑组件,等。。。-->
    <context:component-scan base-package="com.zh.mybatis">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!--引入数据库配置文件-->
    <context:property-placeholder location="classpath:dbConfig.properties"/>


    <!--Spring用来控制业务逻辑,数据源、事务控制、aop-->

    <!--c3p0数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!-- spring事务管理 -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 开启基于注解的事务 -->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

    <!--
        整合mybatis
            目的:
                1. spring管理所有的组件。mapper的实现类。
                    service==>Dao   @Autowired:自动注入mapper
                2. spring用来管理事务,spring声明式事务
    -->
    <!--创建出SqlSessionFactory对象-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- configLocation指定全局配置文件的位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--mapperLocations: 指定mapper文件的位置-->
        <property name="mapperLocations" value="classpath:mybatis.mapper/*.xml"></property>
    </bean>

    <!--
        扫描所有的mapper接口的实现,让这些mapper能够自动注入;
        base-package:指定mapper接口的包名
    -->
    <!--<mybatis-spring:scan base-package="com.zh.mybatis.dao"/>-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zh.mybatis.dao"/>
    </bean>

</beans>

MyBatis-Config

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <!--开启驼峰命名转换-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--没有为参数指定特定的 JDBC 类型时,空值的默认 JDBC 类型-->
        <setting name="jdbcTypeForNull" value="NULL"/>

        <!--显式的指定每个我们需要更改的的配置的值,即使它是默认的。防止版本更新带来的问题-->
        <!--开启全局二级缓存配置-->
        <setting name="cacheEnabled" value="true"/>
        <!--延迟加载的全局开关-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--开启时,任一方法的调用都会加载该对象的所有延迟加载属性。 否则,每个延迟加载属性会按需加载-->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>


    <databaseIdProvider type="DB_VENDOR">
        <!--为不同的数据库厂商起别名-->
        <property name="MySQL" value="mysql"/>
        <property name="Oracle" value="oracle"/>
        <property name="SQL Server" value="sqlServer"/>
    </databaseIdProvider>
</configuration>