Skip to content

Latest commit

 

History

History
63 lines (58 loc) · 2.06 KB

mybatis-xml-configuration.md

File metadata and controls

63 lines (58 loc) · 2.06 KB

Back to Home

The Overview of MyBatis XML Configuration

  1. Data Source Configuration: You can specify the details of your database connection, such as driver class, connection URL, username, and password.

    <configuration>
      <environments default="development">
        <environment id="development">
          <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
            <property name="username" value="root"/>
            <property name="password" value="password"/>
          </dataSource>
        </environment>
      </environments>
    </configuration>
  2. Mappers Configuration: You define the mappings between your SQL statements and Java methods in mapper XML files. You can specify the location of these files in the MyBatis configuration.

    <mappers>
        <mapper resource="com/example/mapper/EmployeeMapper.xml"/>
        <mapper resource="com/example/mapper/DepartmentMapper.xml"/>
    </mappers>
  3. Type Aliases: You can define aliases for your Java classes to simplify mapping and querying.

    <typeAliases>
      <typeAlias alias="Employee" type="com.example.model.Employee"/>
      <typeAlias alias="Department" type="com.example.model.Department"/>
    </typeAliases>
  4. Plugins: MyBatis provides a plugin mechanism to intercept and modify the behavior of its components. You can configure plugins in the XML file.

    <plugins>
      <plugin interceptor="com.example.plugins.MyCustomPlugin">
        <property name="property1" value="value1"/>
        <property name="property2" value="value2"/>
      </plugin>
    </plugins>
  5. Global Settings: You can configure various global settings such as logging, cache behavior, and lazy loading.

    <settings>
      <setting name="logImpl" value="SLF4J"/>
      <setting name="cacheEnabled" value="true"/>
      <setting name="lazyLoadingEnabled" value="true"/>
    </settings>