关于hibernate查询,RowMapper的使用小结

//生成起止时间(一天)
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
Date from = c.getTime();
c.add(Calendar.DAY_OF_YEAR, 1);
Date to = c.getTime();
//这里是取当天的记录,还有一种判断写法是:to_char(scheduled_date_time, 'yyyy-MM-dd') = to_char(sysdate, 'yyyy-MM-dd'),但是这样写产生的效率没有下面的高,故优化如下
String sql = "select a.* from view_yh a where a.oper_status = ? and a.scheduled_date_time >= ? and a.scheduled_date_time <= ?  order by a.scheduled_date_time asc";
//组装rowMapper时,需要特别注意在 处,组装对象赋值时,列名不能写错,不然它会一直报告列名无效,而错误指向的是上面写的sql语句,这个问题让我郁闷了好半天
RowMapper<WSOpertInfo> rowMapper = new RowMapper<WSOpertInfo>() {

public WSOpertInfo mapRow(ResultSet rs, int arg1)
throws SQLException {
WSOpertInfo info = new WSOpertInfo();
info.setAnesthesiaAssistant(rs.getString("ANESTHESIA_ASSISTANT"));
......
return info;
}
};
List<WSOpertInfo> list = yhJdbcTemplate.query(sql, new Object[]{status, new Timestamp(from.getTime()), new Timestamp(to.getTime())}, rowMapper);
这里绿色标注的原因是因为在oracle中,时间会把秒过滤掉,这样写可以精确到秒,对于时间要求很苛刻的功能,需要特别注意下。
return list != null && list.size() > 0 ? list.toArray(new WSOpertInfo[list.size()]) : null ;

转载于:https://my.oschina.net/6tao/blog/77415

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值