1、首先在pom.xml中添加如何配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<!-- 设置对auto-config.properties,jdbc.properties进行过虑,即这些文件中的${key}会被替换掉为真正的值 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>jdbc.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<jdbc.driver>test.dirver</jdbc.driver>
<jdbc.url>test.url</jdbc.url>
<jdbc.username>test.username</jdbc.username>
<jdbc.password>test.password</jdbc.password>
</properties>
</profile>
<profile>
<id>dev</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<jdbc.driver>dev.dirver</jdbc.driver>
<jdbc.url>dev.url</jdbc.url>
<jdbc.username>dev.username</jdbc.username>
<jdbc.password>dev.password</jdbc.password>
</properties>
</profile>
<profile>
<id>product</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<jdbc.driver>product.dirver</jdbc.driver>
<jdbc.url>product.url</jdbc.url>
<jdbc.username>product.username</jdbc.username>
<jdbc.password>product.password</jdbc.password>
</properties>
</profile>
</profiles>
2、修改配置文件
jdbc.driver = ${jdbc.driver}
jdbc.url = ${jdbc.url}
jdbc.username = ${jdbc.username}
jdbc.password = ${jdbc.password}
说明:配置文件中${key} key即对应pom.xml中的jdbc.driver,jdbc.url,jdbc.username,jdbc.password四个选项。