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>
<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>
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可以删除了;运行测试成功;
Spring的DataSource配置、将Hibernate配置全部写到Spring配置,布布扣,bubuko.com
Spring的DataSource配置、将Hibernate配置全部写到Spring配置
原文:http://blog.csdn.net/jerome_s/article/details/26546533