使用MyBatis Generator生成DAO

虽然MyBatis很方便,但是想要手写全部的mapper还是很累人的,好在MyBatis官方推出了自动化工具,可以根据数据库和定义好的配置直接生成DAO层及以下的全部代码,非常方便.

 

需要注意的是,虽然自动化工具需要一个配置文件,但是MyBatis的配置文件仍然不能少,自动化工具的配置文件用于对生成的代码的选项进行配置,MyBatis的配置文件才是运行时的主要配置文件.

 

这个工具叫做MyBatis_Generator,不过比较扯淡的是官方虽然推出了这个工具,不过在google code上面看到的工具仅仅是一个jar包而已,在用这个工具的时候需要在cmd下面执行命令才能根据配置文件生成所需的各种文件,下面是该jar的下载地址:

http://mybatis.googlecode.com/files/mybatis-generator-core-1.3.1-bundle.zip

 

后来我仔细挖掘了Google Code上的东西,发现官方提供的也有可以直接安装到Eclipse里面的插件,唯一让人不爽的是这个插件只能安装在Eclipse3.6以上的版本,目前大部分人用的都是3.5.2或者更低的版本,给个地址吧,想方便点的可以试试,请确认你的eclipse的版本或者Myeclipse所用的eclipse版本是3.6以上的,否则无法安装,下面是eclipse更新地址:

http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/

 

下面我要说的是针对cmd下操作的方法,插件操作方法超级简单和abator(ibatis 2.X的插件)的使用方法一样.

 

在类路径下面建立generatorConfig.xml文件,这个文件配置各种生成选项,

 

Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <properties   url="file:///D:/softcode/mybatis.properties" />
	<classPathEntry location="${classPath}" />
	<context id="MBG" targetRuntime="MyBatis3" defaultModelType="conditional">	<!--targetRuntime 此属性用于指定生成的代码的运行目标。 -->
		<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
		<!-- 
		<plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
			<property name="fileName" value="GeneratedMapperConfig.xml" />
			<property name="targetPackage" value="com.easyway.app.core.mgr" />
			<property name="targetProject" value="../java" />
		</plugin>
		 -->
		<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">    
		    <property name="searchString" value="Example$" />    
		    <property name="replaceString" value="Criteria" />    
		</plugin>    
		<commentGenerator>
			<property name="suppressAllComments" value="false" />
			<property name="suppressDate" value="true" />  
		</commentGenerator>
		
		<jdbcConnection driverClass="${driverClass}" connectionURL="${connectionURL}" userId="${userName}" password="${password}"/>
		
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />											<!-- 类型解析器 -->
		</javaTypeResolver>

		<javaModelGenerator targetPackage="${modelPackage}" targetProject="../java">	<!-- 实体类 -->
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />  
		</javaModelGenerator>

		<sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="../resources">	<!-- 实体类SQL映射文件 -->
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<javaClientGenerator type="XMLMAPPER" targetPackage="${daoMapperPackage}" targetProject="../java">
			<property name="enableSubPackages" value="true" />											<!-- 接口 -->
		</javaClientGenerator>


		<table  catalog="TBS"   tableName="TEST_TEST" domainObjectName="LoginLog"
			enableCountByExample="true" enableUpdateByExample="true"
			enableDeleteByExample="true" enableSelectByExample="true"
			selectByExampleQueryId="true">
		</table>

	</context>
</generatorConfiguration>
 
 

需要注意的是上面的targetProject此处应写成文件路径的形式,而不是项目路径,指定类文件生成到src的org.qiuqiu.vo包下面,如果是用插件的话targetProject直接写项目名称即可,比如直接写MyBatis_Generator.

上面我仅仅简单的写了些注释,稍后我会将详细的配置选项以附件的形式发上来

 

然后就开始要生成文件了,打开cmd,进入项目路径的lib下面,也就是含有mybatis-generator-core-1.3.1.jar文件的目录中,执行以下命令:

 

classPath=ojdbc14-10.2.0.2.jar

targetProject=xxxxx
driverClass=oracle.jdbc.driver.OracleDriver

connectionURL=jdbc:oracle:thin:@10.100.102.8:1521:xxx
userName=xxx

password=123456


modelPackage=xxxxxxxxxxxxxxxxxxxxxxxxxx
sqlMapperPackage=mybatis/mappers

daoMapperPackage=xxxxxxxxxxxxx

 maven自动生成配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>generator</groupId>
  <artifactId>generator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <properties>
       <configurationFile>generatorConfig.xml</configurationFile>
   </properties>
   
  <build>
  	<plugins>
  		 <plugin>
      	  <groupId>org.mybatis.generator</groupId>
      	  <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.0</version>
          <configuration>
	          <configurationFile>generatorConfig.xml</configurationFile>
	          <verbose>true</verbose>  
	          <overwrite>true</overwrite>
          </configuration>
          <executions>
            <execution>
              <id>Generate MyBatis Artifacts</id>
              <goals>
                <goal>generate</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
  	</plugins>
  </build>
</project>

 

Cmd
  1. java -jar mybatis-generator-core-1.3.1.jar -configfile ../src/generatorConfig.xml -overwrite

OK了,刷新一遍目录,可以看到vo,dao,xml全都自动生成了.

 

附件中有一个示例项目已经含有了所需要的全部包,

另一个附件是配置选项的官方说明文档

再奉上一个MyBatis的官方eclipse插件,可以安装在eclipse3.6版本以上

猜你喜欢

转载自topmanopensource.iteye.com/blog/1936580