助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
# 运行 MyBatis Generator 后的任务 在您运行 MyBatis Generator (MBG)后, 您需要创建或者修改其他的 MyBatis 或 iBATIS 配置文件。 主要的任务如下: * 对 MyBatis 3.x: * 创建或修改 MapperConfig.xml 文件 * 对 iBATIS 2.x: * 创建或修改 SqlMapConfig.xml 文件 * 创建或修改 dao.xml 文件 (只有当您使用iBATIS DAO 框架时) 下面将详细地介绍每个任务。 ## 更新 MapperConfig.xml 文件 (MyBatis 3.x) MyBatis 3.x 使用了一个 XML 文件, 通常的名字是 `MapperConfig.xml`, 声明数据库连接信息, 事务管理方案, 和 将会用在MyBatis会话中的 XML mapper 文件。 MBG 不会为您创建这个文件,因为它对您执行环境一无所知。然而,一些在此文件中的项目直接涉及 MBG 生成项目。 请参阅标准 MyBatis 数据映射开发指南有关不同的配置选项的详细信息。 MBG 具体需要在配置文件中的有如下: * 必须列出MBG 生成的 XML 映射文件 例如,假设MBG已经生成一个名为`MyTableMapper.xml`的XML映射文件, 并且该文件已经在您的项目的`test.xml`包中。 `MapperConfig.xml` 文件应该有这些条目: ``` <?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> <!-- 安装适合于您的环境的事务管理器和数据源 --> <environments default"..."> <environment id"..."> <transactionManager type="..."> </transactionManager> <dataSource type="..."> </dataSource> </environment> </environments> <mappers> <!-- XML mapper 文件应该在这里列出来 --> <mapper resource="test/xml/MyTable_SqlMap.xml" /> </mappers> </configuration> ``` 如果有多个XML mapper文件(这很常见), 然后您可以通过反复的使用`&lt;mapper&gt;` 按任意顺序在`&lt;mappers&gt;`元素中列出这些文件。 **生成 MapperConfig.xml** 您可能会要MBG使用MapperConfigPlugin生成一个Mapper配置文件的骨架。 查看[&lt;插件&gt;](configreference/plugin.html)页面获取详细信息。 ## 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 `&lt;sqlMap&gt;` elements after the `&lt;transactionManager&gt;` 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 [&lt;plugin&gt;](configreference/plugin.html) page for more information. ## Updating the dao.xml File (iBATIS 2.x) **重要提示:** 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> ```