Hibernate环境搭建步骤

来源:互联网 发布:淘宝开车是什么意思 编辑:程序博客网 时间:2024/04/30 15:07

一:下载源码

           版本:hibernate-distribution-3.6.0.Final

二:引入Jar文件

hibernate3.jar核心  +  required 必须引入的(6) +  jpa 目录  数据库驱动包


三:写对象以及对象的映射

                Employee.java            对象

                Employee.hbm.xml        对象的映射 (映射文件)

// 对象public class Employee {private int empId;private String empName;private Date workDate;public int getEmpId() {return empId;}public void setEmpId(int empId) {this.empId = empId;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}public Date getWorkDate() {return workDate;}public void setWorkDate(Date workDate) {this.workDate = workDate;}@Overridepublic String toString() {return "Employee [empId=" + empId + ", empName=" + empName+ ", workDate=" + workDate + "]";}}


四:在src目录下写Hibernate.cfg.xml配置文件

-----------------------1.数据库连接配置

-----------------------2.加载所用的映射(*.hbm.xml)

Hibernate.cfg.xml配置文件如下所示:

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><!-- 通常,一个session-factory节点代表一个数据库 --><session-factory><!-- 1. 数据库连接配置 --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql:///employee</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">467276</property><!-- 数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql --><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><!-- 2. 其他相关配置 --><!-- 2.1 显示hibernate在运行时候执行的sql语句 --><property name="hibernate.show_sql">true</property><!-- 2.2 格式化sql --><property name="hibernate.format_sql">true</property><!-- 2.3 自动建表  --><property name="hibernate.hbm2ddl.auto">update</property><!-- 3. 加载所有映射 <mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/>--></session-factory></hibernate-configuration>




0 0
原创粉丝点击