hibernate-HQL語句(2)

火星人 @ 2014-03-09 , reply:0
←手機掃碼閱讀

  6.修改默認查詢結果(query.list())不以Object[]數組形式返回,以Set形式返回,但是因為Set里是不允許有重複的元素,:username和password的值不能相同.只需將hql改為:String hql = " select new set(name,passwd) from Users";

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

  自定義類:

  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;

  }

  }

  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;

  }

  }

  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;

  }

  }

  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

  */

  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

  */

  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

  */

  8:條件查詢

  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());

  }

  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());

  }

  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());

  }

  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());

  }

  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());

  }

  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());

  }





[火星人 via ] hibernate-HQL語句(2)已經有268次圍觀

http://www.coctec.com/docs/java/show-post-60254.html