最近在學Mybatis,類似Hibernate,Mybatis也有逆工程可以直接生成代碼(mapping,xml,pojo),方便快速開發。用的是mybatis-generator-core-1.3.2.jar這個架包。這里我用的是mysql數據庫。
1.下載mybatis-generator-core-1.3.2.jar和mysql-connector-java-5.1.13-bin.jar,大家可以在這里下載http://maven.outofmemory.cn/org.mybatis.generator/mybatis-generator-core/1.3.2/
2.新建一個文件夾,把第1步下載的mybatis-generator-core-1.3.2.jar和mysql-connector-java-5.1.13-bin.jar移到該文件夾內,在文件夾的根目錄新建src文件夾。

3.在文件夾根目錄新建1個txt文本文檔,寫上代碼:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
然后將txt文本文檔的文件名后綴改為bat。
4.新建generatorConfig.xml 并在里面配置逆工程信息如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfigurationPUBLIC "-//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.1.13-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:3306/login" userId="root" password="root"></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false"/></javaTypeResolver><!-- 配置生成的pojo實體類 --> <javaModelGenerator targetPackage="tse.model" targetProject="src"><property name="enableSubPackages" value="true"/><property name="trimStrings" value="true"/></javaModelGenerator><!-- 配置生成的xml --><sqlMapGenerator targetPackage="tse.mapping" targetProject="src"><property name="enableSubPackages" value="true"/></sqlMapGenerator><!-- 配置生成的mapping接口 --><javaClientGenerator type="XMLMAPPER" targetPackage="tse.mapping" targetProject="src"><property name="enableSubPackages" value="true"/></javaClientGenerator><!-- 配置逆工程的表,tableName可用通配符%匹配所有表 --><table tableName="login" domainObjectName="Login" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table></context></generatorConfiguration>
記得修改jdbcConnection標簽的數據庫連接的配置和table標簽的tableName屬性,如果你數據庫中所有表都想逆工程,可以直接設置tableName值為%,即匹配所有表,不過此時domainObjectName屬性就要去掉。
好了,通過以上步驟,整個目錄結構應該是這樣的

而src文件夾還是個空文件夾
此時運行根目錄下的bat文件,在src目錄中可看到生成了你要的代碼

以上所述是小編給大家介紹的Mybatis逆工程的使用相關知識,主要是介紹逆工程的使用,這時候有朋友就會問,那我怎么讓逆工程生成自己定義的代碼格式呢。不用急,下一篇我會講mybatis-generator-core-1.3.2.jar架包的修改和打包。感興趣的朋友繼續關注本站!
新聞熱點
疑難解答