逆向工程的创建:
导入jar包(在pom.xml):
<!--逆向工程--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency>
在resources文件夹内创建generator.xml
在generator.xml文件中配置:
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="simple" targetRuntime="MyBatis3"> <!--添加在生成文件时不需要注释--> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:///mybatis?characterEncoding=utf8" userId="root" password="123"/> <!--实体类的保存位置以及包名--> <javaModelGenerator targetPackage="com.cn.bean" targetProject="src/main/java"/> <!--映射文件保存位置以及保存文件的文件夹名称--> <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources"/> <!--接口的保存位置以及接口包名--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.cn.dao" targetProject="src/main/java"/> <!--哪些表格要逆向生成--> <table tableName="t_user" /> <table tableName="t_code" /> <table tableName="t_order" /> <table tableName="t_detail" /> <table tableName="t_goods" /> <table tableName="t_order_goods" /> </context> </generatorConfiguration>
调用junit实现逆向工程:
@Test public void testGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //file路径为generator.xml的本地绝对路径 File configFile = new File("D:\\javaidea\\week1\\reversal\\src\\main\\resources\\generator.xml"); 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); }