[The Road to Java] MyBatis Study Notes (1)

Three-tier architecture

Presentation layer: display data
Business layer: handle business requirements
Persistence layer: interact with database

MyBatis: persistence layer framework
Spring: IoC (inversion of control) and AOP (aspect-oriented programming)
SpringMVC: presentation layer MVC framework

Persistent technology solutions

JDBC technology ( specification )

  • Connection
  • PreparedStatement
  • ResultSet

Spring's JdbcTemplate ( tool class )
Apache's DBUtils ( tool class )

MyBatis overview

MyBatis is a persistence layer framework, written in Java

Encapsulates many details of the JDBC operation, so that developers only need to pay attention to the SQL statement itself, and do not need to pay attention to the complicated process of registering drivers and creating connections

It uses ORM ideas to achieve encapsulation of result sets, etc.

ORM: Object Relational Mapping object relationship mapping mapping
database table and entity class, entity class attributes

Getting started with MyBatis

MyBatis environment construction

  1. Create Maven project and import coordinates
  2. Create an entity class and dao interface
  3. Create the main configuration file of MyBatis (SqlMapConfig.xml)
  4. Create a mapping configuration file (IUserDao.xml)

Notes on environment construction:

  1. When creating IUserDao.xml and IUserDao.java, the name is to be consistent with the previous knowledge.
    The operation interface name and mapping file of the persistence layer in MyBatis is also called Mapper.
    So IUserDao and IUserMapper are the same.
  2. When creating a directory in IDEA, the creation method is different from the package.
    When the package is created: com.akatsuki.dao is a three-tier structure
    directory. When created, com.akatsuki.dao is a first-level directory
  3. The location of the mapping configuration file of MyBatis must be the same as the package structure of the dao interface.
  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. For the operation configuration of the mapping configuration file, the value of the id attribute must be the method name of the dao interface.

After we have complied with points 3-5, there is no need to write dao implementation classes in development.

Guess you like

Origin www.cnblogs.com/Akatsuki-Sanjou/p/12749009.html