Hibernate 学习-2

package com.yh.hib.test;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.yh.hib.entity.Emp;
import com.yh.hib.entity.EmpCondition;
import com.yh.hib.utils.DateUtils;

public class HibernateTest {

@Test
public void selEmp() {
Configuration cfg = null;
SessionFactory sf = null;
Session session = null;
// Transaction tx=null;

String name = "王加宾";
try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = sf.openSession();
// tx=session.beginTransaction();
Query query = session
.createQuery("from Emp where job like ? and ename=:n");// ?
// 命名参数
// query.setString(0, name);
query.setParameter("n", name);
query.setParameter(0, "%保洁%");//对问号进行赋值

List<Emp> list = query.list();
// Iterator<Emp> it = query.iterate();
// while (it.hasNext()) {
// Emp emp = it.next();
// System.out.println(emp.getEname());
// }
// 以上是1+n

// Iterator<Emp> it1=query.iterate();
// while(it1.hasNext()){
// Emp emp=it1.next();
// System.out.println(emp.getEname());
// }
for (Emp emp : list) {
System.out.println(emp.getEname());
}

// List<Emp> list1=query.list();

} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}

/**
* 按主键查找
*/
@Test
public void findById() {
Configuration cfg = null;
SessionFactory sf = null;
Session session = null;

try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = sf.openSession();

// Emp emp=(Emp)session.get(Emp.class, 9999);//立即加载 ,查询不存在的时候,返回null
Emp emp = (Emp) session.load(Emp.class, 9999);// 延迟加载、懒加载:用的时候才发送sql
// .查询不存在的时候,用的时候会抛出异常
System.out.println("7369员工姓名:" + emp.getEname());

} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}

}

@Test
public void saveEmp() {
Configuration cfg = null;
SessionFactory sf = null;
Session session = null;
Transaction tx = null;

try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = sf.getCurrentSession();// 获取当前线程中的session
// ,当事务结束,session自动关闭
tx = session.beginTransaction();

Emp emp = new Emp(); // 瞬时状态:内存中存在,数据库中不存在,不在session 中
// emp.setEmpno(9995);
emp.setEname("王加宾");
emp.setJob("保洁2");

session.save(emp);
// session.update(emp);
// session.saveOrUpdate(emp);
// session.merge(emp);

tx.commit();// 持久状态:内存中存在,数据库中存在,session 存在
// session.evict(emp);
//
// emp.setJob("经理");
//
// session.update(emp);

} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();
tx.rollback();
} finally {
if (session != null && session.isOpen()) {
session.close();// 游离状态:数据库中存在,session中不存在
}
}
}

@Test
public void delEmp() {
Configuration cfg = null;
SessionFactory sf = null;
Session session = null;
Transaction tx = null;

try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = sf.getCurrentSession();// 获取当前线程中的session
// ,当事务结束,session自动关闭
tx = session.beginTransaction();

Emp emp = (Emp) session.get(Emp.class, 9999);

session.delete(emp);
tx.commit();

} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();
tx.rollback();
} finally {
if (session != null && session.isOpen()) {
session.close();// 游离状态:数据库中存在,session中不存在
}
}
}

/**
* 多条件动态查询
*/
@Test
public void selByConditions() {//通过条件查找,实现多条件查找
Configuration cfg = null;
SessionFactory sf = null;
Session session = null;

EmpCondition condition = new EmpCondition();

condition.setJob("CLERK");
condition.setSalary(1000d);
condition.setBeginDate(DateUtils.string2Date("1981-04-01"));
condition.setEndDate(DateUtils.string2Date("1985-09-09"));

String hql = "from Emp where 1=1";

if (condition.getJob() != null) {
hql += " and job=:job";//命名参数

}

if (condition.getSalary() != null) {
hql += " and sal>:salary";
}

if (condition.getBeginDate() != null) {
hql += " and hiredate>=:beginDate";
}

if (condition.getEndDate() != null) {
hql += " and hiredate<=:endDate";
}

try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = sf.openSession();

Query query = session.createQuery(hql);

query.setProperties(condition);// 以下注释代码,就是此方法的底层实现

// if(condition.getJob()!=null){
// query.setParameter("job", condition.getJob());
//
// }
//
// if(condition.getSalary()!=null){
// hql+=" and sal=:salary";
// }
//
// if(condition.getBeginDate()!=null){
// hql+=" and hiredate>=:beginDate";
// }
//
// if(condition.getEndDate()!=null){
// hql+=" and hiredate<=:endDate";
// }

List<Emp> list = query.list();
for (Emp emp : list) {
System.out.println(emp.getEname() + "*****" + emp.getJob());
}

} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();

} finally {
if (session != null && session.isOpen()) {
session.close();// 游离状态:数据库中存在,session中不存在
}
}
}

}

原文地址:https://www.cnblogs.com/jimorulang/p/5525713.html