1、maven方式使用
配置好.pom文件
1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.mybatis.generator</groupId> 5 <artifactId>mybatis-generator-maven-plugin</artifactId> 6 <version>1.3.5</version> 7 <dependencies> 8 <dependency> 9 <groupId>mysql</groupId> 10 <artifactId>mysql-connector-java</artifactId> 11 <version>5.1.21</version> 12 </dependency> 13 </dependencies> 14 </plugin> 15 </plugins> 16 </build>
①src/main/resources下创建 generatorConfig.xml,并配置好(自动生成的配置文件骨架)
1 <generatorConfiguration> 2 <context id="mysql" defaultModelType="hierarchical" 3 targetRuntime="MyBatis3Simple"> 4 <jdbcConnection connectionURL="jdbc:mysql:///chat" driverClass="com.mysql.jdbc.Driver" password="123456" userId="root"/> 5 <javaModelGenerator targetPackage="com.rr.mybatisgenerator.domain" targetProject="src/main/java"/> 6 <sqlMapGenerator targetPackage="com.rr.mybatisgenerator.mapper" targetProject="src/main/java"/> 7 <javaClientGenerator targetPackage="com.rr.mybatisgenerator.mapper" targetProject="src/main/java" type="XMLMAPPER"/> 8 <table schema="chat" tableName="product"> 9 </table> 10 </context> 11 </generatorConfiguration>
②src/main/java 下创建对应的domain和mapper包,也可以不创建
③右击项目 --> run as... --> maven build... --> Goals中写入命令 "mybatis-generator:generate"--> apply --> run -->....
④build success
2、eclipse 插件方式 ??? 不起作用 mybatis-generator-1.3.6
3、java 方式自动生成
①引入依赖
1 <dependency> 2 <groupId>org.mybatis</groupId> 3 <artifactId>mybatis</artifactId> 4 <version>3.4.1</version> 5 </dependency> 6 <dependency> 7 <groupId>org.mybatis.generator</groupId> 8 <artifactId>mybatis-generator-core</artifactId> 9 <version>1.3.6</version> 10 </dependency> 11 <dependency> 12 <groupId>mysql</groupId> 13 <artifactId>mysql-connector-java</artifactId> 14 <version>5.1.21</version> 15 </dependency>
②创建 generatorConfig.xml,内容同上,并配置好(自动生成的配置文件骨架)
③生成代码,注意配置文件的路径
1 @Test 2 public void Test() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { 3 4 List<String> warnings = new ArrayList<String>(); 5 boolean overwrite = true; 6 File configFile = new File("src/main/resources/generatorConfig.xml"); 7 ConfigurationParser cp = new ConfigurationParser(warnings); 8 Configuration config = cp.parseConfiguration(configFile); 9 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 10 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 11 myBatisGenerator.generate(null);}