直接上(默認(rèn)什么都配置好了的哦)
mark-1:準(zhǔn)備數(shù)據(jù)類
//這是一個(gè)學(xué)生成績(jī)類
public class Score {
private int id;
private int stuId;//學(xué)生編號(hào)
private int subjectId;//科目編號(hào)
private double result;//成績(jī)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public int getSubjectId() {
return subjectId;
}
public void setSubjectId(int subjectId) {
this.subjectId = subjectId;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
}
mark-2: 配置him.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">
<hibernate-mapping>
<class name="com.hibernate.zuhekey.Score" table="Score">
<id name="id">
<!-- 主鍵生成策略 -->
<generator class="native"></generator>
</id>
<!-- 實(shí)體類的屬性 -->
<property name="stuId"/>
<property name="subjectId"/>
<property name="result"/>
</class>
</hibernate-mapping>
mark-3:配置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>
<!--配置數(shù)據(jù)庫(kù)的連接信息-->
<!--配置數(shù)據(jù)庫(kù)驅(qū)動(dòng)-->
<property name="connection.driver_class">
com.mysql.jdbc.Driver</property>
<!--配置要連接的數(shù)據(jù)庫(kù)地址-->
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate4
</property>
<!--配置用戶名和密碼-->
<property name="connection.username">root</property>
<property name="connection.password”>root</property>
<!-- 數(shù)據(jù)庫(kù)方言 -->
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<!-- 將hibernate生成的sql語(yǔ)句打印到控制臺(tái) -->
<property name="hibernate.show_sql">true</property>
<!-- 將hibernate生成的sql語(yǔ)句格式化(語(yǔ)法縮進(jìn)) -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 引入orm元數(shù)據(jù)
路徑書(shū)寫(xiě): 填寫(xiě)src下的路徑
-->
<!-- 有幾個(gè)pojo類就配置幾個(gè)資源路徑 -->
<mapping resource="com/hibernate/demo/User.hbm.xml" />
<mapping resource="com/hibernate/demo/Product.hbm.xml"/>
<mapping resource="com/hibernate/cust/Customer.hbm.xml"/>
<mapping resource="com/hibernate/zuhekey/Score.hbm.xml"/>
</session-factory>
mark-4: 利用hibernate生成對(duì)應(yīng)數(shù)據(jù)表
public static void main(String[] args){
Configuration config = new Configuration().configure();
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.configure().build();
Metadata metadata = new MetadataSources(serviceRegistry)
.buildMetadata();
SchemaExport schemaExport = new SchemaExport();
schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
}
5.0之前的寫(xiě)法過(guò)期了: