Mybatis series (25) – related concepts – total number of queries

Time:2020-10-19

Step 1: run first, see the effect, and then learn
Step 2: imitation and troubleshooting
Step 3: add new method
Step 4: Test

Step 1: run first, see the effect, and then learn

As the old rule, first download the executable items in the download area (click to enter), configure and run them, and then learn what steps to do to achieve this effect.

Step 2: imitation and troubleshooting

After making sure that the runnable project runs correctly, follow the steps of the tutorial and imitate the code again.
It is inevitable that there is code discrepancy in the simulation process, which leads to the failure to get the expected running resultsright key(can run the project) and your own code to locate the problem.
In this way,Learning is effective and troubleshooting is efficientIt can obviously improve the learning speed and cross the threshold of learning.

It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my runnable project folder.
This software is very powerful, you can know which two files in the folder are wrong, and mark them out clearly
Here is a green installation and use tutorial: diffmerge download and use tutorial

Step 3: add new method

There is nothing special about the total statistics. It is a common SQL query statement

@Select(" select count(*) from category_ ")
public int count();

package com.how2java.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.how2java.pojo.Category;
public interface CategoryMapper {

    @Insert(" insert into category_ ( name ) values (#{name}) ")
    public int add(Category category);

    @Delete(" delete from category_ where id= #{id} ")
    public void delete(int id);

    @Select("select * from category_ where id= #{id} ")
    public Category get(int id);

    @Update("update category_ set name=#{name} where id=#{id} ")
    public int update(Category category);

    @Select(" select * from category_ ")
    public List<Category> list();

    @Select(" select count(*) from category_ ")
    public int count();
}

Step 4: Test

Mybatis series (25) - related concepts - total number of queries

For more information, click to learn: https://how2j.cn/k/mybatis/mybatis-annotation-count/1104.html