Properties configuration in mybatis (recommended)

These properties are externally configurable and dynamically replaceable. They can be configured in a typical Java property file or passed through the child elements of the properties element. For example:

The attributes can be used in the whole configuration file to replace the attribute values that need to be dynamically configured. For example:

The username and password in this example will be replaced by the corresponding values set in the properties element. The driver and URL properties will be defined by config Replace with the corresponding value in the properties file. This provides many flexible options for configuration.

Property can also be passed to sqlsessionfactorybuilder In the build () method. For example:

If the properties are configured in more than one place, mybatis will be loaded in the following order:

The properties specified in the properties element body are read first.

Then read the property file under the classpath according to the resource attribute in the properties element or read the property file according to the path specified by the URL attribute, and overwrite the read property with the same name.

Finally, read the property passed as a method parameter and overwrite the read property with the same name.

Therefore, the attributes passed through the method parameters have the highest priority, followed by the configuration file specified in the resource / url attribute, and the attribute specified in the properties attribute has the lowest priority.

From mybatis 3.4 Starting with 2, you can specify a default value for the placeholder. For example:

This feature is off by default. If you want to specify a default value for the placeholder, you should add a specified property to turn on this feature. For example:

You can use ":" as the attribute key (e.g. DB: username) or you can also use the ternary operator of ognl expression (e.g. ${tablename! = null? Tablename: 'global_constants'}) in the SQL definition. You should change the characters of separation key and default value by adding a specified attribute. For example:

summary

The above is the properties configuration in mybatis introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>