MyBatis configuration file example

Database connection configuration

<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

In the above example, we defined an developmentenvironment named using the JDBC transaction manager and the POOLED data source. Among them, driver, url, usernameand passwordspecify the database connection driver, connection URL, user name and password respectively.

Mapper configuration

<configuration>
    <mappers>
        <mapper resource="com/example/mapper/UserMapper.xml"/>
        <mapper class="com.example.mapper.OrderMapper"/>
    </mappers>
</configuration>

In the above example, we defined two mappers, namely com.example.mapper.UserMapper.xmland com.example.mapper.OrderMapper. Among them, resourcethe attribute specifies the path to the XML mapping file, and classthe attribute specifies the fully qualified name of the Java interface.

Global configuration

<configuration>
    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="multipleResultSetsEnabled" value="true"/>
        <setting name="useColumnLabel" value="true"/>
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <setting name="defaultStatementTimeout" value="25000"/>
        <setting name="defaultFetchSize" value="100"/>
    </settings>
</configuration>

In the above example, we defined some global configurations, including caching on, lazy loading on, allowing multiple result sets, using column labels, default executor type, default statement timeout, and default result set size.

Guess you like

Origin blog.csdn.net/qq_43597256/article/details/131192321
Recommended