This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

1. What is Mybatis

Mybatis is an excellent Java-based persistence layer framework, which encapsulates JDBC internally, so that developers only need to focus on THE SQL statement itself, and do not need to spend energy to deal with the complex process of loading drivers, creating connections, creating statements and so on.

Mybatis configures various statements to be executed using XML or annotations, and maps the dynamic parameters of the SQL in the Statement to generate the SQL statement to be executed.

Finally, the MyBatis framework executes the SQL and maps the results to Java objects and returns them. Using ORM thought to solve the entity and database mapping problem, the JDBC encapsulation, shielding the JDBC API access details, so that we do not have to deal with JDBC API, we can complete the database persistence operation.

MyBatis official website: www.mybatis.org/mybatis-3/

2. API related to Mybatis

2.1 the Resources

  • Org. Apache. Ibatis. IO. Resources: loading resource utility class.

  • Core method

2.2 Builder SqlSessionFactoryBuilder

  • Org. Apache. Ibatis. Session. The SqlSessionFactoryBuilder is: get SqlSessionFactory factory object function class

  • Core method

  • Build an SqlSessionFactory object by loading the input stream of the mybatis core file
String resource = "org/mybatis/builder/mybatis-config.xml"; 
InputStream inputStream = Resources.getResourceAsStream(resource); 
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); 
SqlSessionFactory factory = builder.build(inputStream);
Copy the code

The Resources utility class is in the org.apache.ibatis. IO package. The Resources class helps you load resource files from the classpath, file system, or a Web URL.

2.3 Factory object SqlSessionFactory

  • Org. Apache. Ibatis. Session. SqlSessionFactory: get SqlSession factory interface builder object.

  • Core API

2.4 SqlSession Session object

  • Org. Apache. Ibatis. Session. SqlSession: builder object interface. Used to execute SQL, manage transactions, and interface proxies.

  • Core API

SqlSession instances are a very powerful class in MyBatis. Here you’ll see all the methods for executing statements, committing or rolling back transactions, and getting mapper instances.

MyBatis mapping configuration file

3.1 Introduction to Mapping Configuration Files

  • The mapping configuration file contains the mapping between data and objects and the SQL statements to execute

3.2 Query Function

  • Select Query function label.

  • attribute

    Id: unique identifier used with namespaces. ParameterType: specifies the object type for parameter mapping. ResultType: Specifies the object type of the result mapping.Copy the code
  • #{attribute name}

  • The sample

3.3 New Functions

  • Insert: Added function labels.

  • attribute

    Id: unique identifier used with namespaces.

    ParameterType: specifies the object type for parameter mapping.

    ResultType: Specifies the object type of the result mapping.

  • #{attribute name}

  • The sample

3.4 Modifying Functions

  • Update: Modifies function labels.

  • attribute

    Id: unique identifier used with namespaces.

    ParameterType: specifies the object type for parameter mapping.

    ResultType: Specifies the object type of the result mapping.

  • #{attribute name}

  • The sample

3.5 Deleting Function

  • Delete: queries the function label.

  • attribute

    Id: unique identifier used with namespaces.

    ParameterType: specifies the object type for parameter mapping.

    ResultType: Specifies the object type of the result mapping.

  • #{attribute name}

  • The sample

  • Summary: CRUD operation, except the label name and SQL statement is different, other attribute parameters are basically the same.

3.6 Mapping Configuration File Summary

4.Mybatis core configuration file introduction

4.1 Core Configuration File Introduction

The core configuration file contains the core Settings and properties of MyBatis. Such as database connections, transactions, connection pool information, etc.

The diagram below:


      
<! DTD constraint for MyBatis -->
<! DOCTYPEconfiguration PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<! -- Configuration Core root tag -->
<configuration>

    <! -- Environments Configure the database environment. You can have multiple environments. The default attribute specifies which is used -->
    <environments default="mysql">
        <! --environment Configure the unique identifier of the database environment ID attribute -->
        <environment id="mysql">
            <! -- transactionManager Transaction management. Type attribute, using JDBC default transaction -->
            <transactionManager type="JDBC"></transactionManager>
            <! -- dataSource dataSource information type attribute connection pool -->
            <dataSource type="POOLED">
                <! -- property Gets configuration information about the database connection -->
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql:///db1" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>

    <! Mappers import the mapping configuration file -->
    <mappers>
        <! -- Mapper imports the specified mapping configuration file name -->
        <mapper resource="StudentMapper.xml"/>
    </mappers>
</configuration>
Copy the code

4.2 Importing database Connection Configuration Files

  • The properties tag brings in external files

        <! -- Import database connection configuration file -->
        <properties resource="jdbc.properties"/>
    Copy the code
  • For details, see the following table

      <! -- property Gets configuration information about the database connection -->
        <property name="driver" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    Copy the code

4.3 names

  • TypeAliases: Parent tags for aliases of full class names.

  • TypeAlias: Child tags aliased for full class names.

  • attribute

    Type: Specifies the full class name

    Alias: Specifies an alias

  • Package: A subtag that aliases all classes under the specified package. (An alias is the class name)

  • The diagram below:

  • The configuration is as follows:

        <! -- alias -->
        <typeAliases>
            <typeAlias type="com.111.bean.Student" alias="student"/>
            <! --<package name="com.111.bean"/>-->
        </typeAliase
    Copy the code

4.4 summarize