Summary of MyBatis learning notes [1]

Official learning documents

Introduction to MyBatis

mybatis is a persistence layer framework written in Java.
He encapsulates many details of jdbc operation, so that developers only need to care about the sql statement itself, without paying attention to the complex processes such as registering drivers and creating connections. It uses the idea of ORM (ORM: object relational mapping) to encapsulate the result set. It is to correspond the database table to the entity class and the attributes of the entity class, so that we can operate the entity class to operate the database.
The attribute in the entity class is consistent with the field name of the database table.


  • Easy to learn: it is small and simple in itself. There is no third-party dependency. The simplest installation is as long as two jar files + several sql mapping files are configured. It is easy to learn and use. Through documents and source code, you can fully master its design idea and implementation.
  • Flexibility: mybatis does not impose any impact on the existing design of the application or database. sql is written in xml to facilitate unified management and optimization. All the requirements of operating the database can be met through sql statements.
  • Decouple sql and program code: separate business logic and data access logic by providing DAO layer, so as to make the design of the system clearer, easier to maintain and easier to unit test. The separation of sql and code improves maintainability.
  • Provide mapping labels to support the mapping between objects and ORM fields in the database.
  • Provide object relationship mapping labels to support the establishment and maintenance of object relationships.
  • Provide xml tags to support writing dynamic sql.

Environment construction

1. Import dependency - > 2 Create a module - > 3 Write code (entity class, mapper[dao] interface, interface implementation class)
1. Import dependency
For these dependencies, you can find the corresponding version in MVN.
MVN link

<!--mysql drive-->

Written by MyBatis

[note] generally, mapper is used for package naming (dao is used in the figure)

MyBatis profile


# MySQL 8.0 + drive path
# driver=com.mysql.cj.jdbc.Driver
#For MySQL 8.0 +, you need to set the time zone& serverTimezone=Asia/Shanghai


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-// Config 3.0//EN"

	<!--These properties can be configured externally and can be replaced dynamically. You can either Java These properties can be configured in the properties file or in the properties Set in the child element of the element.-->
    <properties resource=""/>
    <!--This is MyBatis Very important adjustment settings in, they change MyBatis Runtime behavior of.-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="cacheEnabled" value="true"/>
	<!--Type alias can be Java Type sets an abbreviated name. It is only used for XML Configuration, which is intended to reduce redundant writing of fully qualified class names-->
        <package name="pojo"/>
	<!--MyBatis It can be configured to adapt to a variety of environments, and this mechanism helps to SQL Mapping is applied to a variety of databases, and in reality there are many reasons to do so. For example, development, testing and production environments need different configurations; Or want to have the same Schema Use the same in multiple production databases SQL Mapping. There are many similar usage scenarios.
But remember: Although you can configure multiple environments, each SqlSessionFactory Only one environment can be selected for an instance.-->

    <environments default="development">

        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>


	<!--tell MyBatis Where can I find the mapping file-->
        <mapper class="dao.UserMapper"/>

Keywords: Java Mybatis

Added by purpleshadez on Sun, 27 Feb 2022 10:56:40 +0200