public class testDemo {
@Test
public void test15() {
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
//内连接
/* Query query = session.createQuery("from Student s inner join s.orders");
List<Object[]> list = query.list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}*/
//迫切内连接
Query query = session.createQuery("select distinct s from Student s inner join fetch s.orders");
List list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
/**
* 模糊查询
*/
@Test
public void test14() {
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
//HQL模糊查询
/* Query query = session.createQuery("from Student where sname like ?");
query.setParameter(0, "赵%");
List<Student> list = query.list();
System.out.println(list);*/
//QBC模糊查询
Criteria criteria = session.createCriteria(Student.class);
criteria.add(Restrictions.like("sname", "赵%"));
List<Student> list = criteria.list();
System.out.println(list);
tx.commit();
session.close();
}
/**
* 投影操作:查询对象的某几个属性
*
*
*/
@Test
public void test13() {
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
//查询多个就要加[]了必须用循环 输出
/* List<Object[]> list = session.createQuery("select s.sname,s.stuid from Student s").list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
*/
//必须有构造函数只查sname就必须添加一个只有name的构造函数
List<Student> list = session.createQuery("select new Student(sname) from Student").list();
System.out.println(list);
tx.commit();
session.close();
}
@Test
public void test12() {
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
//查询用户名字 List<>泛型中不管有什么都可以用Object Object超级父类
List<Object> list = session.createQuery("select s.sname from Student s").list();
System.out.println(list);
tx.commit();
session.close();
}
@Test
public void test11() {
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Student where sname=:name and stuid=:id");
//这里的name对应着HQL语句:后的name
query.setString("name", "赵云");
query.setInteger("id", 4);
List<Student> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
@Test
public void test10() {
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Student where sname=? and stuid=?");
query.setString(0, "赵云");
query.setInteger(1, 4);
List<Student> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
@Test
public void test9() {
//QBC检索一条数据 .uniqueResult()是只查一条语句如果数据库查出了多条语句会报错
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
Student student = (Student) session.createCriteria(Student.class)
.add(Restrictions.eq("sname", "皇子")).uniqueResult();
System.out.println(student);
tx.commit();
session.close();
}
@Test
public void test8() {
//HQL检索一条数据 .uniqueResult()是只查一条语句如果数据库查出了多条语句会报错
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
Student student = (Student) session.createQuery("from Student where sname=?")
.setString(0, "赵云").uniqueResult();
System.out.println(student);
tx.commit();
session.close();
}
@Test
public void test7() {
//用QBC实现分页
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Orders.class);
//setFirstResult()设置参数表示下标 从0开始 表示第一个数据 从第几条数据开始
criteria.setFirstResult(0);
//setMaxResults()设置每页显示的数量
criteria.setMaxResults(10);
List<Orders> list = criteria.list();
for (Orders orders : list) {
System.out.println(orders);
}
tx.commit();
session.close();
}
@Test
public void test6() {
//用HQL实现分页
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Orders");
//setFirstResult()设置参数表示下标 从0开始 表示第一个数据 从第几条数据开始
query.setFirstResult(0);
//setMaxResults()设置每页显示的数量
query.setMaxResults(10);
List<Orders> list = query.list();
for (Orders orders : list) {
System.out.println(orders);
}
tx.commit();
session.close();
}
@Test
public void test5() {
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
/* List<Student> list = session.createQuery("from Student s order by s.stuid desc").list();
for (Student student : list) {
System.out.println(student);
}*/
//使用QBC进行排序
List<Student> list = session.createCriteria(Student.class)
.addOrder(org.hibernate.criterion.Order.desc("stuid"))
.list();
for (Student student : list) {
System.out.println(student);
}
tx.commit();
session.close();
}
/**
* 排序 使用HQL操作
* 默认升序ASC 降序desc
* 使用QBC进行排序 固定写法
* .addOrder(org.hibernate.criterion.Order.desc("stuid"))
* .desc("字段")根据这个字段排序
* .asc()正序
*/
@Test
public void test4() {
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
tx.commit();
session.close();
}
/**
* 多态检索
* 因为session.createQuery("from java.lang.Object").list();查询的是object对象
* <mapping resource="com/bdqn/pojo/Orders.hbm.xml" />
* <mapping resource="com/bdqn/pojo/Student.hbm.xml" />
* 会返回所有对象
*/
@Test
public void test3() {
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
List<Object> list = session.createQuery("from java.lang.Object").list();
for (Object object : list) {
System.out.println(object);
}
tx.commit();
session.close();
}
@Test
public void test2(){
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
//使用别名查询
/* List<Student> list = session.createQuery("from Student s").list();
System.out.println(list);*/
//使用别名带参数查询
List<Student> list = session.createQuery("from Student s where s.sname=?").setString(0, "赵云").list();
System.out.println(list);
tx.commit();
session.close();
}
@Test
public void test1(){
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
//使用HQL查询
/* List<Student> list = session.createQuery("from Student").list();
for (Student student : list) {
System.out.println(student);
}*/
//使用QBC查询
/* List<Student> list = session.createCriteria(Student.class).list();
for (Student student : list) {
System.out.println(student);
}*/
//使用sql查询
List<Student> list = session.createSQLQuery("select * from Student").addEntity(Student.class).list();
for (Student student : list) {
System.out.println(student);
}
tx.commit();
session.close();
}
}