zoukankan      html  css  js  c++  java
  • IOC(十二)

    常见的是配置数据库文件, 以配置Druid连接池为例, 直接配置连接池方式如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--直接配置连接池(需要先导入相关依赖)-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/userDB"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123"></property>
        </bean>
    </beans>

    但是这样写的话, 数据库的配置信息就写死在xml文件里了, 不利于后期维护, 所以我们采用导入外部文件的方式:

    先创建数据库的配置文件:

     再在xml文件中进行配置:(需要先配置context名称空间)

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--引入外部文件-->
        <context:property-placeholder location="classpath:com/ryan/spring5/autowire/jdbc.properties"/>
        <!--根据外部文件配置数据库-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${prop.dirverClass}"></property>
            <property name="url" value="${prop.url}"></property>
            <property name="username" value="${prop.userName}"></property>
            <property name="password" value="${prop.password}"></property>
        </bean>
    </beans>
  • 相关阅读:
    Vue学习之webpack中使用vue(十七)
    Vue学习之Babel配置(十六)
    Vue学习之webpack调用第三方loader(十五)
    JAVA基础之设置随机成语验证码
    JAVA基础之HttpServletResponse响应
    Vue学习之npm常用命令及参数小结(十四)
    EXCEL 查找某个字符在字符串中最后一次出现的位置
    SQLSERVER存储过程基本语法
    SQL SERVER 字符串函数 STRING_SPLIT()
    SQL SERVER 字符串函数 PATINDEX()
  • 原文地址:https://www.cnblogs.com/Ryan368/p/13902843.html
Copyright © 2011-2022 走看看