一、灵活配置DataSource

一、本课目标

  • 能够使用属性文件配置数据源
  • 能够使用JNDI数据源

二、配置数据源

2.1使用属性文件配置数据源

  • 数据库信息写在属性文件中
  • 采用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">
    
    <!-- 配置数据源 -->
    <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>
    <!-- 事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <tx:annotation-driven transaction-manager="txManager"/>
    
    <!-- 事务增强 -->
    <!-- <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数据源

dbcp的数据源相当于是针对于当前这个项目的,只能为当前项目所使用,其他项目不能共享;而jndi数据源是存储在服务器之中的,服务器中可能会有很多项目,而这很多项目都可以去共享这个数据源。
通过JNDI从服务器容器中获取DataSource资源

  • 在服务器环境中配置JNDI数据源
  • 在Spring配置文件引用JNDI资源
image.png

配置JNDI数据源步骤:
1、在tomcat服务器文件中:
conf——context.xml下面增加一段代码:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容