Tasks After Running MyBatis Generator

After you run MyBatis Generator (MBG), you will need to create or modify other MyBatis or iBATIS configuration artifacts. The main tasks are as follows:

  • For MyBatis 3.x:
    • Create or Modify the MapperConfig.xml file
  • For iBATIS 2.x:
    • Create or Modify the SqlMapConfig.xml file
    • Create or modify the dao.xml file (only if using the iBATIS DAO Framework)

Each task is described in detail below.

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.

Generated MapperConfig.xml You may ask MBG to generate a skeleton Mapper Configuration file with the MapperConfigPlugin. See the <plugin> page for more information.

Updating the SqlMapConfig.xml File (iBATIS 2.x)

iBATIS 2 uses an XML file, commonly named SqlMapConfig.xml, to specify information for a database connection, a transaction management scheme, and SQL map XML files that will be used in an iBATIS session. MBG cannot create this file for you because MBG 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 iBATIS data mapper developer guide for details about the different configuration options.

MBG specific needs in the configuration file are as follows:

  • Statement namespaces must be enabled
  • MBG generated SQL Map XML files must be listed

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

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE sqlMapConfig
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

  <sqlMapConfig>
    <!-- Statement namespaces are required for MBG -->
    <settings useStatementNamespaces="true" />

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

    <!-- SQL Map XML files should be listed here -->
    <sqlMap resource="test/xml/MyTable_SqlMap.xml" />

  </sqlMapConfig>

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

Version 1.2 New Enhancement With MBG version 1.2 and later, You may ask MBG to generate a skeleton SQL Map Configuration file with the SqlMapConfigPlugin. See the <plugin> page for more information.

Updating the dao.xml File (iBATIS 2.x)

Important Note: this step is only required if you generated DAOs for the deprecated iBATIS DAO framework (we suggest using Spring instead).

The iBATIS DAO framework is configured by an xml file commonly called dao.xml. The iBATIS DAO framework uses this file to control the database connection information for DAOs, and also to list the DAO implementation classes and DAO interfaces. In this file you should specify the path to your SqlMapConfig.xml file, and all the MBG generated DAO interfaces and implementation classes.

For example, suppose that MBG has generated a DAO interface called MyTableDAO and a implementation class called MyTableDAOImpl, and that the files have been placed in the test.dao package of your project. The dao.xml file should have these entries:

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE daoConfig
   PUBLIC "-//ibatis.apache.org//DTD DAO Configuration 2.0//EN"
   "http://ibatis.apache.org/dtd/dao-2.dtd">

  <daoConfig>
    <context>
      <transactionManager type="SQLMAP">
        <property name="SqlMapConfigResource"
                  value="test/SqlMapConfig.xml"/>
      </transactionManager>

      <!-- DAO interfaces and implementations should be listed here -->
      <dao interface="test.dao.MyTableDAO"
           implementation="test.dao.MyTableDAOImpl" />

    </context>
  </daoConfig>