特殊查询

addColumn方法

addColumn方法用于指定返回的列信息,即select {{column}} from table 中的column部分. 基于此,addColumn的用法非常多样,同时需要调用对应的Response接口里面定义的方法.

Person实体类

  1. public class Person {
  2. @Id(strategy = IdStrategy.AutoIncrement)
  3. private long id;
  4. private String username;
  5. private String password;
  6. private int type;
  7. }

返回单属性

  1. //select id from person 返回第一行的id属性
  2. String id = (String)dao.query(Person.class)
  3. .addColumn("id")
  4. .execute()
  5. .getSingleColumn(String.class);

返回单列

  1. //select id from person
  2. List<Long> ids = dao.query(Person.class)
  3. .addColumn("id")
  4. .execute()
  5. .getSingleColumnList(Long.class);

返回部分属性

  1. //select username,password from person
  2. List<Person> personList = dao.query(Person.class)
  3. .addColumn("username","password")
  4. .execute()
  5. .getList();

分组聚合查询

  1. //select COUNT(ID) as count,max(id) as \"M(ID)\" from person group by id having count(id) = 1 order by max(id)
  2. JSONArray array = dao.query(Person.class)
  3. .addColumn("count(id) as count")
  4. .addColumn("max(id) as \"M(ID)\"")
  5. .groupBy("id")
  6. .having("count(id) = 1",null)
  7. .orderByDesc("max(id)")
  8. .execute()
  9. .getArray();