package com.hibernaetSix.test;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.hibernateFive.book.Book;
import com.hibernateTwo.util.SessionFactoryUtils;
public class HqlTest {
private Session session;
private Transaction transaction;
@Before
public void before() {
session = SessionFactoryUtils.getSessionFactory();
transaction = session.beginTransaction();
}
@After
public void after() {
transaction.commit();
session.close();
}
/**
* hql查询的五种情况
* 结果处理情况一:
* 直接利用对象进行数据接受
* jdbc:操作是数据库表
* select * from t_gibernate_book
* list<Book> list = this.bookDao.list.executeQuery(sql,pageBean)
*
*
*
* 获取类对象的三种方法 Class clz=Book.class;
* while(rs.next){
* Book b = clz.new Instance();
* Field[] fields = clz.getDecaredecarldFields();
* for(Filed f:fields){
* f.set(b,rs.getObject);
* }
* list.add(b);
* }
*
*
*
*/
@Test
public void testList1() {
String hql = "from Book";
// Qurey --> preparestatement
List<Book> query = session.createQuery(hql).list();
for (Book book : query) {
System.out.println(book);
}
}
/**
* hql查询的五种情况
* 结果处理情况二:
* 查一列
* jdbc:查的是表中的某列,列名不区分打小写
* hql:查的是实体类的某一属性,严格区分打小写,实体类是大写这里就是大写如果是小写这里也是小写
*/
@Test
public void testList2() {
String hql = "select bookName from Book";
List<String> list = session.createQuery(hql).list();
for (String bname : list) {
System.out.println(bname);
}
}
/**
* hql查询的五种情况
* 结果处理情况三:
* 查两列
* Object[]数值接受
* Class
* Book
* mvc --> EntityBaseDao.insert(sql,keys,Book book)
*
* 类 8
* 实体类 8
*
* Book book传值六个
*
*
*/
@Test
public void testList3() {
String hql = "select bookId,bookName from Book";
List<Object[]> list = session.createQuery(hql).list();
for (Object[] book : list) {
System.out.println(Arrays.toString(book));
}
}
/**
* hql查询的五种情况
* 结果处理情况四:
* 利用hibernate内置的函数进行处理
* 很多人都认为Map是个接口
* 但在Hibernate里面是内置函数
* 如果map是类的话就查不出来
* 在这里的map不就像jdbc里的函数不区分大小写
*/
@Test
public void testList4() {
String hql = "select new map(bookId,bookName) from Book";
List<Map> list = session.createQuery(hql).list();
for (Map book : list) {
System.out.println(book);
}
}
/**
* hql查询的五种情况
* 结果处理情况五:
* 利用构造反方进行结果处理
* Book里面写了哪些属性就在实体类里提供哪些属性的构造方法
* 1.当你给某些一个类提供有参的构造器,顺便加上无参构造器
* 2.构造方法赋值的原理
* resultset
* Class clz = Book.class
* construct c = clz.getDecarldconstruct()
* 获取有参的构造器中的参数
* Field[] fields = clz.getDecaredecarldFields();
* for(Filed f:fields){
* f.set(b,rs.getObject(1));
* }
* list.add(c.newInstance(?,?));
*/
@Test
public void testList5() {
String hql = "select new Book(bookId,bookName) from Book";
List<Book> list = session.createQuery(hql).list();
for (Book book : list) {
System.out.println(book);
}
}
/**
* hql中的站位符
* jdbc:select * from t_hibernate_book where bid = ?
* hql:1 from Book where bookId = ?
* hql:2 from Book where bookId:bookId(可以自己命名)
*/
@Test
public void testList6() {
/*String hql = "from Book where bookId = :bookId";
Query query = session.createQuery(hql);
query.setParameter("bookId", 1);
Book book = (Book) query.getSingleResult();
System.out.println(book);*/
String hql = "from Book where bookName like :bookName";
Query query = session.createQuery(hql);
query.setParameter("bookName", "%三%");
Book book = (Book) query.getSingleResult();
System.out.println(book);
}
/**
* 分页
* .setFirstResult(0) 从第几条开始
* .setMaxResults(5) 到第几条结束
* .list()接后
* @return
*/
@Test
public void test() {
String hql = "from Book";
int rows = 3;
int page = 1;
List<Book> list = session.createQuery(hql).setFirstResult((page-1)*rows).setMaxResults(rows).list();
for (Book book : list) {
System.out.println(book);
}
}
}
Hibernate的Hql的五种查询
最新推荐文章于 2022-01-02 11:00:00 发布