Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接表时正确使用BeanListHandler_Java_Apache Commons_Apache Commons Dbutils - Fatal编程技术网

Java 连接表时正确使用BeanListHandler

Java 连接表时正确使用BeanListHandler,java,apache-commons,apache-commons-dbutils,Java,Apache Commons,Apache Commons Dbutils,我正在我的简单项目中使用。我有项和人实体类(人和项作为数据库中的表)。这简化了课堂,更好地展示了我的意思。现在我需要使用获取具有登录名的项目列表。为此,我将登录属性添加到项,但这是一个丑陋的解决方案。是否有更好的方法来做到这一点,并利用其优势 公共类人物{ 私人长id; 私有字符串登录; } 公共类项目{ 私人长id; 私有字符串名称; //…项目的更多属性 private Long personId;//这是“items”表中的实列 私有字符串login;//丑陋(登录不在“items”表中,

我正在我的简单项目中使用。我有
实体类(
作为数据库中的表)。这简化了课堂,更好地展示了我的意思。现在我需要使用获取具有登录名的项目列表。为此,我将
登录
属性添加到
,但这是一个丑陋的解决方案。是否有更好的方法来做到这一点,并利用其优势

公共类人物{
私人长id;
私有字符串登录;
}
公共类项目{
私人长id;
私有字符串名称;
//…项目的更多属性
private Long personId;//这是“items”表中的实列
私有字符串login;//丑陋(登录不在“items”表中,仅针对BeanListHandler)
}
QueryRunner q=新的QueryRunner(getDataSource());
String sql=“选择i.*,p.login from items i,persons p,其中p.id=i.personId”;
listl=(List)q.query(sql,新的BeanListHandler(Item.class));
public class Person {
   private Long id;
   private String login;
}

public class Item {
   private Long id;
   private String name;
   // ... a lot more properties of item

   private Long personId;  // this is real column in "items" table
   private String login;   // UGLY (login is not in "items" table, only for BeanListHandler)
}

QueryRunner q = new QueryRunner(getDataSource());
String sql = "select i.*, p.login from items i, persons p where p.id = i.personId";
List<Item> l = (List<Item>) q.query(sql, new BeanListHandler<Item>(Item.class));