[Spring] Spring integrates MyBatis

Keywords: Java Spring

Integration ideas

Spring can integrate many frameworks, which is an advantageous function of spring.
Through the integration function, it is more convenient for developers to use other frameworks.
The integration uses Spring IoC core technology.

To use a framework, such as mybatis, how do I use mybatis?

The purpose of Spring's integration with mybatis 👉 The objects used in the mybatis framework are managed by Spring, mainly SqlSessionFactory and dao proxy, which are handed over to the container Spring. Declare objects in the Spring main configuration file

To use mybatis, you need to create some objects in the mybatis framework. Using these objects, you can use the functions provided by mybatis.

Analysis: which objects do mybatis need to use to execute sql statements
1. You need a proxy object with Dao interface, such as StudentDao interface. You need a proxy object. Use SqlSession.getMapper(StudentDao.class) to get the Dao proxy object

2. SqlSessionFactory is required 👉 Create SqlSessionFactory object 👉 Get the SqlSession object using openSession() 👉 Execute SqlSession.getMapper()

3. Data source DataSource object. Use a more powerful and functional connection pool object to replace mybatis's own PooledDataSource

👇👇👇

spring integration mybatis

Implementation steps:
1. The mysql database used, student table student2(id int primary key column, auto growth, name varchar(80), age int)

2. Create maven project

3. Add dependent gav
spring dependency, mybatis dependency, mysql driver, junit dependency
Mybatis spring dependency (provided on the mybatis website to create mybatis objects in spring projects)
spring's dependency on transactions (commit, rollback).

When mybatis and spring are integrated, transactions are automatically committed.

4. Create entity class Student

5. Create Dao interface and mapper file and write sql statements

6. Write mybatis master configuration file

7. Create the service interface and its implementation class

8. Create spring configuration file
1) Declare the data source DataSource and use Alibaba's Druid connection pool
2) Declare the SqlSessionFactoryBean class. The SqlSessionFactory object is created inside this class.
3) Declare mappercannerconfiguration class and create dao proxy object internally,
The created objects are placed in the spring container.
4) Declare the service object and assign the dao in 3) to the service attribute

9. Test dao access database

Concrete implementation

1. Create maven project
2.MySQL creates a database springdb and a new table Student

3. Add dependency in pom.xml

<!--spring rely on-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.2.5.RELEASE</version>
      </dependency>

      <!--spring Transaction 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>

      <!--mybatis rely on-->
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.5.1</version>
      </dependency>

      <!--mybatis and spring integrate-->
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.3.1</version>
      </dependency>

      <!--mysql drive-->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.9</version>
      </dependency>

      <!--Ali's connection pool-->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.12</version>
      </dependency>

Add plug-in:

   <build>
        <resources>
            <resource>
                <directory>src/main/java</directory> <!--Directory where-->
                <includes>
                    <!--Including the.properties,.xml All files will be scanned-->
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

4. Create entity class (domain package) --- Stuent

Define properties and column names, and generate set, get and toString methods.

5. Create Dao interface (Dao package) ---- StudentDao

Write two methods:

6. Without generating implementation classes, write mapper file - mybatis-mapper.xml


👇

7. Main configuration file: mybatis-config.xml

👇

8. Create service class

Implementation interface class:


9.Spring configuration file

Three objects:

1> Druid datasource DruidDataSource

Druid is Alibaba's open source database connection pool. It is the best database connection pool in the Java language.
Druid can provide powerful monitoring and extension functions.
The biggest difference between Druid and other database connection pools is that it provides database connection
👉Official website
👉Use address

2> Declare data source object:



3> Declare mappercannerconfigurer
 SqlSession.getMapper(StudentDao.class)

Mappercannerconfigurer is used to loop the package represented by basePackage, find each interface in the package, and call SqlSession.getMapper. Each dao interface creates a dao object, and the dao proxy object is placed in the container.

ApplicationContext ctx = ....
SqlSessionFactory sqlSessionFactory  = ctx.getBean("factory");
SqlSession session = sqlSessionFactory.openSession();
for(Interface: com.bjpowernode.dao){
         Interface object =  session.getMapper(Interface)
         springMap.put(The first letter of the interface name is lowercase, and the object)
}

👇 Create dao proxy objects from all interfaces under the dao package and put them in the spring container

Test: view object name and type

👇
Create a new OrderDao and its configuration file

Test: by default, the created object name is the initial lowercase of the interface name

Insert data:

Refresh database:

4> Declare service 👉 The service uses the corresponding dao for data related operations




10. Use external attribute profile
The database information has a separate property configuration file. properties


Then in the Spring main configuration file:

<!--Load external property profile-->
<context:property-placeholder location="classpath:jdbc.properties"/>

Posted by auteejay on Wed, 10 Nov 2021 03:25:49 -0800