04-spring frame - Spring Integration MyBatis

  The MyBatis and Spring integration, the main problem is to solve a SqlSessionFactory handed over to Spring
to manage. Therefore, the integrated, just to the object generator SqlSessionFactoryBean SqlSessionFactory Note
Book Spring container, then injecting it to Dao implementation class to complete the consolidation.

  Spring and MyBatis integrate the usual way: dynamic proxy scanned Mapper
  Spring image as patch panel, the frame MyBatis plugs, can be easily combined together. Plug plate spring inserted
on mybatis, two framework is a whole.

4.1.1 MySQL create database springdb, the new table Student

4.1.2 maven pom.xml dependence

<?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 driver-->
    <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 > 
  </ the Dependencies > 

  < Build > 
    < Resources > 
      < Resource > 
        < Directory > src / main / the Java </ Directory > <-! directory is located -> 
        < Includes > <! - include directory .properties, .xml files will be scanned -> 
          < the include > . ** / * the Properties </ the include > 
          < the 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 class definition entities

 

4.1.4 defined StudentDao 

4.1.5 define the mapping file mapper

  Dao created in the package in an interface mapper MyBatis mapping file of the same name and the name of the interface, in this case
StudentDao.xml. namespace value mapper is also fully qualified name of the interface Dao.

 

4.1.6 Service definitions 

Interface definition:

Implementation class definition:

 

4.1.7 defined MyBatis 

MyBatis master profile defined in src, named mybatis.xml.
There are two points to note:

(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 配置文件的全部配置

 

Guess you like

Origin www.cnblogs.com/Tpf386/p/10990882.html