Mybatis is a very useful tool, but write mapper is a very troublesome thing, since Mybatis 3.0 can start to use annotations, greatly simplifies the amount of XML writing, local want to see mybatis source code, their own extension to write a tool, In the process of reading the source code found a universal mapper toolkit, feel no need to repeat the wheel, a brief record of the use of Spring Boot integration universal Mapper.

  1. Make sure you can use Mybatis normally

  2. The POM introduces dependency packages, and the starter needs to be used with the @mapper annotation, which is used here, or with the @Mapperscan annotation, @ tk. Mybatis. Spring. The annotation. MapperScan (basePackages = “scan package”) used with native mapper.

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

    The version I’m using is 2.0.2

  3. Mybatis Scan Configuration (Deprecated, Spring automatic configuration)

    Note that MapperScannerConfigurer runs early, So you must have the following comments @ AutoConfigureAfter (MybatisAutoConfiguration. Class) public class MyBatisMapperScannerConfig {@ Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); mapperScannerConfigurer.setBasePackage("org.springboot.sample.mapper"); Properties properties = new Properties(); // Be careful not to place MyMapper in basePackage, i.e. it cannot be scanned like other Mapper. properties.setProperty("mappers", MyMapper.class.getName()); properties.setProperty("notEmpty", "false"); properties.setProperty("IDENTITY", "MYSQL"); mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer; }}Copy the code
  4. Create a new BaseMapper class. This class cannot be scanned as a normal Mapper, without the @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> {
    }Copy the code
  5. Business processing DAO layer, extending BaseMapper

    package com.zj.mapper;
    
    import com.zj.model.OrderInfo;
    import org.apache.ibatis.annotations.Mapper;
    
    @Mapper
    public interface OrderInfoMapper extends BaseMapper<OrderInfo> {}Copy the code
  6. Other and use ordinary Mybatis consistent, service layer part of the code

    orderInfoMapper.insertSelective(info);
    OrderInfo info = orderInfoMapper.selectByPrimaryKey(id);Copy the code

    The general mapper provides some common operations: deleteByPrimaryKey, insert, insertSelective, selectByPrimaryKey, updateByPrimaryKeySelective, updateByPrimaryKey, InsertList and many other methods need you to further explore 😀😀

  7. Primary key ID problem

    When you use insert, insertSelective, etc., and want to return increments generated by the database, you need to add annotations to the entity class

    @Id
    @GeneratedValue(generator = "JDBC")
    private Long orderInfoId;Copy the code

    Generator =”JDBC” means that MyBatis uses JDBC’s getGeneratedKeys method to retrieve primary keys generated internally by the database. Generator =”JDBC” means that MyBatis uses JDBC’s getGeneratedKeys method to retrieve primary keys generated internally by the database.

    Or:

    @Id
    @KeySql(useGeneratedKeys = true)
    private Long id;Copy the code
  8. If the entity field does not match the database field, you can use the @column annotation. For other annotations, see Annotations

     @Column(name="SCORE_SUM")
     private String sumScore;Copy the code
  9. MBG generation see github.com/abel533/Map… [email protected]:silloy/mybatis-generator.git

  10. See wiki for more details

The general purpose Mapper greatly simplifies the writing of XML files, but still requires a few XML files to be further optimized. At the same time, since this is a personal project, it is not recommended to use it if you are not familiar with it.

In this paper, byBy the old farmerCreate, adoptCC BY 4.0 CNAgreement to license. It can be reproduced and quoted freely, but the author must be signed and indicate the source of the article. If reprinted to wechat official account, please add the author’s official qr code at the end of the article.