hibernate4中主要的配置文件配置

在hibernate中有两个主要的配置文件:hibernate.cfg.xml,xxx.hbm.xml。

使用mysql数据库时,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 >
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        
         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test_hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>
        
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        
        <mapping resource="com/zc/hibernate/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

User类代码如下:

package com.zc.hibernate;

import java.util.Date;

public class User {

    private int id;
    private String username;
    private String password;
    private Date birthday;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    
    
}
User类的映射文件:User.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">

<hibernate-mapping  package="com.zc.hibernate">
    <class name="User" table="t_user">
     <id name="id">
            <generator class="native"/>
        </id>
    <property name="username"/>
    <property name="password"/>
    <property name="birthday" type="timestamp"/>
    </class>
</hibernate-mapping>

原文地址:https://www.cnblogs.com/charleszhang1988/p/2936045.html