mybatis由浅入深day02_8spring和mybatis整合

spring和mybatis整合

  8.1 整合思路

需要spring通过单例方式管理SqlSessionFactory、mapper接口。

spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(spring和mybatis整合自动完成)

  8.2 整合环境

创建一个新的java工程(接近实际开发的工程结构)

jar包:

mybatis3.2.7的jar包

  

spring3.2.0的jar包

mybatis和spring的整合包:早期ibatis和spring整合是由spring官方提供,mybatis和spring整合由mybatis提供。

  

全部jar包

  

  

  8.3 Mybatis配置文件

在classpath下创建mybatis/SqlMapConfig.xml

<?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>
    <!-- 别名定义 -->
    <typeAliases>
          <!-- 批量别名定义
              指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名(首字母大写或小写都可以)
           -->
          <package name="cn.itcast.ssm.po"/>
          
    </typeAliases>
    
    <!-- 加载映射文件 -->
    <mappers>
        <mapper resource="sqlmap/User.xml" />
        
        <!-- 批量加载mapper
            指定mapper接口的包名,mybatis自动扫描包下边所有mapper接口进行加载
            遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录中
            上边规范的前提是:使用的是mapper代理方法
         -->
        <package name="cn.itcast.ssm.mapper"/>
    </mappers>
</configuration>

  

  8.4 Spring配置文件:

在classpath下创建applicationContext.xml,定义数据库链接池、SqlSessionFactory。

sqlSessionFactory在mybatis和spring的整合包下。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    
    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 数据源,使用dbcp(数据库连接池) -->    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="10"/>
        <property name="maxIdle" value="5"/>
    </bean>
    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加载mybatis的配置文件 -->
        <property name="configLocation" value="mybatis/SqlMapConfig.xml"></property>
        <!-- 数据源dataSource,因为要与数据库打交道 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

注意:在定义sqlSessionFactory时指定数据源dataSource和mybatis的配置文件。

  8.5 原始dao开发(和spring整合后)

使用此种方法即Dao接口实现类继承SqlSessionDaoSupport,需要编写dao接口,dao接口实现类、映射文件。

     8.5.1 User.xml

  

在SqlMapconfig.xml中加载User.xml

  

    8.5.2 dao接口(实现类继承SqlSessionDaoSupport)

dao接口实现类需要注入SqlSessoinFactory,通过spring进行注入。

这里spring声明配置方式,配置dao的bean:

让UserDaoImpl实现类继承SqlSessionDaoSupport

   

    8.5.3 配置dao

在applicationContext.xml中配置dao。

    8.5.4 测试程序

  

  8.6 mapper代理开发

    8.6.1 mapper.xml和mapper.java

  

      8.6.2 通过MapperFactoryBean创建代理对象

此方法问题:

需要针对每个mapper进行配置,麻烦。

      8.6.3 通过MapperScannerConfigurer进行mapper扫描(建议使用)

  

SqlMapConfig.xml中不需要配置批量加载了

  

  

  8.7 测试代码

原文地址:https://www.cnblogs.com/justdoitba/p/8051712.html