利用MyBatis Generator Core 逆向生成数据库表和映射类

建立Maven工程

工程目录如下

利用MyBatis Generator Core 逆向生成数据库表和映射类_第1张图片

pom.xml配置如下


  4.0.0

  genData
  genData
  0.0.1-SNAPSHOT
  jar

  genData
  http://maven.apache.org

  
    UTF-8
  

  
    
	
	    junit
	    junit
	    4.12
	    test
	

	
	
	    org.mybatis.generator
	    mybatis-generator-core
	    1.3.7
	
	
	
	    javax.xml.bind
	    com.springsource.javax.xml.bind
	    2.2.0
	    provided
	
		
	
	    org.apache.logging.log4j
	    log4j-core
	    2.11.2
	
	
	
	
  

generatorConfig.xml配置如下





    
    

    
    
    
        
        
        
        
        
        
        

        
        
        
        
        

        

        
        
            
             
        

        
        
        				
        
        
            
            
        

        
        
            
            
        
        
        
        
            
        
        
        
        
            
        
              
        
        
            
            
        

生成代码如下gen.java

package main;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
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.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;



public class gen {

	public static void main(String[] args)  {
		// TODO Auto-generated method stub
		 List warnings = new ArrayList();
		 boolean overwrite = true;
		 File configFile = new File("generatorConfig.xml");
		 ConfigurationParser cp = new ConfigurationParser(warnings);
		 Configuration config = null;
		try {
			config = cp.parseConfiguration(configFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XMLParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = null;
		try {
			myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		} catch (InvalidConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			myBatisGenerator.generate(null);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

log4j2.xml配置如下

  
  
    
      
        
      
    
    
      
        
      
    
  

db.properties配置如下

jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.connect=jdbc:sqlserver://ip:1433;databaseName=数据库
jdbc.username=xx
jdbc.password=xx

运行代码即可

你可能感兴趣的:(Mybatis)