怎么在Spring中引入外部属性文件配置数据库连接

发布时间:2021-01-18 14:27:55 作者:Leah
来源:亿速云 阅读:129

怎么在Spring中引入外部属性文件配置数据库连接?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

直接配置数据库的信息

xml配置文件直接配置:

<?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:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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="root" ></property>
  </bean>

</beans>

一般不会这样用,不便于修改,我们看下面的引入外部属性文件配置的方法

引入外部属性文件配置数据库连接

1.引入德鲁伊连接池jar包

(1)导入进来一个druid-1.0.9.jar,直接复制粘贴到当前目录就可以了。

怎么在Spring中引入外部属性文件配置数据库连接

(2)引入到当前项目。

怎么在Spring中引入外部属性文件配置数据库连接
怎么在Spring中引入外部属性文件配置数据库连接

2.配置德鲁伊连接池

(1)新建一个jdbc.properties文件,写数据库的相关信息。
jdbc.properties:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/userDb?characterEncoding=utf8&useUnicode=true&useSSL=false
jdbc.username=root
jdbc.password=root

(2)新建一个配置文件。

bean6.xml:

<?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:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


  <!--引入外部的属性文件-->
  <context:property-placeholder location="classpath:jdbc.properties"/>

  <!--配置连接池-->
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

    <property name="driverClassName" value="${jdbc.driverClass}"></property>
    <property name="url" value="${jdbc.url}" ></property>
    <property name="username" value="${jdbc.username}" ></property>
    <property name ="password" value="${jdbc.password}" ></property>

  </bean>

</beans>

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 配置OSPF引入自治系统外部路由
  2. 使用外部属性文件和spring的事件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring 数据库

上一篇:怎么在PostgreSQL中打印日志信息的源文件

下一篇:selenium设置窗口大小不准确的解决方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》