Understanding of the MyBatis framework

Article Directory


foreword

Writing this is actually as my own notes to record learning, if there are similarities, it is normal


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is the framework?

       A software framework is a generic, reusable software environment that provides specific functionality to facilitate the development of software applications, products, and solutions. A software framework will include supporting programs, compilers, code, libraries, toolsets, and APIs that bring all these components together to support the development of a project or system.

        MyBatis is iBatis, an open source project of Apache. In 2010, the project was migrated from Apache Software Foundation to Google Code, and renamed MyBatis. In November 2013, MyBatis was migrated to Github. MyBatis is an excellent persistence layer framework , which can establish a mapping relationship between entity classes and SQL statements. It is a semi-automatic ORM (Object/Relation Mapping, object-relational mapping) implementation. MyBatis is less encapsulated than Hibernate, but it has superior performance, is easy to learn, and is widely used in the development of Internet applications.

Second, the advantages of the MyBatis framework

The MyBatis framework can solve the disadvantages of JDBC

What are the disadvantages of JDBC? How MyBatis solves it

First of all, JDBC is the basis for Java programs to achieve data access

  1. Frequent creation and release of database connections will waste system resources and affect system performance.                      MyBatis solution: configure the data link pool in SqlMapConfig.xml, and use the connection pool to manage the database link.
  2. SQL statements are hard-coded in the code, making the code difficult to maintain. In the development of practical applications, SQL is more likely to change. In traditional JDBC programming, SQL changes need to change Java code, which violates the principle of opening and closing.                 MyBatis solution: MyBatis configures the SQL statement in the MyBatis mapping file, realizing the separation from the Java code.
  3. Using PreparedStatement to pass parameters to placeholders is hard-coded, because the where conditions of SQL statements are not necessarily certain, and may be more or less. Modifying SQL requires modifying the code, making the system difficult to maintain.                                        MyBatis solution: MyBatis automatically maps Java objects to SQL statements, and defines the type of input parameters through parameterType in Statement.
  4. JDBC has hard-coded result set parsing (query column names), and SQL changes lead to changes in parsing codes, making the system difficult to maintain.                                                                                                                               MyBatis solution: MyBatis automatically maps SQL execution results to Java objects, and defines the type of output results through the resultType in Statement.

        MyBatis is a persistence layer framework that supports common SQL queries, stored procedures, and advanced mapping. It eliminates almost all manual settings of JDBC codes and parameters and retrieval of result sets. It uses simple XML or annotations for configuration and original mapping, and maps interfaces and Java POJOs to records in the database, enabling Java developers to use object-oriented programming ideas to operate the database . The MyBatis framework is an ORM (Object/Relation Mapping, object-relational mapping) framework. The so-called ORM is a technology to solve the mismatch of data types in object-oriented and relational databases . It automatically persists objects in Java applications to tables in relational databases by describing the mapping relationship between Java objects and database tables.

       How ORM Frameworks Work

3. The construction of MyBatis environment

The basic steps are (1) Create a project;        

                   (2) Introduce related dependencies;        

                   (3) Database preparation;        

                   (4) Write database connection information configuration files;        

                   (5) Write core configuration files and mapping files.

1. Create a project

Create a Maven project in IDEA

2. Introduce related dependencies

Import MySQL driver package, Junit test package, MyBatis core package and other related dependencies into the pom.xml file of the project.

For example

<dependencies>
     <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>
</dependencies>

Maven provides an automatic import function, you only need to specify the type and version of the reference in the pom.

Manually import the jar

Create a new directory on the project, lib, copy the used jar package into it, right click on lib, select Add As Library..----- Add to module-----Project Library, click ok, delete <dependencies></dependencies> of pom.xml together with the code between it.

3. Database preparation

omit

4. Write database connection information configuration file

Create a database connection information configuration file : Create a database connection configuration file in the src/main/resources directory of the project, here it is named db.properties, and configure the parameters of the database connection in this file.

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
username=root
password=

5. Write core configuration files and mapping files

Create the core configuration file of MyBatis: Create the core configuration file of MyBatis under the src/main/resources directory of the project. This file is mainly used for the environment configuration of the project, such as database connection related configuration, etc. The core configuration file can be named whatever you want, but it is usually named mybatis-config.xml.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="db.properties"/>
    <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}"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

Summarize

no summary

Guess you like

Origin blog.csdn.net/weixin_56342559/article/details/129891803