Spring Boot-Mybatis general mapper use

Posted May 25, 20203 min read

Mybatis is a very useful tool, but writing mapper is a very troublesome thing. Since mybatis 3.0, annotations can be used, which greatly simplifies the amount of xml writing. I want to see mybatis source code locally and write it myself. A tool, a common mapper toolkit was found in the process of reading the source code, I feel that there is no need to reinvent the wheel, briefly record the use of spring boot integrated general mapper.


  1. Make sure that mybatis can be used normally

  2. Pom introduces dependency packages. The starter needs to be used with @Mapper annotations. This method is used here, or the @ MapperScan annotation,` @ tk.mybatis.spring.annotation.MapperScan(basePackages = "scan package") Native mapper use.

       <groupId> tk.mybatis </groupId>
       <artifactId> mapper-spring-boot-starter </artifactId>
       <version> {version} </version>

    The version I use is 2.0. \ [2 ]()

  3. Mybatis scan configuration(Deprecated, spring automatic configuration)

     //TODO Note that since MapperScannerConfigurer executes earlier, it must have the following note
     public class MyBatisMapperScannerConfig {
         public MapperScannerConfigurer mapperScannerConfigurer() {
             MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
             Properties properties = new Properties();
             //Pay special attention here, do not put MyMapper in basePackage, that is, it cannot be scanned like other Mappers.
             properties.setProperty("mappers", MyMapper.class.getName());
             properties.setProperty("notEmpty", "false");
             properties.setProperty("IDENTITY", "MYSQL");
             return mapperScannerConfigurer;
  4. Create a new BaseMapper class, which cannot be scanned as a normal Mapper, without @Mapper annotation, or placed in a different folder

     package com.zj.mapper;
     import tk.mybatis.mapper.common.Mapper;
     import tk.mybatis.mapper.common.MySqlMapper;
     public interface BaseMapper <T> extends Mapper <T>, MySqlMapper <T> {
  5. Business processing dao layer, extend BaseMapper

     package com.zj.mapper;
     import com.zj.model.OrderInfo;
     import org.apache.ibatis.annotations.Mapper;
     public interface OrderInfoMapper extends BaseMapper <OrderInfo> {}
  6. Others are consistent with the use of ordinary mybatis, part of the service layer code

     OrderInfo info = orderInfoMapper.selectByPrimaryKey(id);

    The general mapper provides some commonly used operation methods:deleteByPrimaryKey, insert, insertSelective, selectByPrimaryKey, updateByPrimaryKeySelective, updateByPrimaryKey, insertList and many other methods, which require you to further explore ??

  7. The primary key id problem

    When using insert, insertSelective and other methods, I hope to return to the gradual generation of the database, you need to add annotations on the entity class

     @GeneratedValue(generator = "JDBC")
     private Long orderInfoId;

    generator = "JDBC" means that MyBatis uses JDBC's getGeneratedKeys method to retrieve the primary key generated internally by the database. It is suitable for self-increasing primary keys such as MySQL and SQL Server.


     @KeySql(useGeneratedKeys = true)
     private Long id;
  8. If the entity field and database field are inconsistent, you can use the @Column annotation, for other annotations, see Annotation

      @Column(name = "SCORE_SUM")
      private String sumScore;
  9. MBG generation see https://github.com/abel533/Ma ... , demo see git@github.com :silloy /mybatis-generator.git

  10. See wiki for more details

    Generic Mapper greatly simplifies the preparation of xml files, but still requires a few xml files, to be further optimized. At the same time, because this is a personal project, it is not familiar with the use and it is not recommended.

This article was created by QITU Lao Nong and licensed under the CC BY 4.0 CN agreement. It can be reproduced and quoted freely, but the author must be signed and the source of the article should be indicated. If reprinted to WeChat public account, please add the author's public account QR code at the end of the article.