Myatis learning-MyBatis overview and environment construction

What's in this article

What is Mybatis? Why does it appear? How do we build its environment? How do we simply understand the role of Mybatis.

What is a framework?

It is a set of solutions in our software development, and different frameworks solve different problems.

It’s a semi-finished product, a set of components, a stage set up by others, and you perform

Benefits of using the framework:

  • The framework encapsulates a lot of details, allowing developers to implement functions in a minimalist way. Greatly improve development efficiency

Three-tier architecture

Presentation layer: used to display data

Business layer: deal with business needs

Persistence layer: it interacts with the database
Insert picture description here

Persistence layer technology solutions

  • JDBC技术:Connection PreparedStatement ResultSet

  • Spring's JdbcTemplate: Simple encapsulation of jdbc in Spring

  • Apache's DBUtils: It is very similar to Spring's JdbcTemplate, and it is also a simple encapsulation of Jdbc

  • These are not frameworks, JDBC is a specification, and the remaining two are just tools

  • Traditional JDBC is very troublesome. It is very troublesome and repetitive to set up connections, execute SQL, query result sets, and release resources. In fact, we are only concerned about sql

  • Encapsulate the tedious part, and then let us put more energy on the business

Overview of Mybatis framework

  • Mabatis is an excellent Java- based persistence layer framework , which encapsulates many details of jdbc, so that developers only need to pay attention to the SQL statement itself, instead of spending energy to deal with the complicated process of loading drivers, creating connections, creating statements, etc.
  • Mybatis configures various statements to be executed through xml or annotation, and generates the final executed sql statement through the mapping of java objects and the dynamic parameters of sql in the statement. Finally, the mybatis framework executes sql and maps the results into java objects return
  • The ORM idea is adopted to solve the problem of entity and database mapping, and encapsulate jdbc, shielding the details of the underlying access of jdbc api, so that we can complete the persistence operation of the database without using jdbc api to deal with
  • ORM (object relational mapping): Corresponding database tables with entity classes and the attributes of entity classes, so that we can operate the database by operating the entity classes

Mybatis introductory exercise

Create database and table

  • And add data

Create a maven project, prepare the database, and import dependent coordinates

  • It's just an ordinary maven project, it's no different

  • Packaging method uses jar package

  • Introduce the dependency of mybatis, find the coordinate of the dependency through the official website

  • Mybatis alone is not enough, but also a database

  • Then log log4j

  • Then unit test junit

  • Only the above two are enough to use mybatis

  • Insert picture description here
    The effect is shown in the figure, the jar package is already there

    <dependencies>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.2</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.41</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.12</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

Set up the environment

  • Domain and dao to be prepared

  • dao (data access object) data access layer

  • One-to-one correspondence between domain and database table javabean (one extra layer in addition to the three-tier architecture)

  • The entity class is placed in the domain (one-to-one correspondence with the database table), and the method of database interaction is placed in the dao

  • Insert picture description here

  • Configure mybatis environment

  • Create SqlMapConfig.xml under resources, which should add constraints and declarations.

    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <!--以上是约束,下面开始配置-->
    
    <!--mybatis的主配置文件-->
    <configuration>
        <!--    配置环境-->
        <environments default="mysql">
            <!--        配置mysql环境-->
            <environment id="mysql">
                <!--            配置事务的类型-->
                <transactionManager type="JDBC"></transactionManager>
                <!--            配置数据源(连接池)-->
                <dataSource type="POOLED">
                    <!--            配置连接数据库的4个基本信息-->
                    <property name="driver" value="com.mysql.jdbc.Driver"></property>
                    <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"></property>
                    <property name="username" value="root"></property>
                    <property name="password" value="root"></property>
                </dataSource>
            </environment>
        </environments>
        <!--        指定映射配置文件的位置,映射配置文件值的是每个dao独立的配置文件-->
        <mappers>
            <mapper resource="com/itheima/dao/IUserDao.xml"/>
        </mappers>
    
    </configuration>
    

Configure dao interface (configuration file)

The configuration file is stored under resources, and the directory structure corresponds to the dao interface file one-to-one (otherwise it cannot be found). Then its role is to configure the sql statement used by the method in the corresponding interface, so that mybatis can help us build the implementation class.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.itheima.dao.IUserDao">
 <!--配置查询所有-->
 <select id="findAll">
     select * from user
 </select>
</mapper>

Summary of environment construction

Step 1: Create a maven project and import coordinates

Part 2: Create an interface between entity classes and dao

Step 3: Create the main configuration file SqlMapConfig.xml of Mybatis

Step 4: Create the mapping configuration file IUserDao.xml

Precautions for environment construction:

  1. The names of IUserDao.xml and IUserDao.java were created to be consistent with our previous knowledge. In Mybatis, it also calls the operation interface name and mapping file of the persistence layer mapper . So IUserDao and IUserMapper are the same (ylrc)

  2. When creating a directory in idea, it is not the same as a package. When the package is created, such as com.itheima.dao is a three-level structure, and the directory is a first-level structure, so if you want three layers, you need to create them one by one

  3. The location of the mapping configuration file of mybatis must be the same as the package structure of the dao interface. One is in the main-java directory, and the other is in the resources directory. The two should correspond to each other

  4. The value of the namespace attribute of the mapper tag of the mapping configuration file must be the fully qualified class name of the dao interface

  5. The operation configuration of the mapping configuration file (select), the value of the id attribute must be the method name of the dao interface

    When we comply with points 3, 4, and 5, we no longer need to write dao implementation classes during development, and we no longer need to write dao implementation classes during development. mybatis helps us to achieve . (This is very convenient, we only need to define the interface, define the sql statement, and then we don't have to worry about it, before we have to write the implementation class ourselves, which is actually very troublesome)

Guess you like

Origin blog.csdn.net/qq_34687559/article/details/110224778