SoFunction
Updated on 2025-03-08

Query specified fields in Mybatis-Plus

Mybatis-Plus query for specified fields

select word from addition;
public void test3(){
    LambdaQueryWrapper<Addition> queryWrapper = new LambdaQueryWrapper<>();
    (Addition::getWord);
     //Method 1: Operation through stream    List<String> list = (queryWrapper).stream().map(Addition::getWord).collect(());
    //Method 2: listObjs    List<Object> list1 = (queryWrapper);
}
  • The SQL statement executed during method one query is:
select * from addition;
  • The SQL statement executed by method two is: more efficient.
select word from addition;

Mybatis-Plus only has three ways to query some fields

Method 1

Just query the name and phone fields: use the select() method of queryWrapper to specify the field to query

public void selectByWrapper1() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        ("name", "phone").eq("age",25);
        List<User> users = (queryWrapper);
        (::println);
    }

This method causes the value of the field except "name" to be null

Method 2

Query the data of all fields except age and address: Use the select() method of queryWrapper

public void selectByWrapper2() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        (, info -> !().equals("age")
                && !().equals("address")).eq("name","jack");
        List<User> users = (queryWrapper);
        (::println);
    }

This method will cause the value of the field except "age" and "address" to be null

Method 3

selectMaps

public void selectMaps2(){
        QueryWrapper<User> userQueryWrapper = ();

        ("avg(age) avg_age" , "min(age) min_age" , "max(age) max_age")
                        .groupBy("age")
                        .having("sum(age) < {0}" , 60);
        List<Map<String , Object>> mapList = (userQueryWrapper);
        (::println);
    }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.