Spring-04 (Spring integrates MyBatis)

Spring integrates MyBatis

Personal blog : www.xiaobeigua.icu

Integrating MyBatis with Spring, the main problem solved is that the SqlSessionFactory object is managed by Spring. Therefore, for this integration, you only need to register SqlSessionFactoryBean, the object generator of SqlSessionFactory, in the Spring container, and then inject it into Dao's implementation class to complete the integration.

A common way to achieve the integration of Spring and MyBatis: Scanning Mapper dynamic proxy

Spring is like a patch panel, and the mybatis frame is a plug that can be easily assembled together. Plug the spring of the power strip into mybatis, and the two frames are a whole.


4.1.1 MySQL Create database springdb, create new table Student

4.1.2maven depends on pom.xml

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency> <dependency>   <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.5.RELEASE</version> </dependency>   <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <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>       插件: <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>   

4.1.3 Define the entity class Student

​​​​​​​4.1.4 Define StudentDao interface

4.1.5 Define the mapping file mapper

Create the MyBatis mapping file mapper in the Dao interface package. The name is the same as the interface name. This example is

StudentDao.xml. The namespace value in the mapper is also the fully qualified name of the Dao interface.

4.1.6 Define Service interface and implementation class

Interface definition:

Implementation class definition:

4.1.7 Define MyBatis main configuration file

Define the main configuration file of MyBatis under src and name it mybatis.xml. There are two points to note here:

  1. The configuration of the data source is no longer required in the main configuration file. Because the data source has to be managed by the Spring container.
  2. For the registration of the mapper mapping file, use the <package/> tag, that is, you only need to give the package where the mapper mapping file is located. Because the name of the mapper is the same as the name of the Dao interface, this simple registration method can be used. The advantage of this method is that if there are multiple mapping files, the configuration here does not need to be changed. Of course, you can also use the original

<resource/> tag method.

4.1.8 Modify Spring configuration file

(1) Configuration of data source ( master )

To use the JDBC template, you first need to configure the data source, which is directly configured in the Spring configuration file in the form of a bean. Depending on the data source, the configuration method is different:

Druid data source DruidDataSource

Druid is Ali's open source database connection pool. It is the best database connection pool in the Java language. Druid can provide powerful monitoring and extended functions. The biggest difference between Druid and other database connection pools is the provision of databases

Official website: https://github.com/alibaba/druid

Use address: https://github.com/alibaba/druid/wiki/FAQ Configure connection pool:

Spring configuration file:

(2) Read the database connection information from the properties file

In order to facilitate maintenance, you can write the database connection information into the properties file so that the Spring configuration file can read data from it.

The name of the property file is customized, but it is generally placed under src.

When the Spring configuration file reads data from the property file, it is necessary to use ${} in the value attribute of <property/> to enclose the key defined in the property file to reference the value of the specified property.

For this property file to be read by the Spring configuration file, it must be registered in the configuration file. Use the <context> tag.

<context:property-placeholder/> method ( master )

This method requires the spring-context.xsd constraint file to be added to the head of the Spring configuration file

There is a property location in the <context:property-placeholder/> tag, which is used to specify the location of the property file.

(3) Register SqlSessionFactoryBean

(4) defines Mapper scan configurator MapperScannerConfigurer

Mapper Scanner Configurator MapperScannerConfigurer will automatically generate the proxy object of the mapper in the specified basic package. The bean does not need to set the id attribute. basePackage uses semicolons or commas to set multiple packages.

4.1.9 Injecting the interface name into the Service

When injecting Mapper proxy objects into the Service, you need to pay attention. Since the Mapper proxy objects generated by the Mapper ScannerConfigurer have no name, you cannot inject the Mapper proxy by name when injecting the Mapper proxy into the Service. But it can be injected through the simple class name of the interface, because the object of this Dao interface is generated.

​​​​​​​4.1.10   Spring configuration files are all configured