逆向工程就是说通过数据库当中的表生成class,mapper,接口,不需要自己编写那些,很方便。跟symfony里面的自动生成是一样的;视频里的人说用的不多,但我觉得很方便呀
具体步骤,首先导入MyBatis-generator-core.jar,
之后复制一下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> <!--数据库驱动--> <!-- 如果IDE(eclipse或者idea) 项目里导入了jar包,那么就不需要配置了jar包的绝对路径了 <classPathEntry location="e:/project/mybatis/lib/mysql-connector-java-5.0.8-bin.jar"/> --> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="false"/> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection userId="root" driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis_generator?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT" password="wen52010"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model类存放位置--> <javaModelGenerator targetPackage="Entity" targetProject="src"> <!-- <property name="enableSubPackages" value="true"/>--> <!-- <property name="trimStrings" value="true"/>--> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="Mapper" targetProject="src"> <!-- <property name="enableSubPackages" value="true"/>--> </sqlMapGenerator> <!--生成Dao类存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="Mapper" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="Student"></table> <table tableName="Adress"></table> <!--生成对应表及类名--> <!-- <table tableName="category_" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false">--> <!-- <property name="my.isgen.usekeys" value="true"/>--> <!-- <generatedKey column="id" sqlStatement="JDBC"/>--> <!-- </table>--> <!-- <table tableName="product_" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> --> </context> </generatorConfiguration>
将路径名,表明等更改正确,
main函数之中,复制以下代码,运行就Ok,
File file = new File("src/generator.xml"); List<String> warnings = new ArrayList<String>(); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(file); DefaultShellCallback callback = new DefaultShellCallback(true); MyBatisGenerator generator = new MyBatisGenerator(config,callback,warnings); generator.generate(null);
原文:https://www.cnblogs.com/eenio/p/11329720.html