使用MyBatis Generator生成DAO



使用MyBatis Generator生成DAO

Help–Install new software–add
在弹出框中填入generator,地址是http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
然后选中搜索出的结果,下一步直到安装结束。
在src/main/resources下添加 名为generatorConfig.xml的配置文件,内容如下:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
<?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>
<!– 数据库驱动 –>
<classPathEntry
location=”D:/mysql-connector-java-5.1.22-bin.jar” />
<context id=”DB2Tables” targetRuntime=”MyBatis3″>
<!– 用来除去时间信息的,这在配合类似subversion的代码管理工具时使用很有效 –>
<commentGenerator>
<property name=”suppressDate” value=”true” />
<!–是否去除自动生成的注释true:是:false:否 –>
<property name=”suppressAllComments” value=”true” />
</commentGenerator>
<!–数据库链接URL,用户名、密码 –>
<jdbcConnection driverClass=”com.mysql.jdbc.Driver”
connectionURL=”jdbc:mysql://10.15.172.108:3306/userdata” userId=”userdata”
password=”43f59a7e5d”>
</jdbcConnection>
<!– 类型转换的信息 –>
<javaTypeResolver>
<property name=”forceBigDecimals” value=”false” />
</javaTypeResolver>
<!– 生成模型的包名和位置 –>
<javaModelGenerator targetPackage=”test.model”
targetProject=”DBToJavaBean”>
<property name=”enableSubPackages” value=”true” />
<property name=”trimStrings” value=”true” />
</javaModelGenerator>
<!– 生成映射文件的包名和位置 –>
<sqlMapGenerator targetPackage=”test.mapping”
targetProject=”DBToJavaBean”>
<property name=”enableSubPackages” value=”true” />
<property name=”trimStrings” value=”true” />
</sqlMapGenerator>
<!– 生成DAO的包名和位置 –>
<javaClientGenerator type=”XMLMAPPER”
targetPackage=”test.dao” targetProject=”DBToJavaBean”>
<property name=”enableSubPackages” value=”true” />
</javaClientGenerator>
<!– 要生成哪些表 –>
<table tableName=”position_data” domainObjectName=”PositionData”
enableCountByExample=”false” enableUpdateByExample=”false”
enableDeleteByExample=”false” enableSelectByExample=”false”
selectByExampleQueryId=”false”></table>
<!– <table tableName=”user” domainObjectName=”UserDto” enableCountByExample=”false”
enableUpdateByExample=”false” enableDeleteByExample=”false” enableSelectByExample=”false”
selectByExampleQueryId=”false”></table> <table tableName=”syslogs” domainObjectName=”SyslogsDto”
enableCountByExample=”false” enableUpdateByExample=”false” enableDeleteByExample=”false”
enableSelectByExample=”false” selectByExampleQueryId=”false”></table> –>
</context>
</generatorConfiguration>

然后再该配置文件上右键,Generator Mybatis/IBatis Aftifacts,刷新目录,即可看到生成的文件。
有时候会出现如下错误path for project must have only one segment
修改方法是,将targetProject改成项目的工程名,而不是src/main/java或../src/main/java