NutzCN Logo
问答 关于数据绑定多个对象!
发布于 3282天前 作者 qq_e7da0606 1894 次浏览 复制 上一个帖子 下一个帖子
标签: dao

根据之前兽哥提供过一段代码:

	Sql sql = Sqls.queryRecord("select a.*,b.* as teacher_name from users a left join teacher b on a.teacher_id =b.id");
  	dao.execute(sql);
  	Entity<User> enUser = dao.getEntity(User.class);
	  Entity<Teacher> enTeacher = dao.getEntity(Teacher.class);
  	for (Record re : sql.getList(Record.class)) {
	 User user = re.toEntity(enUser, "a.");
	  Teacher teacher = re.toEntity(enTeacher, "b.");
	 String teacherName = re.getString("b.name");
  	}

问题一:这种sql语句是a.*这种貌似映射不上。
问题二:这种如果如果a.*,和b.*有相同的字段,它会识别吗

1 回复

... 我晕,貌似我一直理解错了...

		Sql sql = Sqls.create("select o.*, u.* from t_oauth_user o left join t_user u on o.u_id = u.id limit 1");
		sql.setCallback(Sqls.callback.records());
		dao.execute(sql);
		for (Record re : sql.getList(Record.class)) {
			System.out.println(re);
			System.out.println(re.toEntity(dao.getEntity(User.class)));
			System.out.println(re.toEntity(dao.getEntity(OAuthUser.class)));
		}

输出

{
   "pvd" :"github",
   "vid" :"10126041",
   "u_id" :135,
   "ct" :"2015-10-23 17:17:56",
   "ut" :"2015-10-23 17:17:56",
   "a_url" :"https://avatars.githubusercontent.com/u/10126041?v=3",
   "id" :135,
   "name" :"dangfusheng",
   "passwd" :"f38b767b60655f17ff0a23d4779580a8440e09826a6cc745fcbbd9d844f7c3f5",
   "salt" :"80c3980d70e447529c561d827930eee8",
   "locked" :false
}
/*net.wendal.nutzbook.bean.User@141e4061*/{"id":135,"name":"dangfusheng","password":"XYZ","salt":"ABC","locked":false,"createTime":"2015-10-23 17:17:56","updateTime":"2015-10-23 17:17:56"}
/*net.wendal.nutzbook.bean.OAuthUser@5d990e*/{"providerId":"github","validatedId":"10126041","userId":135,"avatar_url":"https://avatars.githubusercontent.com/u/10126041?v=3","createTime":"2015-10-23 17:17:56","updateTime":"2015-10-23 17:17:56"}

添加回复
请先登陆
回到顶部