These properties are externally configurable and dynamically replaceable, and can be configured in typical Java properties files or passed through child elements of properties elements. For example:
<properties resource="org/mybatis/example/config.properties"> <property name="username" value="dev_user"/> <property name="password" value="F2Fa3!33TYyg"/></properties>
The properties in it can be used throughout the configuration file to replace the property values that need to be configured dynamically. for example:
<dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/></dataSource>In this example, the username and password will be replaced by the corresponding values set in the properties element. The driver and url properties will be replaced by the corresponding values in the config.properties file. This provides many flexible options for configuration.
Properties can also be passed into the SqlSessionFactoryBuilder.build() method. For example:
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);// ... or ...SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environment, props);
If the properties are configured in more than one place, MyBatis will be loaded in the following order:
The attribute specified in the body of the properties element is first read.
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 of the same name.
Finally, the attribute passed as a method parameter is read and overwrite the read attribute of the same name.
Therefore, the properties passed through method parameters have the highest priority, followed by the configuration file specified in the resource/url property, and the lowest priority is the properties specified in the properties property.
Starting with MyBatis 3.4.2, you can specify a default value for placeholders. For example:
<dataSource type="POOLED"> <!-- ... --> <property name="username" value="${username:ut_user}"/> <!-- If 'username' property not present, username becomes 'ut_user' --></dataSource>This feature is turned off by default. If you want to specify a default value for the placeholder, you should add a specified property to enable this feature. For example:
<properties resource="org/mybatis/example/config.properties"> <!-- ... --> <property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/> <!-- Enable this feature --></properties>
You can use ":" as the attribute key (eg db:username) or you can use the ternary operator of OGNL expressions in the sql definition (eg ${tableName != null ? tableName : 'global_constants'}). You should change the characters that separate the keys and default values by adding a specified attribute. For example:
<properties resource="org/mybatis/example/config.properties"> <!-- ... --> <property name="org.apache.ibatis.parsing.PropertyParser.default-value-separator" value="?:"/> <!-- Change default value of separator --></properties><dataSource type="POOLED"> <!-- ... --> <property name="username" value="${db:username?:ut_user}"/></dataSource>Summarize
The above is the configuration of properties in MyBatis introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!