Tasks After Running MyBatis Generator

After you run MyBatis Generator (MBG), you will need to create or modify other MyBatis configuration artifacts. The main task is to create or modify a MapperConfig.xml file.

Updating the MapperConfig.xml File (MyBatis 3.x)

MyBatis 3.x uses an XML file, commonly named MapperConfig.xml, to specify information for a database connection, a transaction management scheme, and XML mapper files that will be used in a MyBatis session. MBG cannot create this file for you because it knows nothing about your execution environment. However, some of the items in this file relate directly to MBG generated items. Please refer to the standard MyBatis data mapper developer guide for details about the different configuration options.

MBG specific needs in the configuration file are as follows:

  • MBG generated XML mapper files must be listed

For example, suppose that MBG has generated an XML mapper file called MyTableMapper.xml, and that the file has been placed in the test.xml package of your project. The MapperConfig.xml file should have these entries:

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

  <configuration>
    <!-- Setup the transaction manager and data source that are
         appropriate for your environment
    -->
    <environments default"...">
      <environment id"...">
        <transactionManager type="...">
        </transactionManager>
        <dataSource type="...">
        </dataSource>
      </environment>
    </environments>

    <mappers>
      <!-- XML mapper files should be listed here -->
      <mapper resource="test/xml/MyTable_SqlMap.xml" />
    </mappers>

  </configuration>

If there is more than one XML mapper file (as is quite common), then the files can be listed in any order with repeated <mapper> elements after the <mappers> element.

If you use Java based configuration, or Spring, then you will need to register mappers with your configuration in the manner applicable for your environment.