熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

hibernate-HQL語句(2)

2022-06-13   來源: Java開源技術 

  修改默認查詢結果(querylist())不以Object[]數組形式返回以Set形式返回但是因為Set裡是不允許有重復的元素所以:username和password的值不能相同只需將hql改為String hql = select new set(namepasswd) from Users;

  修改默認查詢結果(querylist())不以Object[]數組形式返回以自定義類型返回

  自定義類

  Java代碼

  package comdomain;

  public class MyUser {

  private String username;

  private String password;

  //因為String hql = select new  comdomainMyUser(namepasswd) from Users;所以必須要有接受個參數的構造函數

  public MyUser(String usernameString password){

  thisusername = username;

  thispassword = password;

  }

  public String getUsername() {

  return username;

  }

  public void setUsername(String username) {

  thisusername = username;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  thispassword = password;

  }

  }

  Java代碼

  package comdomain;

  public class MyUser {

  private String username;

  private String password;

  //因為String hql = select new  comdomainMyUser(namepasswd) from Users;所以必須要有接受個參數的構造函數

  public MyUser(String usernameString password){

  thisusername = username;

  thispassword = password;

  }

  public String getUsername() {

  return username;

  }

  public void setUsername(String username) {

  thisusername = username;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  thispassword = password;

  }

  }

  Java代碼

  package comdomain;

  public class MyUser {

  private String username;

  private String password;

  //因為String hql = select new  comdomainMyUser(namepasswd) from Users;所以必須要有接受個參數的構造函數

  public MyUser(String usernameString password){

  thisusername = username;

  thispassword = password;

  }

  public String getUsername() {

  return username;

  }

  public void setUsername(String username) {

  thisusername = username;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  thispassword = password;

  }

  }

  Java代碼

  //通過querylist()出來的list裡存放的不再是默認的Object數組了而是自定義的類MyUser必須加包名String hql = from Users;中的Users類也是必須加包名的但是因為再Usershbmxml裡<hibernatemapping autoimport=true> autoimport默認值為true(所以autoimport屬性也可以不寫)自動導入了

  String hql = select new  comdomainMyUser(namepasswd) from Users;

  Query query = sessioncreateQuery(hql);

  //默認查詢出來的list裡存放的是一個Object數組但是在這裡list裡存放的不再是默認的Object數組了而是MyUser對象了

  List<MyUser> myUsers = querylist();

  for(MyUser myUser : myUsers){

  String name = myUsergetUsername();

  String passwd = myUsergetPassword();

  Systemoutprintln(name + : + passwd);

  }

  /**

  輸出結果為

  name : password

  name : password

  name : password

  */

  Java代碼

  //通過querylist()出來的list裡存放的不再是默認的Object數組了而是自定義的類MyUser必須加包名String hql = from Users;中的Users類也是必須加包名的但是因為再Usershbmxml裡<hibernatemapping autoimport=true> autoimport默認值為true(所以autoimport屬性也可以不寫)自動導入了

  String hql = select new  comdomainMyUser(namepasswd) from Users;

  Query query = sessioncreateQuery(hql);

  //默認查詢出來的list裡存放的是一個Object數組但是在這裡list裡存放的不再是默認的Object數組了而是MyUser對象了

  List<MyUser> myUsers = querylist();

  for(MyUser myUser : myUsers){

  String name = myUsergetUsername();

  String passwd = myUsergetPassword();

  Systemoutprintln(name + : + passwd);

  }

  /**

  輸出結果為

  name : password

  name : password

  name : password

  */

  Java代碼

  //通過querylist()出來的list裡存放的不再是默認的Object數組了而是自定義的類MyUser必須加包名String hql = from Users;中的Users類也是必須加包名的但是因為再Usershbmxml裡<hibernatemapping autoimport=true> autoimport默認值為true(所以autoimport屬性也可以不寫)自動導入了

  String hql = select new  comdomainMyUser(namepasswd) from Users;

  Query query = sessioncreateQuery(hql);

  //默認查詢出來的list裡存放的是一個Object數組但是在這裡list裡存放的不再是默認的Object數組了而是MyUser對象了

  List<MyUser> myUsers = querylist();

  for(MyUser myUser : myUsers){

  String name = myUsergetUsername();

  String passwd = myUsergetPassword();

  Systemoutprintln(name + : + passwd);

  }

  /**

  輸出結果為

  name : password

  name : password

  name : password

  */

  條件查詢

  Java代碼

  //條件查詢參數索引值從開始索引位置通過setStringsetParameter設置參數

  String hql = from Users where name=? and passwd=?;

  Query query = sessioncreateQuery(hql);

  //第種方式

  //      querysetString( name);

  //      querysetString( password);

  //第種方式

  querysetParameter( nameHibernateSTRING);

  querysetParameter( passwordHibernateSTRING);

  List<Users> list = querylist();

  for(Users users : list){

  Systemoutprintln(usersgetId());

  }

  Java代碼

  //條件查詢參數索引值從開始索引位置通過setStringsetParameter設置參數

  String hql = from Users where name=? and passwd=?;

  Query query = sessioncreateQuery(hql);

  //第種方式

  //      querysetString( name);

  //      querysetString( password);

  //第種方式

  querysetParameter( nameHibernateSTRING);

  querysetParameter( passwordHibernateSTRING);

  List<Users> list = querylist();

  for(Users users : list){

  Systemoutprintln(usersgetId());

  }

  Java代碼

  //條件查詢參數索引值從開始索引位置通過setStringsetParameter設置參數

  String hql = from Users where name=? and passwd=?;

  Query query = sessioncreateQuery(hql);

  //第種方式

  //      querysetString( name);

  //      querysetString( password);

  //第種方式

  querysetParameter( nameHibernateSTRING);

  querysetParameter( passwordHibernateSTRING);

  List<Users> list = querylist();

  for(Users users : list){

  Systemoutprintln(usersgetId());

  }

  Java代碼

  //條件查詢自定義索引名(參數名):username:password通過setStringsetParameter設置參數

  String hql = from Users where name=:username and passwd=:password;

  Query query = sessioncreateQuery(hql);

  //第種方式

  //      querysetString(username name);

  //      querysetString(password password);

  //第種方式個參數確定類型

  querysetParameter(username nameHibernateSTRING);

  querysetParameter(password passwordHibernateSTRING);

  List<Users> list = querylist();

  for(Users users : list){

  Systemoutprintln(usersgetId());

  }

  Java代碼

  //條件查詢自定義索引名(參數名):username:password通過setStringsetParameter設置參數

  String hql = from Users where name=:username and passwd=:password;

  Query query = sessioncreateQuery(hql);

  //第種方式

  //      querysetString(username name);

  //      querysetString(password password);

  //第種方式個參數確定類型

  querysetParameter(username nameHibernateSTRING);

  querysetParameter(password passwordHibernateSTRING);

  List<Users> list = querylist();

  for(Users users : list){

  Systemoutprintln(usersgetId());

  }

  Java代碼

  //條件查詢自定義索引名(參數名):username:password通過setStringsetParameter設置參數

  String hql = from Users where name=:username and passwd=:password;

  Query query = sessioncreateQuery(hql);

  //第種方式

  //      querysetString(username name);

  //      querysetString(password password);

  //第種方式個參數確定類型

  querysetParameter(username nameHibernateSTRING);

  querysetParameter(password passwordHibernateSTRING);

  List<Users> list = querylist();

  for(Users users : list){

  Systemoutprintln(usersgetId());

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28295.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.