1. 程式人生 > >hibernate添加數據入門小案例

hibernate添加數據入門小案例

ng- local main net domain .so 語句 info lec

1.建立一個java項目,在目錄下新建一個lib文件夾引入hibernate架包如圖所示:

技術分享圖片

2. 新建com.LHB.domain包,在包中分別創建一個Employee.java和Employee.hbm.xml文件,

Employee.java中的代碼如下:

 1 package com.LHB.domain;
 2 
 3 import java.util.Date;
 4 
 5 public class Employee {
 6 
 7     private Integer id;
 8     private String name;
 9     private String email;
10 private Date hiredate; 11 public Integer getId() { 12 return id; 13 } 14 public void setId(Integer id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name;
22 } 23 public String getEmail() { 24 return email; 25 } 26 public void setEmail(String email) { 27 this.email = email; 28 } 29 public Date getHiredate() { 30 return hiredate; 31 } 32 public void setHiredate(Date hiredate) { 33 this.hiredate = hiredate;
34 } 35 36 }

Employee.hbm.xml映射文件配置如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!-- 映射文件通過DTD來指定格式 -->
 3 <!DOCTYPE hibernate-mapping PUBLIC 
 4     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 5     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 6 <!-- 該文件用於配置domain對象和表的映射關系 -->
 7 <hibernate-mapping package="com.LHB.domain">
 8     <class name="Employee" table="employee" >
 9         <!-- id元素用於指定主鍵屬性 -->
10         <id name="id" column="id" type="java.lang.Integer">
11             <!-- 該元素用來指定主鍵生成的策略hilo native increment sequence uuid -->
12             <generator class="sequence">
13                 <param name="sequence">emp_seq</param>
14             </generator>
15         </id>
16         
17         <!-- 對其它屬性還要配置 -->
18         <property name="name" type="java.long.String">
19             <column name="name" not-null="false"  />
20         </property>
21         <property name="emial" type="java.long.String">
22             <column name="emial" not-null="false" />
23         </property>
24         <property name="hiredate" type="java.util.Date">
25             <column name="hiredate" not-null="false" />
26         </property>
27     </class>
28     
29 
30 </hibernate-mapping>

3.在項目src文件下創建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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <!-- 配置使用的driver -->
 9         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
10         <property name="connection.username">scott</property>
11         <property name="connection.password">tiger</property>
12         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
13         <!-- 配置dialect方言,明確告訴hibernate連接哪種數據庫 -->
14         <property name="dialect">org.hibernate.dialect.OracleDialect</property>
15         <!-- 顯示出對應的sql語句 -->
16         <property name="show_sql">true</property>
17         <!-- 指定管理的對象映射文件 -->
18         <mapping resource="com/LHB/domain/Employee.hbm.xml"/>
19         
20     </session-factory>
21 </hibernate-configuration>

4. 創建一個測試類TestMain

 1 package com.LHB.view;
 2 
 3 import java.util.Date;
 4 
 5 import org.hibernate.SessionFactory;
 6 import org.hibernate.Transaction;
 7 import org.hibernate.cfg.Configuration;
 8 import org.hibernate.classic.Session;
 9 
10 import com.LHB.domain.Employee;
11 
12 public class TestMain {
13 
14     public static void main(String[] args) {
15         // TODO Auto-generated method stub
16 
17         //使用hibernate完成crud操作
18         //現在不使用service,直接測試
19         //1.創建Configuration,該對象用於讀取hibernate.cfg.xml並完成初始化
20         Configuration configuration = new Configuration().configure();
21         //2.創建SessionFactory【這是一個會話工廠,是一個重量級的對象】
22         SessionFactory sessionFactory = configuration.buildSessionFactory();
23         //3.創建Session,相當於jdbc中的Connection
24         Session  session = sessionFactory.openSession();
25         //4.對於hibernate,要求在進行增加,刪除,修改的時候使用事物提交
26         Transaction transaction = session.beginTransaction();
27         
28         //添加一個雇員
29         Employee employee = new Employee();
30         employee.setName("zhangsan");
31         employee.setEmail("[email protected]");
32         employee.setHiredate(new Date());
33         
34         //保存
35         session.save(employee);//相當於insert into....[被hibernate封裝]
36         
37         session.close();
38         
39     }
40 
41 }

hibernate添加數據入門小案例