1. 程式人生 > >原生SQL組建查詢

原生SQL組建查詢

String sql = "SELECT gather.dwid,gather.dwname,recorditem.unit,record.id equiprecordid,record.equipname FROM eqm_equipgather_item gather LEFT JOIN eqm_equiprecord_item recorditem ON gather.equiprecorditemid =recorditem.id LEFT JOIN eqm_equiprecord record ON recorditem.equiprecordid =record.id WHERE record.equipmodelid = "
								+ equipmodelid + " AND gather.dwname LIKE '%" + displayconfig.getDisplayname()
								+ "%' AND gather.tenantid = " + tenantid + "";

						Query query = em.createNativeQuery(sql);
						query.unwrap(SQLQuery.class).addScalar("dwid", StandardBasicTypes.STRING)
								.addScalar("dwname", StandardBasicTypes.STRING)
								.addScalar("equiprecordid", StandardBasicTypes.LONG)
								.addScalar("unit", StandardBasicTypes.STRING)
								.addScalar("equipname", StandardBasicTypes.STRING)
								.setResultTransformer(Transformers.aliasToBean(EquipgatheritemVo.class));
						List<EquipgatheritemVo> gatherList = query.getResultList();