Spring的DataSource配置、将Hibernate配置全部写到Spring配置

DataSource可以集中管理数据库连接,减少维护工作量,使部署更简单;
Spring的DataSource配置:(Spring数据源配置)这里使用dbcp,还有很多其他的如c3p0,jdbc,jndi等
DataSource和定义普通Bean一样,指定id、类别,注入属性;
站点停止时,应该调用DataSource的close方法,通过destroy-method属性配置实现;

1,在Spring配置文件配置DataSource的bean:

[html]  view plain  copy
  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  2.     <property name="username" value="${jdbc.username}">property>  
  3.     <property name="password" value="${jdbc.password}">property>  
  4.     <property name="url" value="${jdbc.url}">property>  
  5.     <property name="driverClassName" value="${jdbc.driver}" >property>  
  6. bean>  

class="org.apache.commons.dbcp.BasicDataSource" :是DataSource实现类
配置SessionFactory ,注入:

[html]  view plain  copy
  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  2.           
  3.           
  4.         <property name="dataSource" ref="dataSource">property>   
  5.     bean>  

2,数据库连接信息写在属性文件中,在src新建配置文件:jdbc.properties

[html]  view plain  copy
  1. jdbc.driver = com.mysql.jdbc.Driver  
  2. jdbc.url = jdbc:mysql://127.0.0.1:3306/jboa  
  3. jdbc.username = root  
  4. jdbc.password = jerome  
在Spring配置文件引入jdbc.properties配置文件,采用PropertyPlaceholderConfigurer可以引入属性文件,在Spring配置文件中采用${jdbc.url}的方式引用属性值

[html]  view plain  copy
  1. <bean id="DBconfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.         <property name="locations">  
  3.             <value>classpath:jdbc.propertiesvalue>  
  4.         property>  
  5.     bean>  
接下来可以把一下的Hibernat的配置数据连接信息注释:

[html]  view plain  copy
  1. <property name="connection.url">jdbc:mysql://127.0.0.1:3306/jboaproperty>  
  2. <property name="connection.username">rootproperty>  
  3. <property name="connection.password">jeromeproperty>  
  4. <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>   
3,配置hibernateProperties属性,用到了Property类型的属性的配置配置SessionFactory :

[html]  view plain  copy
  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  2.       
  3.       
  4.     <property name="dataSource" ref="dataSource">property>  
  5.     <property name="hibernateProperties">  
  6.         <props>  
  7.             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>  
  8.             <prop key="hibernate.hbm2ddl.auto">falseprop>  
  9.             <prop key="hibernate.show_sql">trueprop>  
  10.         props>  
  11.     property>  
  12. bean>  
Hibernate可以注释:

[html]  view plain  copy
  1. <property name="dialect">org.hibernate.dialect.MySQLDialectproperty>  
  2. <property name="hbm2ddl.auto">falseproperty>  
  3. <property name="show_sql">trueproperty>  
5,通过mappingDirectoryLocations 或者mappingLocations属性配置映射文件位置,把所有的资源文件引进Spring配置:配置SessionFactory

[html]  view plain  copy
  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  2.           
  3.           
  4.         <property name="dataSource" ref="dataSource">property>  
  5.         <property name="hibernateProperties">  
  6.             <props>  
  7.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>  
  8.                 <prop key="hibernate.hbm2ddl.auto">falseprop>  
  9.                 <prop key="hibernate.show_sql">trueprop>  
  10.             props>  
  11.         property>  
  12.           
  13.         <property name="mappingResources">  
  14.             <list>  
  15.                 <value>com/jboa/model/Account.hbm.xmlvalue>  
  16.                 <value>com/jboa/model/CheckResult.hbm.xmlvalue>  
  17.                 <value>com/jboa/model/ClaimVoucher.hbm.xmlvalue>  
  18.                 <value>com/jboa/model/ClaimVoucherDetail.hbm.xmlvalue>  
  19.                 <value>com/jboa/model/Department.hbm.xmlvalue>  
  20.                 <value>com/jboa/model/Dictionary.hbm.xmlvalue>  
  21.                 <value>com/jboa/model/Employee.hbm.xmlvalue>  
  22.                 <value>com/jboa/model/Postion.hbm.xmlvalue>  
  23.             list>  
  24.         property>  
DataSource指定一个方法关闭:


配置好了。Hibernate可以删除了;运行测试成功

你可能感兴趣的:(后端编程)