浏览 1854 次
锁定老帖子 主题:一个简单的 Hibernate 配置例子
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-04-29
import java.io.Serializable; public class User implements Serializable { //标识属性 private int id; //用户名属性 private String userName; //密码属性 private String password; //电子邮件属性 private String email; public void setId(int id) { this.id = id; } public int getId() { return this.id; } public void setUserName(String user) { this.userName = user; } public String getUserName() { return this.userName; } public void setPassword(String pass) { this.password = pass; } public String getPassword() { return this.password; } public void setEmail(String email) { this.email = email; } public String getEmail() { return this.email; } } 然后,要实现ORM(Object Relational Mapping )需要一个 以 ".hbm.xml"后缀的映射文件,这样就可以实现对象映射关系.这里我的数据库使用的是Orale User.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.chen.model"> <class name="User" table="user_table"> <id name="id" column="user_id"> <generator class="increment"/> </id> <property name="userName" column="user_name" not-null="true" length="100"/> <property name="password" not-null="true" length="100" column="user_pwd"/> <property name="email" column="user_mail" length="100"/> </class> </hibernate-mapping> 很自然的,要连接数据库,要一些数据库的配置 "Hibernate.cfg.xml" 在配置文件中需要配置一些用户名,密码,数据库驱动,连接串,映射文件 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:ORA </property> <property name="connection.username">chenlei</property> <property name="connection.password"></property> <property name="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <mapping resource="com/test/hbm/User.hbm.xml" /> </session-factory> </hibernate-configuration> 这样子,基本的配置就已经完成了 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-04-29
lchenjay 写道 。。。 首先,我们之所以需要使用到Hibernate,是因为它是一种数据库面向对象的方法, 。。。 这倒是以前没有听说过。 |
|
返回顶楼 | |