lambdaQueryWrapper及动态获取字段名

实体对象

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

@Data
@Table("")
public class User {
    private Long id;

    @TableField("t_name")
    private String name;

    @TableField("t_age")
    private Integer age;

    private String email;
}

查询

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public void queryUsers() {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(User::getAge, 25);

        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }
}
posted @ 2024-08-30 16:02  SpecialSpeculator  阅读(73)  评论(0编辑  收藏  举报