DataSource can centrally manage database connections, reduce maintenance effort, and make deployment easier;
Spring DataSource configuration: (Spring data source configuration) use dbcp here, there are very many other such as C3p0,jdbc,jndi, etc.
DataSource, like defining ordinary beans, specifies the ID, category, and injection attributes;
When the site stops, you should call the Close method of datasource and configure the implementation through the Destroy-method property;
1, configure the DataSource bean in the Spring configuration file:
<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": Is the DataSource implementation class
Configure Sessionfactory, inject:
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" ><!--< Property Name= "Configlocation" value= "Classpath:hibernate.cfg.xml" ></property>--><!-- Inject a dataSource--><property name= "DataSource" ref= "DataSource" ></property> </bean>
2, database connection information is written in the properties file, in src new configuration file: Jdbc.properties
Jdbc.driver = Com.mysql.jdbc.Driverjdbc.url = Jdbc:mysql://127.0.0.1:3306/jboajdbc.username = Rootjdbc.password = Jerome
Introduction of the Jdbc.properties configuration file in the spring configuration file, the use of propertyplaceholderconfigurer to introduce a property file, in the spring configuration file in the form of ${jdbc.url} to reference the value of the property
<bean id= "Dbconfig" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >< Property Name= "Locations" ><value>classpath:jdbc.properties</value></property></bean>
Next you can gaze at the Hibernat configuration data connection information:
3, configure the Hibernateproperties property, use the property type of the properties of the configuration configuration sessionfactory:
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" ><!--< Property Name= "Configlocation" value= "Classpath:hibernate.cfg.xml" ></property>--><!-- Inject a 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 can gaze at:
<property name= "dialect" >org.hibernate.dialect.mysqldialect</property><property name= " Hbm2ddl.auto ">false</property><property name=" Show_sql ">true</property>
5, through the Mappingdirectorylocations or Mappinglocations properties to configure the mapping file location, the entire resource file introduced spring configuration: Configuration Sessionfactory
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" ><!--< Property Name= "Configlocation" value= "Classpath:hibernate.cfg.xml" ></property>--><!-- Inject a 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 specifies a method to close:
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
The configuration is ready. Hibernate is able to delete; perform test success;
Spring DataSource configuration, hibernate configuration all write to spring configuration