配置数据库信息
方式一:hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/tax</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Spring中调用
<!-- 数据库会话工厂 spring调用hibernate.cfg.xml -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
方式二:db.properties
jdbcUrl=jdbc:mysql://127.0.0.1:3306/tax
driverClass=com.mysql.jdbc.Driver
user=root
password=123
initialPoolSize=10
maxPoolSize=30
Spring 中调用
<!-- 方式二 调用 db.properties文件 -->
<!-- 引入db.properties -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 将db.protperties的信息加入到连接池中 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- ${}调用db.properties中的数据 -->
<property name="jdbcUrl" value="${jdbcUrl}" ></property>
<property name="driverClass" value="${driverClass}" ></property>
<property name="user" value="${user}" ></property>
<property name="password" value="${password}" ></property>
<property name="initialPoolSize" value="${initialPoolSize}" ></property>
<property name="maxPoolSize" value="${maxPoolSize}" ></property>
<property name="minPoolSzie" value="3" ></property>
<!-- 最大空闲时间1800秒内部被使用就丢弃,若为0则永不丢弃 -->
<property name="maxIdleTime" value="1800" ></property>
<!-- 当连接池中的连接耗尽时候一起同时获取的连接是3个 -->
<property name="acquireIncrement" value="3" ></property>
</bean>
<!--配置sesionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<!-- 读取数据 -->
<property name="dataSource" ref="dataSource" ></property>
<!-- 配置hibernate属性信息 -->
<property name="hibernateProperties">
<!-- props属性集合 -->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl">update</prop>
<prop key="javax.persistence.validation.model">none</prop>
</props>
</property>