SSM application (3)--Spring integrates MyBatis

Integrate ideas

  What exactly is MyBatis going to do?

    SqlSessionFactoryBuilder, SqlSessionFactory, SqlSession, operate the database;

    Mainly complete the operation of the database;

  What exactly does Spring do

    manage beans

    Manage the relationship between beans and beans

  SM integration

    Spring manages SqlSessionFactory with corresponding beans

    Spring's support for data manipulation: get the SqlSession object from the Spring-managed factory

    SqlSession operation data - MyBatis

    What are the beans in Spring

      DataSource: data source

      SqlSessionFactory: connection factory

      XXXDao: data operations, SqlSession, and subsequent services also need to find the beans that Spring wants dao;

      Their relationship: SqlSessionFactory needs to use the DataSource bean to establish a connection, representing dependency injection in Spring; XXXDao needs SqlSessionFactory to provide SqlSession to implement data operations and dependency injection;

     

  Integration steps:

    Introduce jar packages and build code frameworks;

      

  The integration of Spring and MyBatis can be achieved in two ways, which will be introduced one by one below:

 

Mapping-Based Integration

  DataSource: Configured in Spring's core configuration file

    Database related settings

  sessionFactory: Build reads the core configuration file of MyBatis: used to set the database environment / register the mapping file

    Depends on the DataSource bean, which can only be created if there is a data source

    The core configuration file of MyBatis provides the mapping file to be registered

   Dao: Use SqlSession to complete data operations

    SqlSession is from sessionFactory

  step:

    1. Configure the core configuration file of Spring

     

    2. Implement the mapper/dao interface of MyBatis and register mapper in conf

    3. Provide the implementation class of the dao layer to operate the database. There is an attribute sqlSessionFactory in the dao implementation class

    

    4. Write the dao implementation class; inherit the daoSupport class, so as to realize the attribute injection of sessionFactory

    

    5. Write test classes;

    

 

Integration based on mapper agent

  Mapper proxy method:

    Mapper interface

    Mapper mapping file

  The same name and the same package, so that the proxy class can be generated based on the interface and mapping file

   

  step:

  1. In Spring's core configuration file, configure DataSource and sessionFactory

  

  2. Configure the bean of the dao proxy class in Spring

  3. For each interface, configure a proxy class

  

  4. Write test classes

  

  

  You can also automatically generate mapper agents by scanning packages; inject sqlSessionFactory, and all interfaces under the default package must generate agents

  

 

PS: Due to the limited ability of the author, please forgive me if there are any mistakes; 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325004293&siteId=291194637