Mybatis pagination query and date comparison

1. Background

  When we are developing Mybatis, we will encounter the problem of large data volume and date. This article focuses on the following issues:

  1. How to perform database paging query to avoid too large results and slow running speed
  2. How to compare dates, compare the size of two dates from different precisions

2. Sample database design

  Suppose there is such a user user table, his definition (MySql) is as follows:

id name birthday
bigint(20) varchar(64) datetime

  We want to design a query:

  • Query users whose birthday is less than 1990-01-30 (that is, born before 90 years)

Assuming that there are a lot of people born before 90 years, a pull will take a lot of time, so we introduce paging query here

3. Paging query design

  Generally, the process of writing Mapper is as follows: DO=> Query=> Mapper=> mapper.xml=>SQL query, of course, this is not a writing order that must be followed, you can do it according to your wishes. First we look at DO:

@Getter
@Setter
@ToString
public UserDO{
    
    
	private Long id;
	private String name;
	private Date birthday;
}

  Then focus on Querythe writing of the class. First, we write one PageQuery, and then all future query classes can inherit this class:

public class PageQuery{
    
    
    private Integer           currentPage;
    private Integer           pageSize;

    public int getStartRow() {
    
    
        if (currentPage == null || pageSize == null || currentPage <= 1) {
    
    
            return 0;
        }

        return (currentPage - 1) * pageSize;
    }

    //getter和setter
}

  The custom query class is BirthdayQueryinherited from PageQuery, and the query date field is mainly added:

@Getter
@Setter
@ToString
public class BirthdayQuery extends PageQuery {
    
    
    /**
     * 查询的生日日期
     */
    private Date queryDate;
}

  Then there is the definition of the Mapper interface, which is UserMapperdefined as follows:

@Mapper
public interface UserMapper{
    
    
	List<UserDO> listByBirthday(BirthdayQuery birthdayQuery)
}

  The last is the preparation of the xml file, the query will use the startRowsum pageSize, and the result mapping is omitted here:

<mapper namespace="com.test.mapper.Usermapper">
    <select id="" resultMap="com.test.dataobject.UserDO">
        SELECT
        id,name,birthday
        FROM
        user
        WHERE <![CDATA[ birthday < (#{queryDate,jdbcType=TIMESTAMP})]]>
        LIMIT #{startRow}, #{pageSize}
    </select>
</mapper>

At this point, you’re done. It’s worth noting that the #{startRow}attribute is getStartRow()obtained by the use method. Mybatis uses OGNL expressions to obtain the attribute value. If you are interested, you can learn more about it.

4. Date comparison in Mybatis

  In the preparation of xml, we used it jdbcType. We should pay attention to an enumeration class JdbcTypein Mybatis , which jdbcTypeacts as an interceptor here . It intercepts when writing data to the database and when fetching data from the database. After the data is retrieved from the database and before the object is assigned, the correct jdbcTypesetting rules are:

  • The jdbctype type corresponding to the date type field is jdbcType="DATE"
  • The datetime and timestimp type fields correspond to jdbcType="TIMESTAMP"
  • The time field corresponds to jdbcType="TIME"

  jdbcTypeThe impact of type on java.util.Date date data access to the database:

  • When jdbcType="DATE", the time, minute and second will be filtered out when accessing data to the database
  • When jdbcType="TIMESTAMP", jdbcType does not filter any content and has no effect on access
  • When jdbcType="TIME", the year, month and day will be filtered out when accessing data to the database

5. References

1.
JdbcType 2. OGNL tutorial in
MyBatis 3. Java date type object access to the database through mybatis

Guess you like

Origin blog.csdn.net/u012397189/article/details/101701141