1:原生sql的查询,返回结果设置到DTO:
Query query = sessionFactoryRtData.getCurrentSession().createSQLQuery(hql.toString()).setResultTransformer(Transformers.aliasToBean(RTDataAttrDto.class));
2:hql查询,返回结果到DTO
"select new com.waternet.datacenter.dto.warningData.AlarmDataDto( id,alarmSettingId,deviceId,deviceName,enName,chName,dataTypeId,dataTypeName,alarmTypeId,alarmTypeName, "+
" alarmLevel,firstWarningValue,lastWarningValue,limitValue,startTime,lastWarningTime,clearTime,alarmState, "+
" alarmText,alarmTimes,createTime,lastModifiedTime,creator,lastModifier,useState ) from AlarmDataBean as ad
对应dto 一定要写对应的构造函数;参数个数要与hql里的查询结果一一对应,且字段名,类型要对应好。
如:
public AlarmDataDto( String id, String alarmSettingId, int deviceId, String deviceName, String enName, String chName,String dataTypeId, String dataTypeName,
String alarmTypeId, String alarmTypeName, int alarmLevel, String firstWarningValue, String lastWarningValue, String limitValue,
Date startTime,Date lastWarningTime, Date clearTime, int alarmState, String alarmText, int alarmTimes,
Date createTime, Date lastModifiedTime, String creator,String lastModifier, int useState ) { //全参构造器
super();
this.id =id ;
this.alarmSettingId =alarmS
...} 其中日期一般用 Date 类型。