Get started quickly with mybatis mapper

Project address: https://mapper.mybatis.io

introduce

This is a general Mapper that can be used directly without any configuration. It can be used directly in the project through simple learning.

1.1 main objectives

  1. Out of the box, without any configuration, you can obtain a large number of general methods by inheriting the base class Mapper;
  2. You can set up your own base class Mapper by copying and pasting at will;
  3. Comprehensive and considerate, providing Service layer encapsulation to facilitate business use and understanding of Mapper;
  4. It is simple and intuitive, provides ActiveRecord mode, and can be used directly in combination with Spring Boot automatic configuration;
  5. Custom method, a few lines of code can realize the custom general method;
  6. Easy extension, easy extension through Java SPI.

1.2 system requirements

MyBatis Mapper requires the minimum version of MyBatis to be 3.5 1. The latest version is recommended.

Like the MyBatis framework, Java 8 is required as a minimum.

1.3 installation

If you use Maven, in your POM XML add the following dependencies:

<dependencies>
  <dependency>
    <groupId>io.mybatis</groupId>
    <artifactId>mybatis-mapper</artifactId>
    <version>1.0.0</version>
  </dependency>
  <!-- TODO Select dependencies as needed -->
  <!-- use Service Layer packaging -->
  <dependency>
    <groupId>io.mybatis</groupId>
    <artifactId>mybatis-service</artifactId>
    <version>1.0.0</version>
  </dependency>
  <!-- TODO Select dependencies as needed -->
  <!-- use ActiveRecord Mode time -->
  <dependency>
    <groupId>io.mybatis</groupId>
    <artifactId>mybatis-activerecord</artifactId>
    <version>1.0.0</version>
  </dependency>
</dependencies>

If you use Gradle, in build Add to Gradle:

dependencies {
    compile("io.mybatis:mybatis-mapper:1.0.0")
    // When using Service layer encapsulation
    compile("io.mybatis:mybatis-service:1.0.0")
    // When using ActiveRecord mode
    compile("io.mybatis:mybatis-activerecord:1.0.0")
}

1.4 quick setting

The basic principle of MyBatis Mapper is to map the entity class to the table and field information in the database. Therefore, the entity class needs to configure the basic metadata through annotation. After configuring the entity, you only need to create a Mapper interface that inherits the basic interface to start using it.

1.4. 1 entity class configuration

Suppose there is a table:

create table user
(
    id   INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
    name VARCHAR(32) DEFAULT 'DEFAULT',
    sex  VARCHAR(2)
);

Corresponding entity class:

import io.mybatis.provider.Entity;

@Entity.Table("user")
public class User {
  @Entity.Column(id = true)
  private Long   id;
  @Entity.Column("name")
  private String username;
  @Entity.Column
  private String sex;

  //Omit the set and get methods
}

@ entity. Must be added on entity class The table annotation specifies the table name corresponding to the entity class. It is recommended to specify the table name explicitly. If the table name is not provided, use the class name as the table name. All fields belonging to columns in the table must be added with @ entity Column annotation. When the column name is not specified, the field name is used (without any conversion). The field can be marked as the primary key by id=true.

@The two annotations contained in Entity provide a large number of configuration attributes. If you want to use more configurations, please refer to 3.1 below@ Content of Entity annotation,
Here is a simple example:

@Entity.Table(value = "sys_user", remark = "System user", autoResultMap = true)
public class User {
 @Entity.Column(id = true, remark = "Primary key", updatable = false, insertable = false)
 private Long   id;
 @Entity.Column(value = "name", remark = "accounts")
 private String userName;
 //Omit others
}

1.4.2 Mapper interface definition

Once you have a User entity, you can directly create an interface that inherits Mapper:

//io.mybatis.mapper.Mapper
public interface UserMapper extends Mapper<User, Long> {
  
}

This interface can be used directly as long as it is scanned by MyBatis.

Here are several common scanning configurations:

  1. The configuration file of MyBatis is MyBatis config xml:
 <mappers>
   <!-- Scan the specified package -->
   <package name="com.example.mapper"/>
 </mappers>
  1. Spring in spring XML configuration:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.example.mapper"/>
  <property name="markerInterface" value="io.mybatis.service.mapper.RoleMarker"/>
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryRole"/>
</bean>
  1. Spring Boot configuration, startup class annotation method:
@MapperScan(basePackages = "com.example.mapper")
@SpringBootApplication
public class SpringBootDemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringBootDemoApplication.class, args);
  }

}

In Spring Boot, you can also directly add @ org. Org to the interface apache. ibatis. annotations. Mapper annotation. You can omit @ MapperScan configuration after adding annotation.

1.4. 3 use

After defining the interface, you can get UserMapper for use. The following is a simple example:

User user = new User();
user.setUserName("test");
userMapper.insert(user);
//Auto increment id written back after saving; cannot be empty
Assert.assertNotNull(user.getId());
//Query by id
user = userMapper.selectByPrimaryKey(user.getId());
//delete
Assert.assertEquals(1, userMapper.deleteByPrimaryKey(user.getId()));

From here, you can see that except for the configuration of MyBatis itself, MyBatis Mapper only needs to configure entity class annotations,
Create the corresponding Mapper interface and you can use it directly without any cumbersome configuration.

The above example simply uses MyBatis Mapper, and many out of the box functions are not involved,
After the above example runs successfully, it is recommended to continue to view the detailed documents of other modules of the project and get familiar with the documents of each part,
When using MyBatis Mapper, you will be more handy and do whatever you want.

More documents are available through https://mapper.mybatis.io Understand.

Added by JasonGreen on Sun, 26 Dec 2021 00:19:20 +0200