本文共 1079 字,大约阅读时间需要 3 分钟。
一、SSH中纯SQL写法
/** 查询全部城市 **/
public List<City> queryCityList() {
String sql = "select c.city_id, c.city, c.city_sign, c.province_id, p.province from city AS c LEFT JOIN province AS p" +
" ON c.province_id = p.province_id where 1=1 order by c.city_sign";
SQLQuery query = this.getSession().createSQLQuery(sql);
query.setFirstResult(0);
List list = query.list();
List<City> cityList = null;
if (list != null && list.size() > 0) {
cityList = new ArrayList<City>();
for (int i = 0; i < list.size(); i++) {
Object[] objects = (Object[]) list.get(i);
City city = new City();
city.setCityId(Integer.parseInt(String.valueOf(objects[0])));
city.setCity(String.valueOf(objects[1]));
city.setCitySign(String.valueOf(objects[2]));
cityList.add(city);
}
}
return cityList;
}
二、SSH中HQL语句写法
public List<City> queryCityListTwo() {
String hql = "select new City(c.cityId,c.city,c.citySign,c.state) from City c order by c.citySign";
Query query=this.getSession().createQuery(hql);
List<City> list = query.list();
return list;
}