第一个Hibernate应用程序

实体类:User

    packagecn.csdn.hibernater.domain;

importjava.util.Date;

public class User {

    private Integer id;

    private String name;

    private Date birthday;

    public User() {

       super();

    }

    publicUser(Integer id, String name, Date birthday) {

       super();

       this.id = id;

       this.name = name;

       this.birthday =birthday;

    }

    public IntegergetId() {

       return id;

    }

    public voidsetId(Integer id) {

       this.id = id;

    }

    public StringgetName() {

       return name;

    }

    public voidsetName(String name) {

       this.name = name;

    }

    public DategetBirthday() {

       return birthday;

    }

    public voidsetBirthday(Date birthday) {

       this.birthday =birthday;

    }

    @Override

    public StringtoString() {

       return "User[id=" + id + ",,birthday=" + birthday

              +"]";

    }

}

Hibernatep.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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

       <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>

       <property name="hibernate.connection.username">root</property>

       <property name="hibernate.connection.password"></property>

<!-- 设置方言 -->

       <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 设置自动生成数据库  -->

       <property name="hibernate.hbm2ddl.auto">create</property>

<!-- 显示生成数据库的sql语句 -->

       <property name="hibernate.show_sql">true</property>

<!-- 设置实体类与数据库映射的配置文件路径 -->

       <mapping resource="cn/csdn/hibernater/domain/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/psxyw.html