• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Spring引入外部属性文件配置数据库连接的步骤详解

java 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论

直接配置数据库的信息

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,直接复制粘贴到当前目录就可以了。

(2)引入到当前项目。

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.springframew<b>本文来源gao@!dai!ma.com搞$$代^@码!网</b>ork.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>

完成以上步骤,就完成了引入外部属性文件配置数据库连接。

到此这篇关于Spring引入外部属性文件配置数据库连接的步骤详解的文章就介绍到这了,更多相关Spring外部属性文件内容请搜索搞代码以前的文章或继续浏览下面的相关文章希望大家以后多多支持搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring引入外部属性文件配置数据库连接的步骤详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址