关于数据表的查询和取值

shenletongxue 2013-05-10 11:35:54
ssh框架,model里有Result和Count两个不同的类
public class Result {
private int day;
private int month;
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
}
public class Count {
private int id;
private String indate;
public String getIndate() {
return indate;
}
public void setIndate(String indate) {
this.indate = indate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Dao的实现类中有如下代码:
...
List<Result> list=(List<Result>)hibernateTemplate.find("select day(indate),month(indate) from com.chart.model.Count group by day(indate)");
System.out.println(list.get(0).getDay());<==========打印不出来
...
为什么打印不出来呢?
list里面有值,是不是因为不能转换成Result类型,改怎么改呢?
...全文
108 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuqi_hz 2013-05-16
  • 打赏
  • 举报
回复
"select day(indate),month(indate) from com.chart.model.Count group by day(indate)") 用这个sql语句,怎么能确定生成的List<Result>,Result的day是被赋值了day(indate),month是被赋值了 month(indate)??????????????
shenletongxue 2013-05-16
  • 打赏
  • 举报
回复
引用 3 楼 q35335010 的回复:
[quote=引用 楼主 shenletongxue 的回复:] ssh框架,model里有Result和Count两个不同的类 public class Result { private int day; private int month; public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } } public class Count { private int id; private String indate; public String getIndate() { return indate; } public void setIndate(String indate) { this.indate = indate; } public int getId() { return id; } public void setId(int id) { this.id = id; } } Dao的实现类中有如下代码: ... List<Result> list=(List<Result>)hibernateTemplate.find("select day(indate),month(indate) from com.chart.model.Count group by day(indate)"); System.out.println(list.get(0).getDay());<==========打印不出来 ... 为什么打印不出来呢? list里面有值,是不是因为不能转换成Result类型,改怎么改呢?
select day(indate),month(indate) from com.chart.model.Count group by day(indate)"); 你这hql怎么看起来挺奇怪啊,day(indate),这个是什么,你查的是Count表,怎么返回的是Result 看不懂你的意思[/quote] day(indate)是取日期中的“日”,我查的是Count,但是我想把结果转换成Result类型的,上面的代码转不过去,想知道怎么才能转过去呢~
q35335010 2013-05-15
  • 打赏
  • 举报
回复
引用 楼主 shenletongxue 的回复:
ssh框架,model里有Result和Count两个不同的类 public class Result { private int day; private int month; public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } } public class Count { private int id; private String indate; public String getIndate() { return indate; } public void setIndate(String indate) { this.indate = indate; } public int getId() { return id; } public void setId(int id) { this.id = id; } } Dao的实现类中有如下代码: ... List<Result> list=(List<Result>)hibernateTemplate.find("select day(indate),month(indate) from com.chart.model.Count group by day(indate)"); System.out.println(list.get(0).getDay());<==========打印不出来 ... 为什么打印不出来呢? list里面有值,是不是因为不能转换成Result类型,改怎么改呢?
select day(indate),month(indate) from com.chart.model.Count group by day(indate)"); 你这hql怎么看起来挺奇怪啊,day(indate),这个是什么,你查的是Count表,怎么返回的是Result 看不懂你的意思
rencht 2013-05-15
  • 打赏
  • 举报
回复
你直接去数据库执行你的sql语句看看能不能查询出结果; 还有既然用了hibernate何必还使用原生的sql呢,用hql或是Criteria吧
shenletongxue 2013-05-15
  • 打赏
  • 举报
回复
有没有人知道呢?
alvin_1989 2013-05-15
  • 打赏
  • 举报
回复
lz,spring中这样查出来的是list里面放置的是map类型。你确定你这样写list.get(0).getDay()不报错? 你这样写list.get(0).get("day")试试。
wyx100 2013-05-15
  • 打赏
  • 举报
回复
回复于: 2013-05-15 11:31:19 你直接去数据库执行你的sql语句看看能不能查询出结果; 还有既然用了hibernate何必还使用原生的sql呢,用hql或是Criteria吧

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧