【参考】:maven pom.xml加载不一样properties配置[转]mysql
首先 看看效果:web
点开咱们项目中的Maven projects 后,会发现右侧 咱们profile有个可勾选选项。默认勾选localhost。localhost对应项目启动后,会加载配置左侧localhost文件夹下面的jdbc.perperties 和 redis.properties。 对于项目中存在测试数据库或者运行数据库的区分的时候 咱们能够经过勾选快速切换。那么咱们是如何实现的呢redis
一:在左侧文件目录项目 配置相对于的文件目录,而且注释掉你以前的加载 ,如图所示,spring
配置好所需的jdbc 连接 须要注意的 (正确写法)url=jdbc:mysql://127.0.0.1:3306/course_design?useUnicode=true&characterEncoding=UTF-8 中间的间隔符 必须用 & 替换掉以前写的 (错误写法)url=jdbc:mysql://127.0.0.1:3306/course_design?useUnicode=true&characterEncoding=UTF-8sql
具体缘由参考 Caused by: org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 84; 对实体 "characterEncoding"数据库
二:配置 pom.xmlapache
<!--配置profiles--> <profiles> <profile> <id>localhost</id> <!--默认勾选设置--> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>localhost</env> </properties> </profile> <profile> <id>dev</id> <properties> <env>dev</env> </properties> </profile> <profile> <id>master</id> <properties> <env>master</env> </properties> </profile> </profiles> <build> ...................................... </build>
<build>
............................. <!--指定待加载的文件位置--> <filters> <filter>configs/${env}/jdbc.properties</filter> <filter>configs/${env}/redis.properties</filter> </filters> <!--指定须要这些加载数据的 配置文件位置--> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
理论上 这个时候 以及在pom,xml中 配置完成了 点击编译以后 若是 正常是能够的 app
但我这里报错了 3 字节的 UTF-8 序列的字节 3 无效 后来查询资料 发现还须要配置一个utf-8编译 【参考】http://blog.csdn.net/lzupb/article/details/53008481 maven
<!-- resource插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin>
其实后来 还有一个问题 就是测试
这个时候 前面介绍的jdbc url写法就解决了 把& 变成 &; 就行了