您当前的位置: 首页 >  mybatis

止步前行

暂无认证

  • 3浏览

    0关注

    247博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Mybatis逆向工程之Mybatis Generator的使用

止步前行 发布时间:2018-02-05 10:25:29 ,浏览量:3

一、引言

对于使用 Mybatis 的开发者来说,绑定 Xml 和 Dao 接口的对应关系,需要在 Mybatis 的全局配置文件中进行配置,每个 Sql 也需要自己来逐一编写。但 Mybatis 也提供了逆向工程来生成这些文件,可以根据 Database 来逆向的生成这些文件,下面来介绍一下。下面附上 Mybatis Generator 的官方文档,也可以参考着看一下。

http://www.mybatis.org/generator/configreference/xmlconfig.html

二、使用

(1)、maven工程下,加入jar的配置,不是maven工程的话,要下载相应的jar包 这里写图片描述

(2)、在项目目录下创建mybatisGenerator.xml配置文件,具体配置如下图: 这里写图片描述这里写图片描述这里写图片描述

(3)、编写mybatisGenerator.xml的测试代码: 这里写图片描述

运行上面的测试代码,就可以逆向生成 Mybatis 的实体类、Dao接口文件和mapper文件,但此方式生成的文件并不完整,可以根据功能的需要对文件进行改写。

三、详细代码 (1)、mybatisGenerator.xml代码






  
	
	
	 	
	




    
    

    
      	
    

	
    
      
      
    

	
    
      
    

	
    
      
    

	











    

  

(2)、测试代码
public class MybatisGeneratorTest {

	public static void main(String[] args) throws Exception {
		List warnings = new ArrayList();
		boolean overwrite = true;
		File configFile = new File("mybatisGenerator.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);
	}

}

(3)、目录结构图

在这里插入图片描述

关注
打赏
1657848381
查看更多评论
立即登录/注册

微信扫码登录

0.0431s