Spring的DataSource配置、將Hibernate配置全部寫到Spring配置
DataSource可以集中管理資料庫連線,減少維護工作量,使部署更簡單;
Spring的DataSource配置:(Spring資料來源配置)這裡使用dbcp,還有很多其他的如c3p0,jdbc,jndi等
DataSource和定義普通Bean一樣,指定id、類別,注入屬性;
站點停止時,應該呼叫DataSource的close方法,通過destroy-method屬性配置實現;
class="org.apache.commons.dbcp.BasicDataSource" :是DataSource實現類
配置SessionFactory ,注入:
2,資料庫連線資訊寫在屬性檔案中,在src新建配置檔案:jdbc.properties
在Spring配置檔案引入jdbc.properties配置檔案,採用PropertyPlaceholderConfigurer可以引入屬性檔案,在Spring配置檔案中採用${jdbc.url}的方式引用屬性值接下來可以把一下的Hibernat的配置資料連線資訊註釋:3,配置hibernateProperties屬性,用到了Property型別的屬性的配置配置SessionFactory :Hibernate可以註釋:5,通過mappingDirectoryLocations 或者mappingLocations屬性配置對映檔案位置,把所有的資原始檔引進Spring配置:配置SessionFactoryDataSource指定一個方法關閉:
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
- <bean id="DBconfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:jdbc.properties</value>
- </property>
- </bean>
- <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>
- <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>
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hbm2ddl.auto">false</property>
- <property name="show_sql">true</property>
- <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>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
配置好了。Hibernate可以刪除了;執行測試成功
相關文章
- Spring Boot + Mybatis + Spring MVC環境配置(三):DataSource配置Spring BootMyBatisMVC
- spring datasource 配置及事務管理Spring
- Spring中如何配置Hibernate事務Spring
- Struts +Spring + Hibernate 的配置問題,謝謝!Spring
- Struts2,Hibernate,Spring 環境配置Spring
- 配置SpringSpring
- Spring配置Spring
- Spring 的容器配置Spring
- Spring Boot & 配置Spring Boot
- Spring配置注入Spring
- Spring RMI配置Spring
- metaq spring配置Spring
- Spring 配置 ActiveMQSpringMQ
- Struts2+hibernate+spring配置程式整合下載Spring
- spring 配置sqlite的方式SpringSQLite
- spring的配置問題Spring
- Spring Cloud 覆寫遠端的配置屬性SpringCloud
- Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis Generator配置Spring BootMyBatisMVC
- struts string和hibernate 如何配置dataSource為連線池
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- 使用Maven配置SpringMavenSpring
- spring boot 配置 JPASpring Boot
- Spring IOC XML配置SpringXML
- MyBatis Spring SqlSessionFactoryBean 配置MyBatisSpringSQLSessionBean
- Spring MVC DispatcherServlet 配置SpringMVCServlet
- Spring中配置WebSocketSpringWeb
- Spring 配置繼承Spring繼承
- Spring Boot核心配置Spring Boot
- Spring配置事務Spring
- Spring--Java配置SpringJava
- Spring配置說明Spring
- spring相關配置Spring
- Spring配置之OpenSessionInViewFilterSpringSessionViewFilter
- Spring配置相關Spring
- Spring的四種宣告式事務的配置-Hibernate事務Spring
- 擴充套件Spring——使用 Annotation將配置資源注入到Bean中套件SpringBean
- 將Hibernate配置到Weblogic JNDI上的詳細步驟Web
- spring cloud 配置中心git和本地地址配置SpringCloudGit