NutzCN Logo
精华 初学Nutz求教多表联查问题
发布于 2872天前 作者 qq_cf257b19 3399 次浏览 复制 上一个帖子 下一个帖子
标签:

多变联查返回多个实体对象

public List<Sys_menu> getMenusAndButtons(String userId) {
        Sql sql = Sqls.create("select distinct a.* from sys_menu a,sys_role_menu b where a.id=b.menuId and" +
                " b.roleId in(select c.roleId from sys_user_role c,sys_role d where c.roleId=d.id and c.userId=@userId and d.disabled=0) and a.disabled=0 order by a.location ASC,a.path asc");
        sql.params().set("userId", userId);
        Entity<Sys_menu> entity = dao().getEntity(Sys_menu.class);
        sql.setEntity(entity);
        sql.setCallback(Sqls.callback.entities());
        dao().execute(sql);
        return sql.getList(Sys_menu.class);
    }

我是不是应该加上Entity entity2 = dao().getEntity(Entity2.class);
sql.setEntity(entity2);
那么我的返回值应该是什么类型......
然后发回前端的只能手动把得到的实体拼接起来么

8 回复

sql里面用前缀别名,然后以record取出,record.toEntity能根据前缀拼出对象

@wendal
比如我这样
select * from tableA as a
left JOIN
tableB_info as b
on a.id=b.user_id
想返回查询结果中两表中所有字段,求教一下具体操作是什么- -
请问有demo么原来用的框架跟Nutz有很大差别,不知道从何下手

@wendal

public List<Record> getStudentList(String userId){
			Sql sql = Sqls.queryRecord("select a.*,b.name from edu_user_account a left join edu_user_basic_info b on a.id = b.user_id where a.status ='1'");
			dao().execute(sql);
			List<Record> list = sql.getList(Record.class);
			for (Record re : list) {
				Edu_user_account m = re.toEntity(dao().getEntity(Edu_user_account.class, "a.");
				}
			return  list;
		}

这么写么,提示
getEntity(Class) in Dao cannot be applied to (Class,java.lang.String)
我是要写一个 Entity getEntity(Class var1,String var2);么

re.toEntity(dao().getEntity(Edu_user_account.class), "a.");

另外, a.*不行的, 要这样

select a.id as a_id , a.age as a_age, b.name as b_name from ....

//然后 
Edu_user_account m = re.toEntity(dao().getEntity(Edu_user_account.class, "a_");
String bName = re.getString("b_name");

当然, 要是a跟b完全没有重复属性的话就不需要了, 直接

Sql sql = Sqls.queryRecord("select a.*,b.name from edu_user_account a left join edu_user_basic_info b on a.id = b.user_id where a.status ='1'");
dao().execute(sql);
Edu_user_account user = re.toEntity(dao().getEntity(Edu_user_account.class);
String bName = re.getString("name");

@wendal
对了,如果自定义sql的话 查询条件只能用sql.params.set()么,还是可以继续用cnd

@qq_cf257b19 变量可以是cnd

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