当前位置: 首页 > news >正文

Spring引入外部数据源

spring-dataSource.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: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/context https://www.springframework.org/schema/context/spring-context.xsd"><!--    引入jdbc.properties,之后可以通过${key}的方式访问value--><context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--    创建德鲁伊外部数据源经常会用到,以后spring整合mybatis,只要spring中有数据源,mybatis就可以直接用
--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean>
</beans>

jdbc.properties

配置数据库的信息:

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC&useSSL=false
jdbc.username=root
jdbc.password=********

DataSourceTest.java

测试连接数据库:通过IOC容器管理当前的配置的数据源

package com.zh.spring.test;import com.alibaba.druid.pool.DruidDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.sql.SQLException;public class DataSourceTest {@Testpublic void testDataSource() throws SQLException {ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-datasource.xml");DruidDataSource dataSource = ioc.getBean(DruidDataSource.class);System.out.println(dataSource.getConnection());}
}

http://www.lryc.cn/news/105609.html

相关文章:

  • word里的页码问题
  • ​LeetCode解法汇总142. 环形链表 II
  • 危化品行业防雷检测综合解决方案
  • 刷题笔记:day 1
  • Linux——平台设备及其驱动
  • 【C语言技巧】三种多组输入的写法
  • DB2数据库巡检脚本
  • Eureka 学习笔记3:EurekaHttpClient
  • Android Framework 之 启动流程
  • Qt、C/C++环境中内嵌LUA脚本、实现LUA函数的调用执行
  • 超详细 | 模拟退火算法及其MATLAB实现
  • 在线餐饮油烟实时监测系统的设计与实现
  • 7-2 凯撒密码 (20分)
  • LeetCode_贪心算法_中等_763.划分字母区间
  • 【算法提高:动态规划】1.5 状态压缩DP TODO
  • 建网站一般使用Windows还是liunx好?
  • NodeJs后端项目使用docker打包部署
  • ARM单片机中断处理过程解析
  • 关于SEDEX会员与平台的相关问题汇总
  • 解读Spring-context的property-placeholder
  • 【Rust】枚举类型创建单链表以及常见的链表操作方法
  • Excel 两列数据中相同的数据进行同行显示
  • Windows本地安装配置Qcadoo MES系统
  • 涛思数据与拾贝云达成战略合作,携手赋能工业数字化转型
  • nginx 配置多域名多站点 Ubuntu
  • Docker实践:使用Docker搭建个人开发环境(极简版)
  • SQL从三个表中根据时间分别查询并汇总数量一行展示
  • 同样是跨端框架,React会不会被VUE取代?
  • Excel·VBA定量装箱、凑数值金额、组合求和问题
  • 通过Jmeter压测存储过程