首页 > 技术文章 > JAVA EE Hibernate基础一之环境配置

AndroidJotting 2015-05-13 18:07 原文

  对于JAVA开发高级,hibernate是java三大框架之一,足以见得它的重要性,那么对于hibernate的使用大家有了解多少呢?从今天开始我将带领大家一道共同探讨一下hibernate的知识,hibernate对于我们开发移动应用关联数据库十分方便,hibernate对于数据库的操作十分便利,省去了很多之前开发时的不便。

  Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。

  Hibernate的核心和类接口一共有6个,分别为:Session、SessionFactory、Transaction、Query、Criteria和Configuration。这6个核心和类接口在任何开发中都会用到。通过这些接口,不仅可以对持久化对象进行存取,还能够进行事务控制。详情见:<http://baike.baidu.com/link?url=KBUhIQIahzy0NfcFh-q3bu1_2hB9uHd1A5X5f_cDqoshxiQiXlkTR5b8r2ohme0hMzZl7ogoroZRqQMLuhDhNa>

  

  下面开始我们本片内容的介绍:

  第一步:安装JAVA EE开发环境

  JAVA EE 开发同样使用ecipse,这里我提供一个下载地址:http://www.newasp.net/soft/71687.html=,感兴趣的小童鞋可以去下载一下,安装过程超级简单,大家自己琢磨一下吧。

  第二步:创建一个新项目

  方法与我们创建web项目一致,左侧右键单击,选择new project,工程名大家可以自行设定,只要符合JAVA的命名规范即可。

  第三步:导入我们可能使用到的jar包

  这里小编没没有完全搞定,后续会将这部分更新,敬请期待!

  第四步:配置Hibernate环境变量

  a、hibernate.properties文件:

######################
### Query Language ###
######################

## define query language constants / function names

hibernate.query.substitutions yes 'Y', no 'N'


## select the classic query parser

#hibernate.query.factory_class org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory


#################
### Platforms ###
#################


## MySQL

#hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://127.0.0.1:3306/test
hibernate.connection.username root
hibernate.connection.password root


## MS SQL Server

#hibernate.dialect org.hibernate.dialect.SQLServerDialect
#hibernate.connection.username sa
#hibernate.connection.password sa


#################################
### Hibernate Connection Pool ###
#################################

hibernate.connection.pool_size 1



###########################
### C3P0 Connection Pool###
###########################

hibernate.c3p0.max_size 2
hibernate.c3p0.min_size 2
hibernate.c3p0.timeout 5000
hibernate.c3p0.max_statements 100
hibernate.c3p0.idle_test_period 3000
hibernate.c3p0.acquire_increment 2
hibernate.c3p0.validate false

##############################
### Miscellaneous Settings ###
##############################

## print all generated SQL to the console

#hibernate.show_sql true


## format SQL in log and console

hibernate.format_sql true

## add comments to the generated SQL

#hibernate.use_sql_comments true


## generate statistics

#hibernate.generate_statistics true


## auto schema export

#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
hibernate.hbm2ddl.auto update
#hibernate.hbm2ddl.auto validate

  注释:C3P0 Connection Pool的注解详见:http://baike.baidu.com/link?url=37RuZ_7cnA1F4S6iCZ3PP6f63H0SRBcUN3j1QSJ6EDmT1k_NFSQkP9PzJNVb7gykidNyROoL8mAq_K9OlcQqBK

      hibernate.format_sql true的注解详见:http://blog.csdn.net/zhengqiqiqinqin/article/details/8450875

       hibernate.hbm2ddl.auto update的注解详见:http://www.linuxidc.com/Linux/2011-12/49206.htm

       hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect:我们安装的mysql数据库版本声明

  第五步:创建我们的User对象类:

  Hibernate的中数据的操作都是通过对象的形式进行的,故我们这里需要创建一个对象类,用于我们后续的操作。

public class User {

    private int id;
    private String name;
    private String pass;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPass() {
        return pass;
    }
    public void setPass(String pass) {
        this.pass = pass;
    }
    
}

  第六步:创建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="po">

    <class name="User">
        <id name="id" column="c_id">
            <generator class="native"></generator><!-- 设置主键 -->
        </id>
        
        <property name="name" column="c_name" length="20"/>
        <property name="pass" column="c_pass" length="20"/>    
    </class>

</hibernate-mapping>

  这个文件需要与我们对应的类放置到同一个文件夹中,并且该文件的文件名后缀一定不要进行改动。

  第七步:配置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  name="foo">
        
        <property name="show_sql">true</property>
        
        <mapping resource="po/User.hbm.xml"/>
        
    </session-factory>
</hibernate-configuration>

  到这里我们的开发前的配置工作已经完成,对于他的使用,我将会在下一篇为大家介绍。

推荐阅读