Hibernate初学笔记

l Hibernate初步配置:

1 新建项目

2 学习建立user-library-hibernate,并加入相应的jar包(hibernate核心jar包,lib下的所有包)

3 引入mysql的JDBC驱动包

4 在MYSQL中建数据库和相应的表student(id,name,age)

5 建立hibernate配置文件hibernate.cfg.xml,模板是参照hibernate.properties。

6 建立student类

7 建立映射文件Student.hbm.xml

8 通过创建类,将映射文件加到hibernate-cfg.xml中

 

 

PS:

hibernate配置hibernate.cfg.xml时,针对不同数据库的配法:

(1)oracle:

<session-factory>

<!-- Database connection settings -->

<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

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

<property name="connection.password">wfdb</property>
   <property name="dialect">org.hibernate.dialect.OracleDialect</property>

</session-factory>

(2)mysql:

<session-factory>

<!-- Database connection settings -->

<property name="connection.url">jdbc:mysql://localhost/数据库名(hibernate)</property>

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

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

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

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

</session-factory>

(3)sql server:

<session-factory>

<!-- Database connection settings -->

<property name="connection.url">

jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbname

</property>

<property name="connection.driver_class">

com.microsoft.sqlserver.jdbc.SQLServerDriver

</property>

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

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

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

</session-factory>

posted on 2013-04-23 16:20  小写K  阅读(246)  评论(0编辑  收藏  举报