MyBatis quick start and Mapper proxy development.

Table of contents

1. MyBatis

Introduction to Basic Concepts

2. MyBatis quick start

 1. Quick start case

 2. Configure the database in idea 

3. Mapper agent development

 1. Development purpose

 2. Mapper proxy method


1. MyBatis

Introduction to Basic Concepts

▶ What is MyBatis

        MyBatis was originally iBatis, an open source project of Apache. In 2010, the project was migrated from apache software foundation to google code and renamed MyBatis. Migrated to Github in November 2013.        

        MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis eliminates almost all JDBC code and the work of setting parameters and getting result sets. MyBatis can configure and map primitive types, interfaces, and Java POJOs (Plain Old Java Objects, Plain Old Java Objects) as records in the database through simple XML or annotations.

         Official website link:  https://mybatis.net.cn/    

▶ frame

  • A framework is a semi-finished software, a set of reusable, general-purpose, software-based code models
  • Building software on the basis of the framework is more efficient, standardized, versatile, and scalable

▶ Persistence layer

  • The layer of code responsible for saving data to the database
  • JavaEE three-tier architecture: presentation layer, business layer, persistence layer

▶ Disadvantages of JDBC

    1. Hardcoded

        ▷ Register the driver repeatedly to get the connection

        ▷ Repeated SQL statements

    2. The operation is cumbersome

        ▷ Manual setting of parameters

        ▷ Manually encapsulate the result set

▶ MyBatis simplifies operation

        1. All connections, SQL statements, etc. are written into the configuration file

        2. The operation of obtaining the result set is eliminated, and it can be done in one sentence


2. MyBatis quick start

 1. Quick start case

▶ Query data in database tables

  1. Create user table and add data

  2. Create a module and import coordinates

  3. Write the MyBatis core configuration file: replace the connection information and solve the hard coding problem.

  4. Write SQL mapping files: manage sql statements in a unified way and solve hard coding problems.

  5. Coding:

        ① Define the POJO class

        ② Load the core configuration file and get the SqlSessionFactory object

        ③ Get the SqlSession object and execute the SQL statement

        ④ Release resources

▶ Create user table and add data

        Create a database, create tables, and add data to it


▶ Create modules, import coordinates

 1. Create a module

 2. Import coordinates

         Go to MyBatis official website to find.

         The code is placed in the pom.xml file:

         

Note: When we need other jar packages, we need to import coordinates, such as:

You also need to add a configuration file under resources


▶ Write MyBatis core configuration file

        Still look at the information on the official website, including:

  


▶ Write SQL mapping file

        Still go to MyBatis official website to find:


▶ coding

① Define the POJO class: here is to write the User class

② Load the core configuration file and get the SqlSessionFactory object

     First find the corresponding code on the official website (because the official website has it, you don’t need to write it yourself)

 

③ Get the SqlSession object and execute the SQL statement

④ Release resources

2. Configure the database in idea 


3. Mapper agent development

 1. Development purpose

▶ Solve the hard coding in the native mode and simplify the later execution of SQL

Native

acting

2. Mapper proxy method

▶ steps

  1. Define the Mapper interface with the same name as the SQL mapping file, and place the Mapper interface and the SQL mapping file in the same directory
  2. Set the namespace attribute of the SQL mapping file to the fully qualified name of the Mapper interface
  3. Define the method in the Mapper interface, the method name is the id of the sql statement in the SQL mapping file, and keep the parameter type and return value type consistent
  4. Coding: Obtain the proxy object of the Mapper interface through the getMapper method of SqliSession, and ② call the corresponding method to complete the execution of sq/.

▶ first step

▶ Step 2

▶ The third step

 Note 1:

Note 2:

         Details: If the Mapper interface name and the SQL mapping file name are the same and are in the same directory, you can use package scanning to simplify the loading of the SQL mapping file

▶ The fourth step of encoding

Guess you like

Origin blog.csdn.net/yzh2776680982/article/details/126707161