//首先创建实体类 package cn.com.bochy.entity; public class hibernatebean { private long id; private String name; private String pwd; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } //然后 实体类同包下新建类名.hbm.xml 实体类和数据表的映射文件 内容如下 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- package 是实体类所对应的完整包名 --> <hibernate-mapping package="cn.com.bochy.entity"> <!-- 对类的配置 nane是实体类名 table 是表名 可以不写 默认与name的值一致 --> <class name="hibernatebean" table="t_user"> <!-- 对类对应的主键配置 name 是实体类中主键属性名 column 是表中主键对应的列名 可以不写 默认与namne的值一致 --> <id name="id" column="id"> <!-- 主键生成策略 native 如果连接的是oracle数据库 则表示序列 ,并且序列的名字为为hibernate——sequence --> <generator class="native"/> </id> <!-- 对类的非主键配置 --> <property name="name" column="name" type="string" unique="true" length="20"></property> <property name="pwd" column="pwd" type="string" length="20"></property> </class> </hibernate-mapping> //接下来新建hibernate配置文件 <?xml version=‘1.0‘ encoding=‘utf-8‘?> <!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> <!-- 数据库连接设置 --> <!-- Database connection settings --> <property name="connection.driver_class"> oracle.jdbc.OracleDriver </property <property name="connection.url"> jdbc:oracle:thin:@admini-PC.mshome.net:1521:orcl </property> <property name="connection.username">scott</property> <property name="connection.password">scott</property> <!-- sql方言 --> <!-- SQL dialect --> <property name="dialect"> org.hibernate.dialect.OracleDialect </property> <!-- 显示sql语句 --> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!--将显示的sql语句按一定的格式 显示出来 --> <property name="format_sql">true</property> <!-- ddl(数据定义语言)的配置 --> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <!-- 配置读取的映射文件 --> <mapping resource="cn/com/bochy/entity/hibernate_HelloWorld.hbm.xml" /> <!-- ddl(数据定义语言)的配置 --> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <!-- 配置读取的数据库映射文件 --> <mapping resource="cn/com/bochy/entity/hibernate_HelloWorld.hbm.xml" /> </session-factory> </hibernate-configuration>
本文出自 “任重道远” 博客,请务必保留此出处http://534256702.blog.51cto.com/10669156/1702950
原文地址:http://534256702.blog.51cto.com/10669156/1702950