Professional Documents
Culture Documents
Upload by Cafeitvn.com
Ni dung trnh by
Gii thiu Hibernate
n gin vi
Xa sinh vin
Upload by Cafeitvn.com
Hibernate
C s d liu thng c thit k v lu tr theo hng quan h. Tuy nhin phn mm thng c xy dng theo hng i tng.
i vi lp trnh vin khi xy dng phn mm thng mun lm vic vi cc i tng v khng phi nh n cc dng , cc ct trong cc bng ca c s d liu.
Upload by Cafeitvn.com
Ci t
Upload by Cafeitvn.com
Mappings
Hibernate
Configuaration
JDBC
DATABASE
Upload by Cafeitvn.com
Cc bc s dng hibernate
Bc 1: To c s d liu
Bc 2: To cc POJO
Bc 3: To file cu hnh hibernate.cfg.xml Bc 4: To cc file mapping <POJO>.hbm.xml Bc 5: Khai bo cc file mapping vo hibernate.cfg.xml Bc 6: Xy dng lp HibernateUtil
Upload by Cafeitvn.com
Bc 1: To c s d liu
CSDL: MySQL QuanLySinhVien
Upload by Cafeitvn.com
Bc 2: To POJO SinhVien
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
8
package pojo; import java.util.Date; public class SinhVien implements java.io.Serializable { private String maSinhVien; private String hoVaTen; private Date ngaySinh; private String diaChi; public SinhVien() { } public SinhVien(String maSinhVien) { this.maSinhVien = maSinhVien; } public SinhVien(String maSinhVien, String hoVaTen, Date ngaySinh, String diaChi) { this.maSinhVien = maSinhVien; this.hoVaTen = hoVaTen; this.ngaySinh = ngaySinh; this.diaChi = diaChi; }
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Upload by Cafeitvn.com
Bc 2: To POJO SinhVien
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public String getMaSinhVien() { return this.maSinhVien; } public void setMaSinhVien(String maSinhVien) { this.maSinhVien = maSinhVien; } public String getHoVaTen() { return this.hoVaTen; } public void setHoVaTen(String hoVaTen) { this.hoVaTen = hoVaTen; } public Date getNgaySinh() { return this.ngaySinh; } public void setNgaySinh(Date ngaySinh) { this.ngaySinh = ngaySinh; } public String getDiaChi() { return this.diaChi; } public void setDiaChi(String diaChi) { this.diaChi = diaChi; }
Upload by Cafeitvn.com
10
Upload by Cafeitvn.com
11
Upload by Cafeitvn.com
12
Upload by Cafeitvn.com
13
Upload by Cafeitvn.com
14
Upload by Cafeitvn.com
Bc 6: Xy dng lp HibernateUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.SessionFactory; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
15
Upload by Cafeitvn.com
16
Upload by Cafeitvn.com
17
Upload by Cafeitvn.com
18
Upload by Cafeitvn.com
public class Main { public static void main(String[] args) { //<editor-fold defaultstate="collapsed" desc="2. Ly thng tin sinh vin"> SinhVien sv = SinhVienDAO.layThongTinSinhVien("0312143"); if(sv!=null){ System.out.println("MSSV: " + sv.getMaSinhVien()); System.out.println("H v tn: " + sv.getHoVaTen()); System.out.println("Ngy sinh: " + sv.getNgaySinh()); System.out.println("a ch: " + sv.getDiaChi()); }else{ System.out.println("Sinh vin 0312143 khng tn ti"); } //</editor-fold>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Upload by Cafeitvn.com
20
Upload by Cafeitvn.com
public class Main { public static void main(String[] args) { //<editor-fold defaultstate="collapsed" desc="3. Thm sinh vin"> SinhVien sv = new SinhVien(); sv.setMaSinhVien("0312171"); sv.setHoVaTen("T Tn Thm"); Calendar calendar = Calendar.getInstance(); calendar.set(1985, 5, 18); Date d = calendar.getTime(); sv.setNgaySinh(d); sv.setDiaChi("Vng Lim Vnh Long"); boolean kq = SinhVienDAO.themSinhVien(sv); if (kq == true) { System.out.println("Thm thnh cng"); } else { System.out.println("Thm tht bi"); } //</editor-fold>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Upload by Cafeitvn.com
22
Upload by Cafeitvn.com
public class Main { public static void main(String[] args) { //<editor-fold defaultstate="collapsed" desc="4. Cp nht sinh vin"> SinhVien sv = SinhVienDAO.layThongTinSinhVien("0312143"); if (sv != null) { sv.setHoVaTen("T Tn Thm"); Calendar calendar = Calendar.getInstance(); calendar.set(1985, 5, 18); Date d = calendar.getTime(); sv.setNgaySinh(d); sv.setDiaChi("Vng Lim Vnh Long"); boolean kq = SinhVienDAO.capNhatThongTinSinhVien(sv); if (kq == true) { System.out.println("Cp nht thnh cng"); } else { System.out.println("Cp nht tht bi"); } } //</editor-fold>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Upload by Cafeitvn.com
public class SinhVienDAO { public static boolean xoaSinhVien(String maSinhVien) { Session session = HibernateUtil.getSessionFactory().openSession(); SinhVien sv = SinhVienDAO.layThongTinSinhVien(maSinhVien); if(sv==null){ return false; } Transaction transaction = null; try { transaction = session.beginTransaction(); session.delete(sv); transaction.commit(); } catch (HibernateException ex) { //Log the exception transaction.rollback(); System.err.println(ex); } finally { session.close(); } return true; } }
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Upload by Cafeitvn.com
25
Upload by Cafeitvn.com
26
Upload by Cafeitvn.com
HI V P
Upload by Cafeitvn.com
27