addEntity(Class entityClass)
引數說明:Session session = sessionFactory.openSession(); //定義Sesssion物件 String sql = "select*from tb_user"; //定義查詢tb_user表物件 List list = null; List rtnList = new ArrayList(); //定義儲存查詢結果的List物件 try{ SQLQuery query = session.createSQLQuery(sql); query.addEntity(UserForm.class); //係結實體類 list = query.list(); //執行查詢語句獲取查詢結果集 }catch(Exception e){ System.out.println("查詢使用者資訊時的錯誤資訊:"+e.getMessage()); }
addEntity(String alias,Class entityClass)
引數說明:Session session=sessionFactory.openSession(); //定義Session物件 String sql = "select{u.*}from tb_user u where u.username='wgh'"; //定義查詢語句 List list = null; List rtnList = new ArrayList(); //定義儲存查詢結果的集合物件 try{ SQLQuery query = session.createSQLQuery(sql); //執行查詢語句 query.addEntity("u",UserForm.class); //係結資料表與實體類 list = query.list();//執行查詢語句 }catch(Exception e){ System.out.println("查詢使用者資訊時的錯誤資訊:"+e.getMessage()); }
addEntity(String entityName)
引數說明:Session session = sessionFactory.openSession(); //建立Session物件 String sql = "select*from tb_user where username='sk'"; //定義查詢語句 List list = null; List rtnList = new ArrayList(); //定義儲存查詢結果的集合物件 try{ SQLQuery query = session.createSQLQuery(sql); query.addEntity("com.model.UserForm"); //係結實體類 list = query.list(); }catch(Exception e){ System.out.println("查詢使用者資訊時的錯誤資訊:"+e.getMessage()); }引數說明:
Session session = sessionFactory.openSession(); //定義Session物件 String sql = "select{u.*}from tb_user u where u.username='wgh'"; //定義查詢語句 SQLQuery query = session.createSQLQuery(sql); //執行查詢 query.addEntity("u","com.model.UserForm"); //係結引數 List list = query.list();