数据库连接与properties文件
管理properties数据库:
现在pom文件中加入Druid的坐标:
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency>
配置文件中添加相应的数据(driverClassName:驱动类名
driverClassName: com.mysql.jdbc.Driver # mysql-connector-java 5.x及之前版本中的驱动类名
driverClassName: com.mysql.cj.jdbc.Driver # mysql-connector-java 6.x及后续版本中的驱动类名
url中的spring_db就是当前连接库中的一个数据库,username:账号,password:密码)
<bean class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_db"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean>
各种参数可以参考:https://blog.csdn.net/qq_37960603/article/details/83415215
测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;public class TestDay04 {public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("TT08.xml");DataSource dataSource=(DataSource) applicationContext.getBean("dataSource");System.out.println(dataSource);}
}
结果:
{CreateTime:"2023-03-01 10:59:30",ActiveCount:0,PoolingCount:0,CreateCount:0,DestroyCount:0,CloseCount:0,ConnectCount:0,Connections:[]
}Process finished with exit code 0
但是这样就将各种参数写死了,于是就有properties文件来解决
properties:
创建一个.properties
文件:
注意: 添加参数名时尽量不要与系统存在的名字重名,如:username=play123
,当写一个显示username的程序时,运行出来的结果就会是系统的名字,而不是play123
;当然,有解决办法:在用context加载properties文件所使用的语句中(从此往下数第三张图)添加不显示系统属性的语句:system-properties-mode="NEVER"
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=123456
- 开启context空间
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"></beans>
- 用context空间加载上面创建的以
.properties
为后缀名的文件(location:需要的properties文件。可以添加多个,文件之间加个,
就行,如此还有很多,如下:)
- 第二个:当两个文件内有同名的时候,不会引起冲突,会自动引用排名靠后的文件
- 最后一个:是只会加载当前工程,不会去加载当前工程所要依赖的jar包等
<context:property-placeholder location="jdbc.properties,jdbc1.properties" system-properties-mode="NEVER"></context:property-placeholder>
- 将配置文件中的数据用通配符代替:
${}
,并且在{}中写入properties文件的jdbc.driver
,jdbc.url
等名,如此,程序运行后会根据上述名在properties文件中进行查找
<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>
结果:
{CreateTime:"2023-03-01 12:42:11",ActiveCount:0,PoolingCount:0,CreateCount:0,DestroyCount:0,CloseCount:0,ConnectCount:0,Connections:[]
}Process finished with exit code 0