Ibatis 简明教程

iBATOR - Introduction

iBATOR 是一个 iBATIS 的代码生成器。iBATOR 会对一张或多张数据库表进行内省,并且生成可用于访问这些表的 iBATIS 工件。

iBATOR is a code generator for iBATIS. iBATOR introspects one or more database tables and generates iBATIS artifacts that can be used to access the tables.

之后你可以编写自己的自定义 SQL 代码或存储过程来满足你的需求。iBATOR 生成了以下工件 −

Later you can write your custom SQL code or stored procedure to meet your requirements. iBATOR generates the following artifacts −

  1. SqlMap XML Files

  2. Java Classes to match the primary key and fields of the table(s)

  3. DAO Classes that use the above objects (optional)

iBATOR 可以作为一个独立的 JAR 文件,或作为一个 Ant 任务,或作为一个 Eclipse 插件来运行。本教程描述了从命令行生成 iBATIS 配置文件的最简单方式。

iBATOR can run as a standalone JAR file, or as an Ant task, or as an Eclipse plugin. This tutorial describes the simplest way of generating iBATIS configuration files from command line.

Download iBATOR

如果你使用的是除了 Eclipse 之外的 IDE,下载独立 JAR。独立 JAR 包含了一个运行 iBATOR 的 Ant 任务,或者你也可以从 Java 代码的命令行中运行 iBATOR。

Download the standalone JAR if you are using an IDE other than Eclipse. The standalone JAR includes an Ant task to run iBATOR, or you can run iBATOR from the command line of Java code.

  1. You can download zip file from Download iBATOR.

  2. You can check online documentation − iBATOR Documentation.

Generating Configuration File

要运行 iBATOR,请执行以下步骤:

To run iBATOR, follow these steps −

Step 1

适当创建并填写配置文件 ibatorConfig.xml。至少必须指定以下内容:

Create and fill a configuration file ibatorConfig.xml appropriately. At a minimum, you must specify −

  1. A <jdbcConnection> element to specify how to connect to the target database.

  2. A <javaModelGenerator> element to specify the target package and the target project for the generated Java model objects.

  3. A <sqlMapGenerator> element to specify the target package and the target project for the generated SQL map files.

  4. A <daoGenerator> element to specify the target package and the target project for the generated DAO interfaces and classes (you may omit the <daoGenerator> element if you don’t wish to generate DAOs).

  5. At least one database <table> element

NOTE − 有关 iBATOR 配置文件的示例,请参阅 XML Configuration File Reference 页面。

NOTE − See the XML Configuration File Reference page for an example of an iBATOR configuration file.

Step 2

将该文件保存到便于查找的位置,例如:\temp\ibatorConfig.xml。

Save the file in a convenient location, for example, at: \temp\ibatorConfig.xml.

Step 3

现在从命令行运行 iBATOR,如下所示:

Now run iBATOR from the command line as follows −

java -jar abator.jar -configfile \temp\abatorConfig.xml -overwrite

这将告知 iBATOR 使用配置文件运行。它还将告知 iBATOR 覆盖同名的任何现有 Java 文件。如果您要保存任何现有 Java 文件,则省略 −overwrite 参数。

It will tell iBATOR to run using your configuration file. It will also tell iBATOR to overwrite any existing Java files with the same name. If you want to save any existing Java files, then omit the −overwrite parameter.

如果出现冲突,iBATOR 会以唯一名称保存新生成的文件。

If there is a conflict, iBATOR saves the newly generated file with a unique name.

运行 iBATOR 后,需要创建或修改标准 iBATIS 配置文件,以利用新生成代码。下一部分对此进行了说明。

After running iBATOR, you need to create or modify the standard iBATIS configuration files to make use of your newly generated code. This is explained in the next section.

Tasks After Running iBATOR

运行 iBATOR 后,需要创建或修改其他 iBATIS 配置工件。主要任务如下:

After you run iBATOR, you need to create or modify other iBATIS configuration artifacts. The main tasks are as follows −

  1. Create or modify the SqlMapConfig.xml file.

  2. Create or modify the dao.xml file (only if you are using the iBATIS DAO Framework).

下面详细描述每个任务:

Each task is described in detail below −

Updating the SqlMapConfig.xml File

iBATIS 使用名为 SqlMapConfig.xml 的 XML 文件来指定连接数据库的信息、事务管理方案以及 iBATIS 会话中使用的 SQL 映射 XML 文件。

iBATIS 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 are used in an iBATIS session.

iBATOR 无法为您创建此文件,因为它不知道您的执行环境。但是,此文件中的某些项直接关系到 iBATOR 生成的项。

iBATOR 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 iBATOR generated items.

iBATOR 配置文件中的特定需求如下 -

iBATOR specific needs in the configuration file are as follows −

  1. Statement namespaces must be enabled.

  2. iBATOR generated SQL Map XML files must be listed.

例如,假设 iBATOR 生成了一个名为 MyTable_SqlMap.xml 的 SQL Map XML 文件,并且该文件已被放入您的项目的 test.xml 包中。SqlMapConfig.xml 文件应具有以下条目 -

For example, suppose iBATOR 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 Abator -->
   <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>

如果有不止一个 SQL Map XML 文件(这是很常见的),那么可以在 <transactionManager> 元素之后使用重复的 <sqlMap> 元素以任意顺序列出这些文件。

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.

Updating the dao.xml File

iBATIS DAO 框架由一个通常称为 dao.xml 的 xml 文件配置。

The iBATIS DAO framework is configured by an xml file commonly called dao.xml.

iBATIS DAO 框架使用这个文件来控制 DAO 的数据库连接信息,并且还可以列出 DAO 实现类和 DAO 接口。

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.

在这个文件中,您应该指定 SqlMapConfig.xml 文件的路径,以及所有 iBATOR 生成的 DAO 接口和实现类。

In this file, you should specify the path to your SqlMapConfig.xml file, and all the iBATOR generated DAO interfaces and implementation classes.

例如,假设 iBATOR 生成了一个名为 MyTableDAO 的 DAO 接口和一个名为 MyTableDAOImpl 的实现类,并且这些文件已被放入您项目的 test.dao 包中。

For example, suppose iBATOR has generated a DAO interface called MyTableDAO and an implementation class called MyTableDAOImpl, and that the files have been placed in the test.dao package of your project.

dao.xml 文件应该具有以下条目 -

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>

NOTE − 仅当您为 iBATIS DAO 框架生成了 DAO 时,才需要此步骤。

NOTE − This step is required only if you generated DAOs for the iBATIS DAO framework.