1、druid.properties
# mysql连接参数
jdbc.driver-class-name=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wangdb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&&useServerPrepStmts=true
jdbc.username=admin
jdbc.password=Admin@123
jdbc.initialSize=5
jdbc.minIdle=5
jdbc.maxActive=20
2、applicationContext.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
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:property-placeholder location="druid.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver-class-name}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
</bean>
<bean id="userDao" class="cn.tjhis.dao.impl.UserDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- <beans id="UserDaoImplrDao" class="cn.tjhis.factory.UserDaoFactoryBean" />-->
<bean id="userService" class="cn.tjhis.service.impl.UserServiceImpl" init-method="init" destroy-method="destroy">
<!-- <constructor-arg name="userDao" ref="userDao" />-->
<!-- <constructor-arg name="name" value="我"/>-->
<property name="userDao" ref="userDao"/>
</bean>
</beans>
推荐写法
- classpath*:表示不仅仅加载当前路径的,也包含依赖包中的配置文件
- system-properties-mode:表示系统属性的模式 never 不用系统属性,防止重名被覆盖掉
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>
标签:jdbc,java,配置文件,--,spring,mysql
From: https://www.cnblogs.com/his365/p/17178783.html