Easily implement basic SQL methods: use BaseMapper class inheritance to write Mapper files

        During the development process, we often need to use a database to store and manage data, and Mybatis is a commonly used persistence layer framework. In Mybatis, we often need to write Mapper files to interact with the database. However, for some very basic operations, we often need to write many repeated SQL statements, which is time-consuming and laborious. This article will introduce a simple and efficient method, which is to implement basic SQL methods by inheriting the BaseMapper class, thereby realizing CRUD operations in Mapper files.

We only need to create the xxMapper interface in the mapper package, which inherits the BaseMapper class. There is no need to write additional code unless you have special database operations.

For example:

@Mapper
public interface PlayerMapper extends BaseMapper<Player> {
}

<Player> is an entity class object.

When using it, you only need to inject the Mapper object and call the method through the mapper object.

You only need to understand the following content and do not need to write it in the project.

The specific code of the BaseMapper class is:

public interface BaseMapper<T, ID> {
  long countByExample(Example example);

  int deleteByExample(Example example);

  int deleteByPrimaryKey(ID id);

  int insert(T record);

  int insertSelective(T record);

  List<T> selectByExampleWithBLOBs(Example example);

  List<T> selectByExample(Example example);

  T selectByPrimaryKey(ID id);

  int updateByExampleSelective(@Param("record") T record, @Param("example") Example example);

  int updateByExampleWithBLOBs(@Param("record") T record, @Param("example") Example example);

  int updateByExample(@Param("record") T record, @Param("example") Example example);

  int updateByPrimaryKeySelective(T record);

  int updateByPrimaryKeyWithBLOBs(T record);

  int updateByPrimaryKey(T record);
}
  • countByExample(Example example): Count the number of records that meet the conditions.
  • deleteByExample(Example example): Delete records based on conditions.
  • deleteByPrimaryKey(ID id): Delete records based on primary key.
  • insert(T record): Insert a record.
  • insertSelective(T record): Selectively insert a record, which can be inserted based on the value of the attribute in the entity class.
  • selectByExampleWithBLOBs(Example example): Query qualified records based on conditions, including BLOB fields.
  • selectByExample(Example example): Query matching records based on conditions.
  • selectByPrimaryKey(ID id): Query a record based on the primary key.
  • updateByExampleSelective(@Param("record") T record, @Param("example") Example example): Selectively update records based on conditions, which can be updated based on the values ​​of attributes in the entity class.
  • updateByExampleWithBLOBs(@Param("record") T record, @Param("example") Example example): Update records based on conditions, including BLOB fields.
  • updateByExample(@Param("record") T record, @Param("example") Example example): Update records based on conditions.
  • updateByPrimaryKeySelective(T record): Selectively update records based on the primary key, which can be updated based on the value of the attribute in the entity class.
  • updateByPrimaryKeyWithBLOBs(T record): Update records based on the primary key, including BLOB fields.
  • updateByPrimaryKey(T record): Update records based on primary key.
  • If the return value is of type int, it will return the number of successful records. If the operation fails, 0 will be returned; if the return value is list or T, it will return the specific content queried.

If the method you want to operate does not exist in the parent class, you can also write it yourself in the class.

For example: write a query for the maximum value of the id field in the tb_prize table and return it.

@Mapper
public interface PrizeMapper extends BaseMapper<Prize> {
    @Select("SELECT MAX(id) FROM tb_prize")
    Integer selectMaxprizeID();
}

Guess you like

Origin blog.csdn.net/weixin_51451545/article/details/131267622