第一个Hibernate应用程序

作者:计算机教程

实体类:User

  有朋友问起 Hibernate的学习,故将以前学习笔记供参考

    packagecn.csdn.hibernater.domain;

1 设置好配置文件,主要是有JDBC的连接字符串

importjava.util.Date;

Hibernate.cfg.xml

public class User {

 

    private Integer id;

<!DOCTYPE hibernate-configuration PUBLIC

    private String name;

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    private Date birthday;

    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;

    public User() {

<hibernate-configuration>

       super();

    <session-factory>

    }

       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

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

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

       super();

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

       this.id = id;

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

       this.name = name;

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

       this.birthday =birthday;

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

    }

       <mapping resource="cn/itcase/hibernate/domain/User.hbm.xml"/>

    public IntegergetId() {

           </session-factory>

       return id;

</hibernate-configuration>

    }

 

    public voidsetId(Integer id) {

数据对象配置文件

       this.id = id;

User.hbm.xml

    }

 

    public StringgetName() {

<?xml version="1.0"?>

       return name;

<!DOCTYPE hibernate-mapping PUBLIC

    }

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    public voidsetName(String name) {

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;

       this.name = name;

<hibernate-mapping package="cn.itcase.hibernate.domain">

    }

    <class name="User" table="user">

    public DategetBirthday() {

       <id name="id">

       return birthday;

           <generator class="native" />

    }

       </id>

    public voidsetBirthday(Date birthday) {

       <property name="name"/>

       this.birthday =birthday;

       <property name="birthday" />

    }

    </class>

    @Override

</hibernate-mapping>

    public StringtoString() {

数据对象类

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

User.Java

              "]";

 

    }

package cn.itcase.hibernate.domain;

}

import java.util.Date;

Hibernatep.cfg.xml配置文件:

public class User {

<!DOCTYPE hibernate-configuration PUBLIC

 

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

     private int id;

    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;

     private String name;

 

     private Date birthday;

<hibernate-configuration>

     

<!-- 设置一个特定数据库的工厂 -->

    public int getId() {

    <session-factory>

       return id;

<!-- 设置连接数据库信息 -->

    }

       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

    public void setId(int id) {

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

       this.id = id;

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

    }

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

    public String getName() {

<!-- 设置方言 -->

       return name;

nba买球,       <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    }

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

    public void setName(String name) {

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

       this.name = name;

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

    }

本文由nba买球发布,转载请注明来源

关键词: