hibernate配置文件有多种方式。
今天记录两种配置,一种比较基础,另一种专业一些。
第一种,初学的时候用的,即hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="connection.url"> jdbc:mysql://localhost:3306/test </property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="format_sql">true</property> <property name="show_sql">true</property> <property name="hibernate.id.new_generator_mappings">true</property> <property name="current_session_context_class">thread</property> <mapping resource="entity\Category.hbm.xml" />
<!-- 此处使用了hibernate映射文件的方式与数据库表连接
如果使用annotation配置,即<mapping class="entity.Category">
-->
</session-factory> </hibernate-configuration>
第二种使用c3p0连接池管理,这种配置方法在使用spring框架时更为常见。
首先配置c3p0-config.xml,这个文件名是固定的,只有这样才会被自动加载。
<c3p0-config> <default-config> <property name="initialPoolSize">3</property><!-- default 3 between minPoolSize and maxPoolSize --> <property name="minPoolSize">3</property><!-- default 3 --> <property name="maxPoolSize">15</property><!-- default 15 --> <property name="maxConnectionAge">0</property><!-- default 0 最长周期时间 --> <property name="maxIdleTime">0</property><!-- default 0 最长闲置时间 --> <property name="maxStatements">0</property><!-- default 0 --> <property name="maxStatementsPerConnection">0</property><!-- default 0 --> <property name="checkoutTimeout">0</property><!-- default 0ms --> <property name="idleConnectionTestPeriod">0</property><!-- default 0 --> <property name="testConnectionOnCheckin">false</property><!-- default false --> <property name="testConnectionOnCheckout">false</property><!-- default false --> <property name="numHelperThreads">3</property><!-- default 3 --> <property name="acquireIncrement">3</property><!-- default 3 --> <property name="acquireRetryAttempts">30</property><!-- default 30 --> <property name="acquireRetryDelay">1000</property><!-- default 1000ms --> <property name="breakAfterAcquireFailure">false</property><!-- default false --> <property name="autoCommitOnClose">false</property><!-- default false --> <property name="connectionTesterClassName">com.mchange.v2.c3p0.impl.DefaultConnectionTester</property><!-- Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester --> <property name="forceIgnoreUnresolvedTransactions">false</property><!-- default false -not recommend --> <property name="propertyCycle">0</property><!-- default 0 --> <property name="debugUnreturnedConnectionStackTraces">false</property><!-- default false --> <property name="unreturnedConnectionTimeout">0</property><!-- default 0 --> </default-config> <!-- define different datasources --> <named-config name="c3p0DataSource"> <property name="driverClass">net.sourceforge.jtds.jdbc.Driver</property> <property name="jdbcUrl">jdbc:jtds:sqlserver://WN7X64-DZD9X2X;DatabaseName=bms_java</property> <property name="user">TIP_JAVA</property> <property name="password">@JavaSummer12</property> </named-config> </c3p0-config>
配置完c3p0-config.xml,就要在applicationConfig.xml中配置bean了
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <constructor-arg name="configName" value="c3p0DataSource" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="c3p0DataSource" /> <property name="hibernateProperties"> <value> hibernate.dialect = org.hibernate.dialect.SQLServerDialect hibernate.show_sql = true hibernate.format_sql = true </value> </property> <property name="annotatedClasses"> <list> <value>bean.Book</value> <value>bean.BookApply</value> <value>bean.BorrowInfo</value> <value>bean.Catagory</value> <value>bean.User</value> </list> </property> </bean> <bean id="baseDao" class="com.dell.bms.dao.impl.BaseDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean>