案例:
SELECT (@num:=@num+1) AS `index`, name, age
FROM sys_user, (SELECT @num:=0) AS a order by age,name;
查询结果:
datagrip和dbeaver查询结果不一样,使用cmd查询,默认也是无小数点的
但实际返回结果,其实是带点的
@Data
public class SysUserDTO {
// private Integer id;
private String id;
// private Integer index;
private String index;
private String name;
private Integer age;
}
@Test
void queryWithIndex() {
List<SysUserDTO> userDTOS = userMapper.queryWithIndex();
log.info("userDTOS:{}",userDTOS);
}
<select id="queryWithIndex" resultType="com.bootmaven.vo.SysUserDTO">
SELECT (@num:=@num+1) AS `index`, name, age,id
FROM sys_user, (SELECT @num:=0) AS a order by age,name
</select>
根据返回类型,mybatis查询有两种不同结果:
userDTOS:[
SysUserDTO(id=1, index=1, name=Jone, age=18),
SysUserDTO(id=2, index=2, name=Jack, age=20),
SysUserDTO(id=4, index=3, name=Sandy, age=21),
SysUserDTO(id=5, index=4, name=Billie, age=24),
SysUserDTO(id=3, index=5, name=Tom, age=28)]userDTOS:[
SysUserDTO(id=1, index=1.0, name=Jone, age=18),
SysUserDTO(id=2, index=2.0, name=Jack, age=20),
SysUserDTO(id=4, index=3.0, name=Sandy, age=21),
SysUserDTO(id=5, index=4.0, name=Billie, age=24),
SysUserDTO(id=3, index=5.0, name=Tom, age=28)]
上面是Integer接收index,下面为String接收index
即:mysql默认返回的下标其实是带小数点的。(多列带点,单列不带)
参考:MySQL给查询加序号