1.首先准备好jar包
https://github.com/mybatis/generator/releases 下载MyBatis Generator
下载压缩包后,打开可以看到lib目录下有我们需要的jar包,添加到项目引用
2.和Hibernate逆向生成一样,这里也需要一个配置文件:
generator.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> <!-- 数据库驱动包的位置,我这里是放到D盘的 --> <classPathEntry location="D:\mysql-connector-java-5.1.0-bin.jar" /> <context id="Mysql2Tables" targetRuntime="MyBatis3"> <!-- 数据库驱动,连接url,用户名,密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.144.100:3306/networkdept" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成实体类的包名和位置 --> <javaModelGenerator targetPackage="cn.networkdepartment.pojo" targetProject="src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成dao接口的包名和位置 --> <sqlMapGenerator targetPackage="cn.networkdepartment.dao" targetProject="src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成的映射文件包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.networkdepartment.dao" targetProject="src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成的那些表(更改tableName 和domainObjectName 就可以了) --> <table schema="test" tableName="nd_class" domainObjectName="NClass" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="nd_integral" domainObjectName="Integral" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="nd_log" domainObjectName="Nlog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="nd_maintain" domainObjectName="Maintain" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="nd_member" domainObjectName="Member" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="nd_post" domainObjectName="Npost" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table schema="test" tableName="nd_systemset" domainObjectName="Systemset" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
3.创建一个类通过main方法生成代码,运行这个类
package test; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; /** * 创建测试类,加载配置文件自动生成dao,Mapper文件 * @author Bryce * */ public class MybatisGeneratorUtil { public static void main(String[] args) { try { System.out.println("start generator ..."); List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File(MybatisGeneratorUtil.class.getResource("/generator.xml").getFile()); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); System.out.println("end generator!"); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
然后就可以看到实体类和dao都生成了