MyBatis-使用mybatis-generator-core.jar生成POJO和Mapper文件

Demo:

http://pan.baidu.com/s/1pLeyVv9


1.pom.xml

 
    

    log4j
    log4j
    1.2.17

    

    ojdbc
    ojdbc
    14-10gR3



    mysql
    mysql-connector-java
    5.1.38

    
    org.mybatis
    mybatis
    3.3.0



    org.mybatis.generator
    mybatis-generator-core
    1.3.2

  
2.mybatis-generator.xml(根目录下新建资源文件夹resources,放里面就行,以后需要读取)

这是用于生成pojo和mapper的配置文件,是独立的,这里面定义了各种映射规则,如生成的位置,生成哪张表甚至可以指定字段等等,具体如下:





	
		
			
			
		
		
		
		
		

		
		
			
		

		
		
			
			
			
			
		
        
		
			
		
		
		
			
		
		
		
		
3.创建执行类GeneratorExecution,用来读取上述配置文件,并且根据配置文件执行(直接Run as Java Application)就大功告成了,哦对了,记得刷新一下项目,不然看不到哦^_^

public class GeneratorExecution {
	public static void generator(){
		
		List warnings=new ArrayList();
		try {
//		导入配置表mybatis-generator.xml
		File configFile=new File("resources/mybatis-generator.xml");
//		解析
		ConfigurationParser cp=new ConfigurationParser(warnings);
		Configuration config=cp.parseConfiguration(configFile);
//		是否覆盖
		DefaultShellCallback dsc=new DefaultShellCallback(true);
		MyBatisGenerator mg=new MyBatisGenerator(config, dsc, warnings);
		mg.generate(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GeneratorExecution.generator();
		System.out.println("done!");
	}

}







你可能感兴趣的:(MyBatis,MyBatis)