1. 下载mybatis generator插件
下载地址:https://github.com/mybatis/generator/releases
下载完成后,解压,将features和plugins文件夹的内容复制到eclipse的相应文件夹中,重启eclipse即可。
2. 使用插件
选中添加generatorConfig文件的项目,右键new–>other
生成的generatorConfig.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> <!-- classPathEntry:数据库的JDBC驱动的jar包地址 --> <classPathEntry location="D:\software\eclipse\workspace\UserRegister\WebContent\WEB-INF\lib\mysql-connector-java-5.1.22-bin.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 抑制警告 --> <property name="suppressTypeWarnings" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> <!-- 是否生成注释代时间戳 --> <property name="suppressDate" value="true" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="root" password="root"> </jdbcConnection> <javaModelGenerator targetPackage="com.demo.domain" targetProject="UserRegister\src"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.demo.mapper" targetProject="UserRegister\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.demo.dao" targetProject="UserRegister\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --> <!-- <table schema="untodo" tableName="T_USER" domainObjectName="User"/> --> <!-- 要生成那些表(更改tableName和domainObjectName就可以) --> <!-- <table schema="untodo" tableName="T_USER" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> --> <!--生成对应表及类名 --> <table schema="general" tableName="T_USERINFO" domainObjectName="User"> <!--domain字段的命名规则,false:默认为驼峰命名 true:按数据库真实命名 --> <property name="useActualColumnNames" value="false" /> <!-- 忽略列,不生成bean 字段 --> <!-- <ignoreColumn column="FRED" /> --> <!-- 指定列的java数据类型 --> <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> </table> </context> </generatorConfiguration>
PS:
targetProject的配置如果写绝对路径可能出错: Project D: does not exist
targetProject写项目名称即可
3. 生成代码文件
选中generatorConfig.xml文件,右键选择Generate MyBatis/IBATIS Artifacts即可
由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包
以下是相关文件截图:
和Hibernate逆向生成一样,这里也需要一个配置文件:
generatorConfig.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>
<!--数据库驱动-->
<classPathEntry location="mysql-connector-java-5.0.8-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="lcw.model" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
上面配置文件中的:
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)。
生成语句文件:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
2、使用方法
在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。
看下效果图: