配置一个简单的hibernate应用

1、导入jar包,运行hibernate应用需要的最少jar包除下数据库驱动还需要:

hibernate3.jar:hibernate的核心包

antlr-2.7.6.jar:语言转换工具,hibernate把hql语句转换成sql语句

commons-colletions-3.1.jar加强java程序对集合的处理能力

dom4j.jar:解析xml文件

javassist.jar:动态java代码生成工具

jta.jar:java事务处理接口

slf4j-api.jar:日记相关

slf4j-nop.jar:日记相关

2、hibernate配置文件(两种形式,放在类路径下):

properties文件(hibernate.properties):

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/SAMPLEDB
hibernate.connection.username=root
hibernate.connection.password=1234
hibernate.show_sql=true

xml配置文件:

<?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>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>
        <!-- 针对建表的操作 -->
        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
    </session-factory>
     
     
 </hibernate-configuration>

3、加载配置文件

properties文件形式:

 Configuration config = new Configuration();//框架会默认加载类路径下的配置文件
      //加载Customer类的对象-关系映射文件
      config.addClass(Customer.class);//需要在代码中加载类对象
      // 创建SessionFactory实例 */
      sessionFactory = config.buildSessionFactory();

xml文件形式:

Configuration config = new Configuration();
config.confingure();//需要在代码中加载配置文件()如果配置名称不是,hibernate.xml,需要指定配置文件名称,不需要手动加载类对象
config.buildSessionFactory();


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。