一、本課目標(biāo)
- 能夠使用屬性文件配置數(shù)據(jù)源
- 能夠使用JNDI數(shù)據(jù)源
二、配置數(shù)據(jù)源
2.1使用屬性文件配置數(shù)據(jù)源
- 數(shù)據(jù)庫信息寫在屬性文件中
-
采用PropertyPlaceholderConfigurer可以引入屬性文件,在Spring配置文件中采用注入${url}的方式引用屬性值。
image.png
image.png
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 配置數(shù)據(jù)源 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:database.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations">
<list>
<value>classpath:cn/smbms/dao/**/*.xml</value>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.smbms.dao"></property>
</bean>
<!-- Service -->
<context:component-scan base-package="cn.smbms.service.user"></context:component-scan>
<!-- 事務(wù)管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 事務(wù)增強 -->
<!-- <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" timeout="1000"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> -->
<!-- 配置切面 -->
<!-- <aop:config>
<aop:pointcut expression="execution(* cn.smbms.service..*.*(..))" id="myPoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint"/>
</aop:config> -->
</beans>
dataSource.properties文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username=root
password=41312019
2.2使用JNDI數(shù)據(jù)源
dbcp的數(shù)據(jù)源相當(dāng)于是針對于當(dāng)前這個項目的,只能為當(dāng)前項目所使用,其他項目不能共享;而jndi數(shù)據(jù)源是存儲在服務(wù)器之中的,服務(wù)器中可能會有很多項目,而這很多項目都可以去共享這個數(shù)據(jù)源。
通過JNDI從服務(wù)器容器中獲取DataSource資源
- 在服務(wù)器環(huán)境中配置JNDI數(shù)據(jù)源
- 在Spring配置文件引用JNDI資源

image.png
配置JNDI數(shù)據(jù)源步驟:
1、在tomcat服務(wù)器文件中:
conf——context.xml下面增加一段代碼:

