Skip to content

查询(query)

Mapper 内置方法

  • getById(id):根据ID查询
  • getById(id,selectFields):根据ID查询,可选择部分列
  • get(Where):动态条件,单个查询

ID 查询

java
public class Demo {
    @Autowired
    private SysUserMapper sysUserMapper;
    
    public void getById() {
       SysUser sysUser= sysUserMapper.getById(1);
    }
}

根据ID查询,可选择部分列

java
public class Demo {
    @Autowired
    private SysUserMapper sysUserMapper;
    
    public void getById() {
       SysUser sysUser= sysUserMapper.getById(1,SysUser::getId,SysUser::getUserName);
    }
}

动态where查询

java
public class Demo {
    @Autowired
    private SysUserMapper sysUserMapper;
    
    public void getById() {
       SysUser sysUser= sysUserMapper.get(where->{
           where.eq(SysUser::getId,1);
       });
    }
}

基于QueryChain(强大)

可构建复杂的场景, 更多操作前往 链路查询 - QueryChain

java
public class Demo {
    @Autowired
    private SysUserMapper sysUserMapper;

    public void update() {
        SysUser sysUser= QueryChain.of(sysUserMapper)
                .eq(SysUser::getId, 1)
                .get();
    }
}