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 }
原文地址:https://www.cnblogs.com/lihuibin/p/8168260.html
时间: 2024-11-02 11:32:51