一,Hibernate API
Configuration
SessionFactory
Session
Transaction
Query
二,知識點
1.讀取並解析配置文件及映射文件
Configuration conf=new Configuration().configure();
2.依據配置文件和映射文件的信息,創建SessionFactory
SessionFactory sf=conf.buildSessionFactory();
3.打開Session 有2種方法
1)session=sf.openSession(); //結束時 需要關閉session 資源 session.close();
2)session=sf.getCurrentSession(); //結束時,不需要關閉session 資源
4.事務
tx=session.beginTransaction(); //開始一個事務
tx.commit(); //提交事務
tx.rollback(); //事務回滾
5.主鍵生成策略
mysql或sqlserver數據庫自動增長:主鍵 identity,數據庫自動增長;主鍵 assigned ,添加記錄時,需要自己添加ID。
6.數據庫操作
查詢:
session.get(實體類名.class, id); //如果數據庫沒有 對應ID的記錄 ,返回null
session.load(實體類名.class, id); //如果數據庫沒有 對應ID的記錄 ,報錯
添加:session.save(實體類);
刪除:session.delete(實體類);
修改:session.update(實體類);
三,數據庫操作
- 幾個Demo
1.添加
創建 一個dao類 DeptAddTest.java ,代碼如下:
package cn.hibernatedemo.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.hibernatedemo.entity.Dept;
public class DeptAddTest {
public static void main(String[] args) {
Configuration conf=null;
SessionFactory sf=null;
Session session=null;
Transaction tx=null; //事務
try{
//1.讀取並解析配置文件及映射文件
conf=new Configuration().configure();
//2.依據配置文件和映射文件的信息,創建SessionFactory
sf=conf.buildSessionFactory();
//3.打開Session
// 打開有2種方法
// 1.openSession(),需要關閉
/* session=sf.openSession();*/
// 2.getCurrentSession(),不需要關閉
session=sf.getCurrentSession();
//4.開始一個事務
tx=session.beginTransaction();
//5.數據庫的操作
Dept dept=new Dept();
dept.setDeptNo(null); //mysql或sqlserver數據庫自動增長,主鍵 identity,數據庫自動增長,主鍵 assigned
dept.setDeptName("開發部");
dept.setLocation("西青區");
session.save(dept);
//6.提交事務
tx.commit();
//7.關閉資源 SessionFactory可以不用關閉 ;Session每個事務都要打開 ,需要關閉。
// session.close();
}catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}
}
}
2.查詢
1)session.get()方法獲得
創建一個dao類 DeptQueryByKeyGetTest.java,代碼如下;
package cn.hibernatedemo.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.hibernatedemo.entity.Dept;
public class DeptQueryByKeyGetTest {
public static void main(String[] args) {
Configuration conf=null;
SessionFactory sf=null;
Session session=null;
Transaction tx=null; //事務
try{
//1.讀取並解析配置文件及映射文件
conf=new Configuration().configure();
//2.依據配置文件和映射文件的信息,創建SessionFactory
sf=conf.buildSessionFactory();
//3.打開Session
// 打開有2種方法
// 1.openSession(),需要關閉
/* session=sf.openSession();*/
// 2.getCurrentSession(),不需要關閉
session=sf.getCurrentSession();
//4.開始一個事務
tx=session.beginTransaction();
//5.數據庫的操作
Dept dept=new Dept();
dept=(Dept)session.get(Dept.class, 1);
//6.提交事務
tx.commit();
//7.關閉資源 SessionFactory可以不用關閉 ;Session每個事務都要打開 ,需要關閉。
// session.close();
}catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}
}
}
2)session.load()方法獲得
創建一個dao類 DeptQueryByLoadGetTest.java ,代碼如下:
package cn.hibernatedemo.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import cn.hibernatedemo.entity.Dept;
public class DeptQueryByLoadGetTest {
public static void main(String[] args) {
Configuration conf=null;
SessionFactory sf=null;
Session session=null;
Transaction tx=null; //事務
try{
//1.讀取並解析配置文件及映射文件
conf=new Configuration().configure();
//2.依據配置文件和映射文件的信息,創建SessionFactory
sf=conf.buildSessionFactory();
//3.打開Session
// 打開有2種方法
// 1.openSession(),需要關閉
/* session=sf.openSession();*/
// 2.getCurrentSession(),不需要關閉
session=sf.getCurrentSession();
//4.開始一個事務
tx=session.beginTransaction();
//5.數據庫的操作
Dept dept=new Dept();
// dept=(Dept)session.get(Dept.class, 13); //不存在 返回null
dept=(Dept)session.load(Dept.class, 1); //不存在 報錯
System.out.println("Dept="+dept.toString());
//6.提交事務
tx.commit();
//7.關閉資源 SessionFactory可以不用關閉 ;Session每個事務都要打開 ,需要關閉。
// session.close();
}catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}
}
}
3.更新
創建一個dao類,代碼如下:
package cn.hibernatedemo.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.hibernatedemo.entity.Dept;
public class DeptUpdateTest {
public static void main(String[] args) {
Configuration conf=null;
SessionFactory sf=null;
Session session=null;
Transaction tx=null; //事務
try{
//1.讀取並解析配置文件及映射文件
conf=new Configuration().configure();
//2.依據配置文件和映射文件的信息,創建SessionFactory
sf=conf.buildSessionFactory();
//3.打開Session
// 打開有2種方法
// 1.openSession(),需要關閉
/* session=sf.openSession();*/
// 2.getCurrentSession(),不需要關閉
session=sf.getCurrentSession();
//4.開始一個事務
tx=session.beginTransaction();
//5.數據庫的操作
Dept dept=new Dept();
dept=(Dept)session.get(Dept.class, 1);
dept.setDeptName("aha");
dept.setLocation("nicai");
//6.提交事務
tx.commit();
//7.關閉資源 SessionFactory可以不用關閉 ;Session每個事務都要打開 ,需要關閉。
// session.close();
}catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}
}
}
4.刪除
創建一個dao類,DeptDeleteTest.java ,代碼如下:
package cn.hibernatedemo.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.hibernatedemo.entity.Dept;
public class DeptDeleteTest {
public static void main(String[] args) {
Configuration conf=null;
SessionFactory sf=null;
Session session=null;
Transaction tx=null; //事務
try{
//1.讀取並解析配置文件及映射文件
conf=new Configuration().configure();
//2.依據配置文件和映射文件的信息,創建SessionFactory
sf=conf.buildSessionFactory();
//3.打開Session
// 打開有2種方法
// 1.openSession(),需要關閉
/* session=sf.openSession();*/
// 2.getCurrentSession(),不需要關閉
session=sf.getCurrentSession();
//4.開始一個事務
tx=session.beginTransaction();
//5.數據庫的操作
Dept dept=new Dept();
dept=(Dept)session.get(Dept.class, 1);
session.delete(dept);
//6.提交事務
tx.commit();
//7.關閉資源 SessionFactory可以不用關閉 ;Session每個事務都要打開 ,需要關閉。
// session.close();
}catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}
}
}
- 統一封裝
1. 創建一個 讀取 配置文件, 獲得Session 的 工具類 HibernateUtil.java ,代碼如下:
package cn.hibernatedemo.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* @author LiuZJ
*
*/
public class HibernateUtil {
// static 唯一性
// final 不被繼承
private static Configuration configuration;
private static final SessionFactory sessionFactory;
// 初始化 Configuration 和 SessionFactory
static {
try{
// 1.讀取並解析配置文件及映射文件
configuration = new Configuration().configure();
// 2.依據配置文件和映射文件的信息,創建SessionFactory
sessionFactory = configuration.buildSessionFactory();
}catch (HibernateException e) {
// TODO: handle exception
throw new ExceptionInInitializerError(e);
}
}
public HibernateUtil(){
}
//獲取Session對象
public static Session currentSession(){
return sessionFactory.getCurrentSession();
}
}
2. 可以創建一個 BaseDao.java ,獲得Session代碼如下:
package cn.hibernatedemo.dao;
import org.hibernate.Session;
public class BaseDao {
public Session currentSession() {
return HibernateUtil.currentSession();
}
}
3. 創建一個 DeptDao.java 繼承BaseDao 代碼如下
package cn.hibernatedemo.dao;
import cn.hibernatedemo.entity.Dept;
public class DeptDao extends BaseDao{
//通過Session 的get()方法,根據OID 加載指定對象
public Dept get(int id){
return (Dept) currentSession().get(Dept.class, id);
}
//通過Session 的load()方法,根據OID 加載指定對象
public Dept load(int id){
return (Dept) currentSession().load(Dept.class, id);
}
//保存指定的Dept對象
public void save(Dept dept){
currentSession().save(dept);
}
//修改指定的Dept對象
public void update(Dept dept){
currentSession().update(dept);
}
//刪除指定的Dept對象
public void delete(Dept dept){
currentSession().delete(dept);
}
}
4.創建一個Service 層
創建一個 service類 DeptBiz.java ,代碼如下:
package cn.hibernatedemo.service;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import cn.hibernatedemo.dao.DeptDao;
import cn.hibernatedemo.dao.HibernateUtil;
import cn.hibernatedemo.entity.Dept;
public class DeptBiz {
private DeptDao dao = new DeptDao();
public DeptDao getDao() {
return dao;
}
public void setDao(DeptDao dao) {
this.dao = dao;
}
// 通過DeptDao.get()方法加載數據
public Dept findDeptById(int id) {
Transaction tx = null; // 事務
Dept result = null;
try {
tx = HibernateUtil.currentSession().beginTransaction();
result = dao.get(id);
tx.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
}
return result;
}
// 通過DeptDao.load()方法加載數據
public Dept findDeptById2(int id) {
Transaction tx = null; // 事務
Dept result = null;
try {
tx = HibernateUtil.currentSession().beginTransaction();
result = dao.load(id);
tx.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
}
return result;
}
// 添加一個Dept
public void addNewDept(Dept dept) {
Transaction tx = null; // 事務
try {
tx = HibernateUtil.currentSession().beginTransaction();
dao.save(dept);
tx.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
}
}
// 修改一個Dept
public void updateDept(Dept dept) {
Transaction tx = null; // 事務
try {
tx = HibernateUtil.currentSession().beginTransaction();
dao.update(dept);
tx.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
}
}
// 刪除一個Dept
public void deleteDept(int id) {
Transaction tx = null; // 事務
try {
tx = HibernateUtil.currentSession().beginTransaction();
Dept dept=dao.get(id);
dao.delete(dept);
tx.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
}
}
}
5.創建一個測試類 DeptTest.java,測試一下
package cn.hibernatedemo.test;
import cn.hibernatedemo.entity.Dept;
import cn.hibernatedemo.service.DeptBiz;
public class DeptTest {
public static void main(String[] args) {
DeptBiz deptBiz=new DeptBiz();
//----查詢
/* Dept dept=deptBiz.findDeptById(1);
System.out.println("dept="+dept.toString());*/
//----添加
/* deptBiz.addNewDept(new Dept("nicai","a"));*/
//----修改
/* Dept dept2=new Dept(1,"叫", "爸爸");
deptBiz.updateDept(dept2);*/
//----刪除
/* deptBiz.deleteDept(5);*/
}
}