加入新的依赖:
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.30version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.0.31version>
dependency>
不能直接使用DataSource,因为它是一个接口,而我们开始学习IOC容器时,就说过,IOC是通过我们class所设置的类型来获取这个类型的class对象,再通过调用newInstance()方法调用无参构造创建对象,但接口是没有构造方法的,因此这里我们不能使用接口, 而应该使用该接口的实现类, DataSource的实现类是com.alibaba.druid.pool.DruidDataSource
创建spring-datasources.xml文件:
<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="dataSources" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm">property>
<property name="username" value="root">property>
<property name="password" value="root">property>
bean>
beans>
创建测试类获取连接对象:
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 DataSources_text {
@Test
public void testDataSources() throws SQLException {
ApplicationContext ioc=new ClassPathXmlApplicationContext("spring-datasources.xml");
DruidDataSource druidDataSource= ioc.getBean(DruidDataSource.class);
System.out.println(druidDataSource.getConnection());
}
}
输出如下:
获取成功!
关于数据库的连接,我们除了可以像上述这种写法一样将其写在.xml文件中之外,我们还可以新创建个properties文件,如下所示:
创建完成之后,我们只需要将关于数据库连接的代码写入其中即可!
如下所示:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm
username=root
password=root
但如果properties文件过多的话,很容易出现重名问题,对此,我们的解决办法是给每个属性+前缀
,如下所示;
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root
而对于.xml文件和properties文件来说,他们之间是没有任何关系的,如果我们想使用properties中的数据,那么需要将其引入到.xml文件中去,如何引入?如下所示,新创建一个bean标签,通过类引入,但这个类上面加了一条横线,那么则表示该类是过时的,是不推荐使用的
既然不推荐使用,那么我们就不用了呗,spring为我们提供了更简单的方式,如下所示:
<context:property-placeholder location="jdbc.properties">context:property-placeholder>
引入之后,该文件中的数据如何使用呢?
方法和在mybatis中学习过的相同,通过${key}的方式,如下所示:
<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>
修改spring-dataSource.xml如下:
<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/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<context:property-placeholder location="jdbc.properties">context:property-placeholder>
<bean id="dataSources" 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>
获取成功!