zoukankan      html  css  js  c++  java
  • 04-spring框架—— Spring 集成 MyBatis

      将 MyBatis与 Spring 进行整合,主要解决的问题就是将 SqlSessionFactory 对象交由 Spring
    来管理。所以,该整合,只需要将 SqlSessionFactory 的对象生成器 SqlSessionFactoryBean 注
    册在 Spring 容器中,再将其注入给 Dao 的实现类即可完成整合。

      实现 Spring 与 MyBatis 的整合常用的方式:扫描的 Mapper 动态代理
      Spring 像插线板一样,mybatis 框架是插头,可以容易的组合到一起。插线板 spring 插
    上 mybatis,两个框架就是一个整体。

    4.1.1 MySQL  创建数据库 springdb, 新建表 Student

    4.1.2 maven  依赖 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.bjpowernode</groupId>
      <artifactId>ch09-spring-mybatis</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <name>ch09-spring-mybatis</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
        <!--spring-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.3.16.RELEASE</version>
        </dependency>
        <!--spring的事务-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>4.3.16.RELEASE</version>
        </dependency>
        <!--spring访问数据库-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>4.3.16.RELEASE</version>
        </dependency>
        <!--mybatis的依赖-->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.5</version>
        </dependency>
        <!--mybatis整合spring的依赖:创建mybatis对象-->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.3.1</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.9</version>
        </dependency>
        <!--数据库连接池-->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.12</version>
        </dependency>
      </dependencies>
    
      <build>
        <resources>
          <resource>
            <directory>src/main/java</directory><!--所在的目录-->
            <includes><!--包括目录下的.properties,.xml文件都会扫描到-->
              <include>**/*.properties</include>
              <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

    4.1.3  定义实体类 Student

    4.1.4  定义 StudentDao 

    4.1.5  定义映射文件 mapper

      在 Dao 接口的包中创建 MyBatis 的映射文件 mapper,命名与接口名相同,本例为
    StudentDao.xml。mapper 中的 namespace 取值也为 Dao 接口的全限定性名。

    4.1.6  定义 Service 

    接口定义:

    实现类定义:

    4.1.7  定义 MyBatis 

    在 src 下定义 MyBatis 的主配置文件,命名为 mybatis.xml。
    这里有两点需要注意:

    (1)主配置文件中不再需要数据源的配置了。因为数据源要交给 Spring 容器来管理了。
    (2)这里对 mapper 映射文件的注册,使用<package/>标签,即只需给出 mapper 映射文件
    所在的包即可。因为 mapper 的名称与 Dao 接口名相同,可以使用这种简单注册方式。这种
    方式的好处是,若有多个映射文件,这里的配置也是不用改变的。当然,也可使用原来的
    <resource/>标签方式。

    4.1.8  修改 Spring  配置文件

    (1 )  数据源的配置(掌握)

      使用 JDBC 模板,首先需要配置好数据源,数据源直接以 Bean 的形式配置在 Spring 配
    置文件中。根据数据源的不同,其配置方式不同:

    Druid  数据源 DruidDataSource

      Druid 是阿里的开源数据库连接池。是 Java 语言中最好的数据库连接池。Druid 能
    够提供强大的监控和扩展功能。Druid 与其他数据库连接池的最大区别是提供数据库的

    官网:https://github.com/alibaba/druid
    使用地址:https://github.com/alibaba/druid/wiki/常见问题

    配置连接池:

    Spring 配置文件:

    (2 )  从属性文件读取数据库连接信息

      为了便于维护,可以将数据库连接信息写入到属性文件中,使 Spring 配置文件从中读取
    数据。

      属性文件名称自定义,但一般都是放在 src 下。

      Spring 配置文件从属性文件中读取数据时,需要在<property/>的 value 属性中使用${ },
    将在属性文件中定义的 key 括起来,以引用指定属性的值。

      该属性文件若要被 Spring 配置文件读取,其必须在配置文件中进行注册。使用<context>
    标签。

    <context:property-placeholder/> 方式(掌握)

    该方式要求在 Spring 配置文件头部加入 spring-context.xsd 约束文件

    <context:property-placeholder/>标签中有一个属性 location,用于指定属性文件的位置。

    (3 )  注册 SqlSessionFactoryBean

    (4 )  定义 Mapper  扫描配置器 MapperScannerConfigurer

    Mapper 扫描配置器 MapperScannerConfigurer 会自动生成指定的基本包中 mapper 的代

    理对象。该 Bean 无需设置 id 属性。basePackage 使用分号或逗号设置多个包。

    4.1.9 向 向 Service 

      向 Service 注入 Mapper 代理对象时需要注意,由于通过 Mapper 扫描配置器
    MapperScannerConfigurer 生成的 Mapper 代理对象没有名称,所以在向 Service 注入 Mapper
    代理时,无法通过名称注入。但可通过接口的简单类名注入,因为生成的是这个 Dao 接口
    的对象。

    4.1.10 Spring 配置文件的全部配置

  • 相关阅读:
    ABCD 谁是小偷
    三剑客-awk(简写)
    三剑客-sed(简写)
    Mysql锁
    MySQL两种内核对比
    netty源码分析(一)
    netty源码分析(二)
    网络编程之NIO
    网络编程之ByteBuffer
    线程池的原码分析(二)
  • 原文地址:https://www.cnblogs.com/Tpf386/p/10990882.html
Copyright © 2011-2022 走看看