1. 查詢整個映射對象全部字段 java
Java代碼
- //直接from查詢出來的是一個映射對象,即:查詢整個映射對象全部字段
- String hql = "from Users";
- Query query = session.createQuery(hql);
-
- List<Users> users = query.list();
- for(Users user : users){
- System.out.println(user.getName() + " : " + user.getPasswd() + " : " + user.getId());
- }
-
- 輸出結果爲:
- name1 : password1 : 1
- name2 : password2 : 2
- name3 : password3 : 3
- //直接from查詢出來的是一個映射對象,即:查詢整個映射對象全部字段
- String hql = "from Users";
- Query query = session.createQuery(hql);
-
- List<Users> users = query.list();
- for(Users user : users){
- System.out.println(user.getName() + " : " + user.getPasswd() + " : " + user.getId());
- }
-
- 輸出結果爲:
- name1 : password1 : 1
- name2 : password2 : 2
- name3 : password3 : 3
2.查詢單個字段 數組
Java代碼
- //查詢單個字段
- String hql = " select name from Users";
- Query query = session.createQuery(hql);
-
- List<String> list = query.list();
- for(String str : list){
- System.out.println(str);
- }
- 輸出結果爲:
- name1
- name2
- name3
- //查詢單個字段
- String hql = " select name from Users";
- Query query = session.createQuery(hql);
-
- List<String> list = query.list();
- for(String str : list){
- System.out.println(str);
- }
- 輸出結果爲:
- name1
- name2
- name3
3.查詢其中幾個字段
session
Java代碼
- //查詢其中幾個字段
- String hql = " select name,passwd from Users";
- Query query = session.createQuery(hql);
- //默認查詢出來的list裏存放的是一個Object數組
- List<Object[]> list = query.list();
- for(Object[] object : list){
- String name = (String)object[0];
- String passwd = (String)object[1];
-
- System.out.println(name + " : " + passwd);
- }
- 輸出結果爲:
- name1 : password1
- name2 : password2
- name3 : password3
- //查詢其中幾個字段
- String hql = " select name,passwd from Users";
- Query query = session.createQuery(hql);
- //默認查詢出來的list裏存放的是一個Object數組
- List<Object[]> list = query.list();
- for(Object[] object : list){
- String name = (String)object[0];
- String passwd = (String)object[1];
-
- System.out.println(name + " : " + passwd);
- }
- 輸出結果爲:
- name1 : password1
- name2 : password2
- name3 : password3
4.修改默認查詢結果(query.list())不以Object[]數組形式返回,以List形式返回 app
Java代碼 dom
//查詢其中幾個字段,添加new list(),注意list裏的l是小寫的。也不須要導入包,這樣經過query.list()出來的list裏存放的再也不是默認的Object數組了,而是List集合了
String hql = " select new list(name,passwd) from Users";
Query query = session.createQuery(hql);
//默認查詢出來的list裏存放的是一個Object數組,可是在這裏list裏存放的再也不是默認的Object數組了,而是List集合了
List<List> list = query.list();
for(List user : list){
String name = (String)user.get(0);
String passwd = (String)user.get(1);
System.out.println(name + " : " + passwd);
}
/**
輸出結果爲:
name1 : password1
name2 : password2
name3 : password3
*/
5.修改默認查詢結果(query.list())不以Object[]數組形式返回,以Map形式返回 函數
Java代碼
- //查詢其中幾個字段,添加new map(),注意map裏的m是小寫的。也不須要導入包,這樣經過query.list()出來的list裏存放的再也不是默認的Object數組了,而是map集合了
- String hql = " select new map(name,passwd) from Users";
- Query query = session.createQuery(hql);
- //默認查詢出來的list裏存放的是一個Object數組,可是在這裏list裏存放的再也不是默認的Object數組了,而是Map集合了
- List<Map> list = query.list();
- for(Map user : list){
- //一條記錄裏全部的字段值都是map裏的一個元素,key是字符串0,1,2,3....,value是字段值
- //若是將hql改成:String hql = " select new map(name as username,passwd as password) from Users";,那麼key將不是字符串0,1,2...了,而是"username","password"了
- String name = (String)user.get("0");//get("0");是get(key),注意:0,1,2...是字符串,而不是整形
- String passwd = (String)user.get("1");
-
- System.out.println(name + " : " + passwd);
- }
- /**
- 輸出結果爲:
- name1 : password1
- name2 : password2
- name3 : password3
- */
- //查詢其中幾個字段,添加new map(),注意map裏的m是小寫的。也不須要導入包,這樣經過query.list()出來的list裏存放的再也不是默認的Object數組了,而是map集合了
- String hql = " select new map(name,passwd) from Users";
- Query query = session.createQuery(hql);
- //默認查詢出來的list裏存放的是一個Object數組,可是在這裏list裏存放的再也不是默認的Object數組了,而是Map集合了
- List<Map> list = query.list();
- for(Map user : list){
- //一條記錄裏全部的字段值都是map裏的一個元素,key是字符串0,1,2,3....,value是字段值
- //若是將hql改成:String hql = " select new map(name as username,passwd as password) from Users";,那麼key將不是字符串0,1,2...了,而是"username","password"了
- String name = (String)user.get("0");//get("0");是get(key),注意:0,1,2...是字符串,而不是整形
- String passwd = (String)user.get("1");
-
- System.out.println(name + " : " + passwd);
- }
- /**
- 輸出結果爲:
- name1 : password1
- name2 : password2
- name3 : password3
- */
6.修改默認查詢結果(query.list())不以Object[]數組形式返回,以Set形式返回,可是由於Set裏是不容許有重複的元素,因此:username和password的值不能相同。只需將hql改成:String hql = " select new set(name,passwd) from Users"; this
7.修改默認查詢結果(query.list())不以Object[]數組形式返回,以自定義類型返回
自定義類:
spa
Java代碼
- package com.domain;
-
- public class MyUser {
-
- private String username;
- private String password;
- //由於:String hql = " select new com.domain.MyUser(name,passwd) from Users";因此必需要有接受2個參數的構造函數
- public MyUser(String username,String password){
- this.username = username;
- this.password = password;
- }
-
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
-
-
- }
- package com.domain;
-
- public class MyUser {
-
- private String username;
- private String password;
- //由於:String hql = " select new com.domain.MyUser(name,passwd) from Users";因此必需要有接受2個參數的構造函數
- public MyUser(String username,String password){
- this.username = username;
- this.password = password;
- }
-
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
-
-
- }
Java代碼
- //經過query.list()出來的list裏存放的再也不是默認的Object數組了,而是自定義的類MyUser,必須加包名,String hql = "from Users";中的Users類也是必須加包名的,可是由於再Users.hbm.xml裏<hibernate-mapping auto-import="true"> auto-import默認值爲true(因此auto-import屬性也能夠不寫),自動導入了
- String hql = " select new com.domain.MyUser(name,passwd) from Users";
- Query query = session.createQuery(hql);
- //默認查詢出來的list裏存放的是一個Object數組,可是在這裏list裏存放的再也不是默認的Object數組了,而是MyUser對象了
- List<MyUser> myUsers = query.list();
- for(MyUser myUser : myUsers){
- String name = myUser.getUsername();
- String passwd = myUser.getPassword();
- System.out.println(name + " : " + passwd);
- }
- /**
- 輸出結果爲:
- name1 : password1
- name2 : password2
- name3 : password3
- */
- //經過query.list()出來的list裏存放的再也不是默認的Object數組了,而是自定義的類MyUser,必須加包名,String hql = "from Users";中的Users類也是必須加包名的,可是由於再Users.hbm.xml裏<hibernate-mapping auto-import="true"> auto-import默認值爲true(因此auto-import屬性也能夠不寫),自動導入了
- String hql = " select new com.domain.MyUser(name,passwd) from Users";
- Query query = session.createQuery(hql);
- //默認查詢出來的list裏存放的是一個Object數組,可是在這裏list裏存放的再也不是默認的Object數組了,而是MyUser對象了
- List<MyUser> myUsers = query.list();
- for(MyUser myUser : myUsers){
- String name = myUser.getUsername();
- String passwd = myUser.getPassword();
- System.out.println(name + " : " + passwd);
- }
- /**
- 輸出結果爲:
- name1 : password1
- name2 : password2
- name3 : password3
- */
8:條件查詢 .net
Java代碼
- //條件查詢,參數索引值從0開始,索引位置。經過setString,setParameter設置參數
- String hql = "from Users where name=? and passwd=?";
- Query query = session.createQuery(hql);
- //第1種方式
- // query.setString(0, "name1");
- // query.setString(1, "password1");
- //第2種方式
- query.setParameter(0, "name1",Hibernate.STRING);
- query.setParameter(1, "password1",Hibernate.STRING);
- List<Users> list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }
- //條件查詢,參數索引值從0開始,索引位置。經過setString,setParameter設置參數
- String hql = "from Users where name=? and passwd=?";
- Query query = session.createQuery(hql);
- //第1種方式
- // query.setString(0, "name1");
- // query.setString(1, "password1");
- //第2種方式
- query.setParameter(0, "name1",Hibernate.STRING);
- query.setParameter(1, "password1",Hibernate.STRING);
- List<Users> list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }
Java代碼
- //條件查詢,自定義索引名(參數名):username,:password.經過setString,setParameter設置參數
- String hql = "from Users where name=:username and passwd=:password";
- Query query = session.createQuery(hql);
- //第1種方式
- // query.setString("username", "name1");
- // query.setString("password", "password1");
- //第2種方式,第3個參數肯定類型
- query.setParameter("username", "name1",Hibernate.STRING);
- query.setParameter("password", "password1",Hibernate.STRING);
- List<Users> list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }
- //條件查詢,自定義索引名(參數名):username,:password.經過setString,setParameter設置參數
- String hql = "from Users where name=:username and passwd=:password";
- Query query = session.createQuery(hql);
- //第1種方式
- // query.setString("username", "name1");
- // query.setString("password", "password1");
- //第2種方式,第3個參數肯定類型
- query.setParameter("username", "name1",Hibernate.STRING);
- query.setParameter("password", "password1",Hibernate.STRING);
- List<Users> list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }
Java代碼
- //條件查詢,經過setProperties設置參數
- String hql = "from Users where name=:username and passwd=:password";
- Query query = session.createQuery(hql);
- //MyUser類的2個屬性必須和:username和:password對應
- MyUser myUser = new MyUser("name1","password1");
- query.setProperties(myUser);
- List<Users> list = query.list();
- for(Users users : list){
- System.out.println(users.getId());
- }