日期:2014-05-18 浏览次数:20722 次
<?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="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 数据库链接URL -->
<property name="connection.url">
jdbc:mysql://localhost/struts2
</property>
<!-- 数据库用户名 -->
<property name="connection.username">root</property>
<!-- 数据库用户密码 -->
<property name="connection.password">root</property>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 指定映射文件 -->
<mapping resource="kedun/dhy/hibernate/Product.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
<?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-mapping>
<!-- 每个class对应一个持久化对象 -->
<class name="kedun.dhy.hibernate.Product">
<!-- id元素用来定义主键标识,并指定主键生成策略 -->
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="name"></property>
<property name="price"></property>
</class>
</hibernate-mapping>
package kedun.dhy.hibernate;
public class Product {
//产品ID
private String id;
//产品名称
private String name;
//产品价格
private double price;
//各属性的setter和getter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
package kedun.dhy.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class CreateDB {
public static void main(String[] args)
{
//读