前言
每次写标量查询都要百度一波,索性记录下来。
标量查询
- 菜单实体
public class Menu {
private String id;
private String name;
private String code;
private Float showOrder;
/* getter/setter */
}
- 查询方法
public List<Menu> listMenus(String code) {
String sql = "select id, name, code, showOrder from menu where code=:code";
Session session = super.getSession();
SQLQuery query = session.createSQLQuery(sql);
query.addScalar("id", Hibernate.STRING);
query.addScalar("name", Hibernate.STRING);
query.addScalar("code", Hibernate.STRING);
query.addScalar("code", Hibernate.FLOAT);
query.setResultTransformer(Transformers.aliasToBean(Menu.class));
query.setParameter("code", code);
return query.list();
}
总结
未完黛旭