一、在pom.xml文件加入
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
二、然后用profiles配置不同的属性
<project>
<profiles>
<profile>
<id>mysql</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<jdbc.url>jdbc:mysql://localhost:3306/ppt?useUnicode=true&characterEncoding=UTF-8</jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password>root</jdbc.password>
<hibernate.dialect>org.hibernate.dialect.MySQL5Dialect</hibernate.dialect>
</properties>
</profile>
<profile>
<id>sqlServer</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<jdbc.url>jdbc:sqlserver://localhost:1433;databaseName=ppt</jdbc.url>
<jdbc.username>sa</jdbc.username>
<jdbc.password>123456</jdbc.password>
<hibernate.dialect>org.hibernate.dialect.SQLServer2008Dialect</hibernate.dialect>
</properties>
</profile>
</profiles>
</project>
三、然后再*.propertise用${jdbc.username}就能获取到用户名
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
hibernate.dialect=${hibernate.dialect}
四、在spring配置文件中就可以通过${jdbc.username}获取数据库链接的用户名