1.首先在pom文件裏面加dependency依賴java
<dependencies> <!--mybatis自帶的結合spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring}</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.8</version> </dependency> </dependencies>
2.在pom文件裏面加入插件依賴mysql
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <executions> <execution> <id>attach-source</id> <goals> <goal>jar</goal> </goals> <configuration> <includes> <include>**/org/**</include> </includes> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>attach-jar</id> <goals> <goal>jar</goal> </goals> <configuration> <mainfest> <addClasspath>true</addClasspath> <classpathPrefix></classpathPrefix> <mainClass>org.mybatis.generator.api.ShellRunner</mainClass> </mainfest> <includes> <include>**/org/**</include> </includes> </configuration> </execution> </executions> </plugin> <!-- <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptors> 描述文件路徑 <appendAssemblyId>false</appendAssemblyId> <descriptor>src/main/resources/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> 綁定到package生命週期階段上 <phase>package</phase> <goals> 綁定到package生命週期階段上 <goal>single</goal> </goals> </execution> </executions> </plugin> --> </plugins> </build>
3.建立generatorConfig.xmlspring
<?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>
<context id="context1">
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3309/test"
userId="root" password="3309"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--targetProgect:生成PO類的位置-->
<javaModelGenerator targetPackage="comm.po" targetProject=".\src">
</javaModelGenerator>
<!--targetPackage:mapper文件生成的位置-->
<sqlMapGenerator targetPackage="comm.mapper" targetProject=".\src"/>
<javaClientGenerator targetPackage="comm.client" targetProject=".\src" type="XMLMAPPER"/>
<!--連接數據庫生成的表-->
<table schema="" tableName="user" domainObjectName="UserInfo"></table>
</context>
</generatorConfiguration>
4.建立自動生成逆向工程的類sql
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; /** * mybatis逆向工程 * Created by fwj on 16-3-18. */ public class Generator { public void generator() throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("generatorConfig.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); } public static void main(String[] args) throws Exception { try { Generator generatorSqlmap = new Generator(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }