Hibernate的用法基于xml方式

来源:互联网 发布:plc电路图绘制软件 编辑:程序博客网 时间:2024/05/21 17:09

(一)Hibernate的配置文件(hibernate.cfg.xml):可以在Hibernate的说明文档中找到具体的使用方法:

<?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>


        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>


        <!-- JDBC connection pool (use the built-in) -->
       <!--  <property name="connection.pool_size">1</property>-->
       
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>


        <!-- Enable Hibernate's automatic session context management -->
       <!--  <property name="current_session_context_class">thread</property> -->


        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>


        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>//显示sql语句

<property name="format_sql">true</property>//使sql语句分行,更加的格式化



        <!-- Drop and re-create the database schema on startup -->

       <!--该配置有四个选项值:validate:会自动验证   create:数据库中没有建表会自动创建表    update:对于新增属性会自动添加到数据库   create-drop:当关闭sessionFactory时会自动删除表 常用create -->
        <property name="hbm2ddl.auto">update</property>


        <mapping resource="model/Student.hbm.xml"/>


    </session-factory>


</hibernate-configuration>


(二)实体类与数据库表的映射配置文件(Student.hbm.xml):与实体类写在一个package中

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="org.hibernate.tutorial.domain">
 <class name="Event" table="EVENTS">        <id name="id" column="EVENT_ID">            <generator class="native"/>        </id>        <property name="date" type="timestamp" column="EVENT_DATE"/>        <property name="title"/>    </class>
</hibernate-mapping>

其中:package表示实体类的包路径,class为实体类与数据库表的映射,如果name与数据库表的名字一直,可以省略table。

column表示实体类的属性的名字与数据库表中字段的名字不一致可以用column来标记,如果属性名与字段名一致,可以省略column。


(三)Hibernate的使用

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


import model.Student;


public class Test {


public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("a1");
s.setAge(18);

Configuration cfg = new Configuration();
    SessionFactory sf = cfg.configure().buildSessionFactory();
    Session session = sf.openSession();
            session.beginTransaction();
            session.save(s);
    session.getTransaction().commit();
    session.close();
    sf.close();

}
}