1. 程式人生 > >Mybatis generator 逆向生成程式碼

Mybatis generator 逆向生成程式碼

簡單介紹

本文介紹用mybatis逆向生成javaben dao介面

1.建立maven專案 建立相應的包

附上專案建立完成的圖片

然後在pom.xml檔案裡面引入需要的jar的依賴

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">;
<modelVersion>4.0.0</modelVersion>
<groupId>com.xx</groupId>
<artifactId>Mybaits</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Mybaits Maven Webapp</name>
<url>

" rel="nofollow">http://maven.apache.org</url>;
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

org.mybatis mybatis 3.4.0
org.mybatis.generator mybatis-generator-core 1.3.5 com.oracle ojdbc6 0.0.1-SNAPSHOT Mybaits 然後在src/main/resources下建立generator所需的配置檔案 配置檔案裡面有些地方需要改動,需要的朋友可以根據自己的情況改動 在這附上配置檔案內容
複製程式碼 然後在com.test包下建立逆向生成的啟動java檔案 啟動檔案內容看下面的程式碼 複製程式碼 package com.test; import java.io.File; 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.internal.DefaultShellCallback; public class Test { public static void main(String[] args) throws Exception { List warnings = new ArrayList (); boolean overwrite = true; // 配置檔案路徑切記寫對 File configFile = new File("src/main/resources/mbg.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); } } 配置檔案寫完之後直接執行此配置檔案,就可以生成程式碼,下面附上生成之後的效果(執行完之後,重新整理專案 就可以看到生成的檔案) 文章中涉及到的配置檔案,啟動類等都可以在mybatis官網找到。 如有錯誤,請大神指導。