Hibernate开发配置
Hibernate开发配置:
首先加入Hibernate和相应数据库所需要的jar包
接着配置Hibernate和数据库连接的配置文件hibernate.cfg.xml:(位于src目录下)
Hibernate加载其配置文件hibernate.properties和hibernate.cfg.xml,常用xml文件比较直观,方便管理, Hibernate 是一个流行的开源对象关系映射工具,方便连接不同数据库,更换数据库,只要修改Hibernate配置文件,大度减少项目的维护。
完整的配置如下:
<!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">org.hsqldb.jdbcDriver</property>//驱动的地址
<property name="connection.url">jdbc:hsqldb:hsql://localhost/test</property>//配置url,其中test为数据库的名字
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->用于配置适配器,用于移植其他数据库
<property name="dialect">org.hibernate.dialect.HSQLDialect</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>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>//这个属性设置成create的意思是,每次执行生成表的类时,所有的表都会删除然后再重建一次,这样表里面原来的数据就没了。可以设成update,这样只有表改动了才会删除表,然后重建表,如果表没有变,就不会这样做,这样数据就不会丢失。
<mapping resource="../*.hbm.xml"/>
</session-factory>
</hibernate-configuration>
常用数据库连接(包括适配器【方言】,驱动类,数据库url,用户和密码)
MySql 3/4/5:
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
Microsoft SQLServer (via jTDS):
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433/test;useCursors=true</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
IBM DB2:
<property name="connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="connection.url">jdbc:db2://localhost:50000/test</property>
<property name="connection.username">db2inst1</property>
<property name="connection.password"></property>
Oracle:
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:test</property>
<property name="connection.username">ora</property>
<property name="connection.password"></property>
Informix:
<property name="connection.driver_class">com.informix.jdbc.IfxDriver</property>
<property name="connection.url">jdbc:informix-sqli://localhost:1526/test:informixserver=server1</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.encoding">true</property>
Sybase (via jTDS):
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sybase://localhost:7100/test;useCursors=true</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
PostgreSQL:
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
MaxDB(SAPDB):
<property name="connection.driver_class">com.sap.dbtech.jdbc.DriverSapDB</property>
<property name="connection.url">jdbc:sapdb://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
接着配置日志文件:log4j.properties(位于src目录下),为了便于调试.(这个可以不配)
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout//除了这个不可以注释掉之外,其他的都可以注释掉
#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
### log schema export/update ###
#log4j.logger.org.hibernate.tool.hbm2ddl=debug
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace