Mybatis 属性配置文件 application.properties

来源:互联网 发布:mac os x 10.11 优化 编辑:程序博客网 时间:2024/05/17 08:33

属性配置元素可以将配置值具体化到一个属性文件中,,并且使用属性文件的 key 名作为占位符。在上述的配置中,我们将数据库连接属性具体化到了 application.properties 文件中,并且为 driver,URL 等属性使用了占位符。
在 applications.properties 文件中配置数据库连接参数,如下所示:

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisdemojdbc.username=rootjdbc.password=admin

在 mybatis-config.xml 文件中,为属性使用 application.properties 文件中定义的占位符:

<properties resource="application.properties"><property name="jdbc.username" value="db_user" /><property name="jdbc.password" value="verysecurepwd" /></properties><dataSource type="POOLED"><property name="driver" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></dataSource>

并且,你可以在元素中配置默认参数的值。如果中定义的元素和属性文件定义元素的 key
值相同,它们会被属性文件中定义的值覆盖。

原创粉丝点击