1、
我封装的 操作Hibernate的类:
1 package hibernateOper; 2 3 import org.hibernate.SessionFactory; 4 import org.hibernate.cfg.Configuration; 5 6 @SuppressWarnings("deprecation") 7 final public class ThibernateOper 8 {// ZC: 上面的"final",让别人无法再继承该类 9 public ThibernateOper() 10 {} 11 12 // 一般连接一个数据库,只用一个SessionFactory。 13 private static SessionFactory FsessionFactory = null; 14 static // 静态块,只会执行一次 15 { 16 // Configuration.configure() 中可以参入参数[hibernate.cfg.xml的文件名和路径] 17 FsessionFactory = (new Configuration()).configure().buildSessionFactory(); 18 } 19 20 public static SessionFactory GetSessionFactory() 21 { 22 return FsessionFactory; 23 } 24 25 public static void main(String[] args) 26 { 27 } 28 }
2、
对Oracle的简单操作:
1 package test; 2 3 import java.util.Date; 4 5 import hibernateOper.ThibernateOper; 6 7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.hibernate.Transaction; 10 11 import domain.Employee; 12 13 public class TTest 14 { 15 public static void SimpleInsert() 16 { 17 SessionFactory sf = ThibernateOper.GetSessionFactory(); 18 Session session = sf.openSession(); 19 Transaction ts = session.beginTransaction(); 20 21 try 22 { 23 Employee e = new Employee(); 24 e.setName("zz"); 25 e.setEmail("zzz@163.com"); 26 e.setHiredate(new Date()); 27 28 session.save(e); 29 ts.commit(); 30 session.close(); 31 } 32 catch(Exception ex) 33 { 34 ts.rollback(); 35 if ( (session != null)&&(session.isOpen()) ) 36 session.close(); 37 ex.printStackTrace(); 38 } 39 } 40 41 public static void SimpleUpdate() 42 { 43 SessionFactory sf = ThibernateOper.GetSessionFactory(); 44 Session session = sf.openSession(); 45 Transaction ts = session.beginTransaction(); 46 47 try 48 { 49 // 强转 50 Employee e = (Employee)session.load(Employee.class, 1); 51 e.setName("zxcvb"); 52 53 ts.commit(); 54 session.close(); 55 } 56 catch(Exception ex) 57 { 58 ts.rollback(); 59 if ( (session != null)&&(session.isOpen()) ) 60 session.close(); 61 ex.printStackTrace(); 62 } 63 } 64 65 public static void SimpleDelete() 66 { 67 SessionFactory sf = ThibernateOper.GetSessionFactory(); 68 Session session = sf.openSession(); 69 Transaction ts = session.beginTransaction(); 70 71 try 72 { 73 // 强转 74 Employee e = (Employee)session.load(Employee.class, 2); 75 e.setName("zxcvb"); 76 77 session.delete(e); 78 ts.commit(); 79 session.close(); 80 } 81 catch(Exception ex) 82 { 83 ts.rollback(); 84 if ( (session != null)&&(session.isOpen()) ) 85 session.close(); 86 ex.printStackTrace(); 87 } 88 } 89 90 public static void main(String[] args) 91 { 92 //SimpleInsert(); 93 //SimpleUpdate(); 94 SimpleDelete(); 95 } 96 97 }
3、
hibernate.cfg.xml
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <!-- Generated by MyEclipse Hibernate Tools. --> 6 <hibernate-configuration> 7 8 <session-factory> 9 <property name="dialect"> 10 org.hibernate.dialect.Oracle10gDialect 11 </property> 12 <property name="connection.url"> 13 jdbc:oracle:thin:@localhost:1521:orcl 14 </property> 15 <property name="connection.username">scott</property> 16 <property name="connection.password">tiger</property> 17 <property name="connection.driver_class"> 18 oracle.jdbc.driver.OracleDriver 19 </property> 20 <property name="myeclipse.connection.profile"> 21 OracleLocal 22 </property> 23 <property name="show_sql">true</property> 24 <property name="format_sql">true</property> 25 <mapping resource="domain/Employee.hbm.xml" /> 26 27 </session-factory> 28 29 </hibernate-configuration>
4、
Employee.hbm.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 4 <!-- 5 Mapping file autogenerated by MyEclipse Persistence Tools 6 --> 7 <hibernate-mapping> 8 <class name="domain.Employee" table="EMPLOYEE" schema="SCOTT"> 9 <id name="id" type="java.lang.Integer"> 10 <column name="ID" precision="22" scale="0" /> 11 <!-- 12 <generator class="assigned" /> 13 --> 14 <generator class="sequence"> 15 <param name="sequence">emp_seq</param> 16 </generator> 17 </id> 18 <property name="name" type="java.lang.String"> 19 <column name="NAME" length="64" not-null="true" /> 20 </property> 21 <property name="email" type="java.lang.String"> 22 <column name="EMAIL" length="64" not-null="true" /> 23 </property> 24 <property name="hiredate" type="java.util.Date"> 25 <column name="HIREDATE" length="7" not-null="true" /> 26 </property> 27 </class> 28 </hibernate-mapping>
5、
Employee.java
1 package domain; 2 3 import java.math.BigDecimal; 4 import java.util.Date; 5 6 /** 7 * Employee entity. @author MyEclipse Persistence Tools 8 */ 9 10 public class Employee implements java.io.Serializable { 11 12 // Fields 13 14 private static final long serialVersionUID = 1L; 15 16 //private BigDecimal id; 17 private Integer id; 18 private String name; 19 private String email; 20 private Date hiredate; 21 22 // Constructors 23 24 /** default constructor */ 25 public Employee() { 26 } 27 28 /** full constructor */ 29 public Employee(Integer id, String name, String email, Date hiredate) { 30 this.id = id; 31 this.name = name; 32 this.email = email; 33 this.hiredate = hiredate; 34 } 35 36 // Property accessors 37 38 public Integer getId() { 39 return this.id; 40 } 41 42 public void setId(Integer id) { 43 this.id = id; 44 } 45 46 public String getName() { 47 return this.name; 48 } 49 50 public void setName(String name) { 51 this.name = name; 52 } 53 54 public String getEmail() { 55 return this.email; 56 } 57 58 public void setEmail(String email) { 59 this.email = email; 60 } 61 62 public Date getHiredate() { 63 return this.hiredate; 64 } 65 66 public void setHiredate(Date hiredate) { 67 this.hiredate = hiredate; 68 } 69 70 }
6、