Spring的DataSource配置:(Spring数据源配置)这里使用dbcp,还有很多其他的如c3p0,jdbc,jndi等
DataSource和定义普通Bean一样,指定id、类别,注入属性;
站点停止时,应该调用DataSource的close方法,通过destroy-method属性配置实现;
1,在Spring配置文件配置DataSource的bean:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driver}" ></property> </bean>
class="org.apache.commons.dbcp.BasicDataSource" :是DataSource实现类
配置SessionFactory ,注入:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> --> <!-- 注入一个DataSource --> <property name="dataSource" ref="dataSource"></property> </bean>
2,数据库连接信息写在属性文件中,在src新建配置文件:jdbc.properties
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://127.0.0.1:3306/jboa jdbc.username = root jdbc.password = jerome在Spring配置文件引入jdbc.properties配置文件,采用PropertyPlaceholderConfigurer可以引入属性文件,在Spring配置文件中采用${jdbc.url}的方式引用属性值
<bean id="DBconfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean>接下来可以把一下的Hibernat的配置数据连接信息注释:
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/jboa</property> <property name="connection.username">root</property> <property name="connection.password">jerome</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property>3,配置hibernateProperties属性,用到了Property类型的属性的配置配置SessionFactory :
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> --> <!-- 注入一个DataSource --> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">false</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>Hibernate可以注释:
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hbm2ddl.auto">false</property> <property name="show_sql">true</property>5,通过mappingDirectoryLocations 或者mappingLocations属性配置映射文件位置,把所有的资源文件引进Spring配置:配置SessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> --> <!-- 注入一个DataSource --> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">false</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/jboa/model/Account.hbm.xml</value> <value>com/jboa/model/CheckResult.hbm.xml</value> <value>com/jboa/model/ClaimVoucher.hbm.xml</value> <value>com/jboa/model/ClaimVoucherDetail.hbm.xml</value> <value>com/jboa/model/Department.hbm.xml</value> <value>com/jboa/model/Dictionary.hbm.xml</value> <value>com/jboa/model/Employee.hbm.xml</value> <value>com/jboa/model/Postion.hbm.xml</value> </list> </property>DataSource指定一个方法关闭:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
配置好了。Hibernate可以删除了;运行测试成功;