初学 MyBatis Plus API 有个问题想请教大佬,谢谢!

可以只查询一列的字段?

比如 有一个学生类 : Student

有没有只查询所有id 返回 直接返回List<Integer> ===> 1,2,3,4 吗 ?

而不是List<Student> ====> ["id":1,name:null,age:0]

 

 

你直接select id from student   不就行了       然后返回类型用List<Integer>

可以,单独写 sql,再mapper.xml中 自定义sql

mybatis plus 查询指定了只一列字段,最后返回一列属性填充的实体对象结果集,

结果集中的对象,没有被填充的属性,具有默认值,会有比直接返回消耗资源要高吗?

List<Integer> list ==> 1,2,3,4

List<Student> list ==> {id:1,name:null,age:0},{id:2,name:null,age:0},{id:3,name:null,age:0}

parameterType='Integer'

我把问题想太复杂了,我以为有什么mybatis plus api 可以直接这样搞

###

注意:resultType="java.lang.Integer"

xml 中定义 sql语句

<select id="getIdList" resultType="java.lang.Integer">
        select id from t_detection_host
    </select>

dao中 写接口


public interface DetectionHostDao extends BaseMapper<DetectionHostEntity> {

    List<Integer> getIdList();

}

 

感谢老哥

mybatis plus框架的初衷就是减少简单查询的xml配置,全部实现自动。你可以看看官方api提供的条件构造器中的select()方法,指定查询字段

感谢,但是它返回的是实体对象

比如student 对象只要id

 

使用select("id") 后==》student[id:1,name:null,,,,,,,,,,,,,,,,,,] 带了太多尾巴了

哦,不好意思,那天看错啦,这种也是可以实现的。

通过条件构造器,和select()方法返回指定字段,配合List<Object> selectObjs(....);不过这个方法只能返回一个字段类型的值集合