一、SSH中纯SQL写法

/** 查询全部城市 **/

public List 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 cityList = null;

if (list != null && list.size() > 0) {

cityList = new ArrayList();

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 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 list = query.list();

return list;

}