Spring引入外部数据源

spring-dataSource.xml

数据源配置文件





    

    
        
        
        
        

    

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 {
    @Test
    public void testDataSource() throws SQLException {
     ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-datasource.xml");
        DruidDataSource dataSource = ioc.getBean(DruidDataSource.class);
        System.out.println(dataSource.getConnection());
    }
}

你可能感兴趣的:(ssm,spring,java,后端)