hibernate連接數(shù)據(jù)庫(kù)的配置文件為hibernate.cfg.xml,下面列出了連接三種數(shù)據(jù)庫(kù)(sql server、oracle、mysql)時(shí),hibernate.cfg.xml的必要配置。
連接mysql的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?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> <!-- 配置數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <property name= "connection.driver_class" > com.mysql.jdbc.driver </property> <!-- 配置數(shù)據(jù)庫(kù)連接url --> <property name= "connection.url" > jdbc:mysql: //localhost:3306/mysqldb </property> <!-- 數(shù)據(jù)庫(kù)user --> <property name= "connection.username" >root</property> <!-- 數(shù)據(jù)庫(kù)user密碼 --> <property name= "connection.password" >admin</property> <!-- 配置jdbc內(nèi)置連接池 --> <property name= "connection.pool_size" > 1 </property> <!-- 配置數(shù)據(jù)庫(kù)方言 --> <property name= "dialect" > org.hibernate.dialect.mysqldialect </property> <!-- 輸出運(yùn)行時(shí)生成的sql語句 --> <property name= "show_sql" > true </property> <!-- 配置對(duì)輸出的sql語句進(jìn)行格式化 --> <property name= "format_sql" > true </property> <!-- 配置映射文件 --> <mapping resource= "com/model/user.hbm.xml" /> </session-factory> </hibernate-configuration> |
連接oracle的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?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> <!-- 配置數(shù)據(jù)庫(kù)的驅(qū)動(dòng)類 --> <property name= "connection.driver_class" > oracle.jdbc.driver.oracledriver </property> <!-- 配置數(shù)據(jù)庫(kù)的連接路徑 --> <property name= "connection.url" > jdbc:oracle:thin: @127 .0. 0.1 : 1521 :dbsql </property> <!-- 配置數(shù)據(jù)庫(kù)的連接用戶名 --> <property name= "connection.username" >personnel_manage</property> <!-- 配置數(shù)據(jù)庫(kù)的連接密碼,密碼為空時(shí)也可以省略該行配置代碼 --> <property name= "connection.password" >mwq</property> <!-- 配置數(shù)據(jù)庫(kù)使用的方言 --> <property name= "dialect" > org.hibernate.dialect.oracledialect </property> <!-- 配置在控制臺(tái)顯示sql語句 --> <property name= "show_sql" > true </property> <!-- 配置對(duì)輸出的sql語句進(jìn)行格式化 --> <property name= "format_sql" > true </property> <!-- 配置在輸出的sql語句前面添加提示信息 --> <property name= "use_sql_comments" > true </property> <!-- 配置持久化類映射文件 --> <mapping resource= "com/chen/entity/users.hbm.xml" /> </session-factory> </hibernate-configuration> |
連接sql server配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?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> <!--配置數(shù)據(jù)庫(kù)jdbc驅(qū)動(dòng)--> <property name= "hibernate.connection.url" >jdbc:microsoft:sqlserver: //127.0.0.1:1433;databasename=db_manpower</property> <!--配置數(shù)據(jù)庫(kù)連接url--> <property name= "hibernate.connection.driver_class" >com.microsoft.jdbc.sqlserver.sqlserverdriver</property> <!--配置數(shù)據(jù)庫(kù)用戶名--> <property name= "hibernate.connection.username" >sa</property> <!--配置數(shù)據(jù)庫(kù)密碼--> <property name= "hibernate.connection.password" /> <!--輸出運(yùn)行時(shí)生成的sql語句--> <property name= "show_sql" >ture</property> <!--配置數(shù)據(jù)庫(kù)方言--> <property name= "hibernate.dialect" >org.hibernate.dialect.sqlserverdialect</property> <!--配置連接池個(gè)數(shù)--> <property name= "hibernate.jdbc.batch_size" > 16 </property> <!--列出映射文件--> <mapping resource= "com/chen/entity/users.hbm.xml" /> </session-factory> </hibernate-configuration> |
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接