Mybatis通过generatorConfig.xml自动生成代码

开发工具intellij IDEA ,maven构建的项目

一.项目外部生成代码

一.新建代码生成的文件夹

需要mybatis-generator-core.jar 和sql jar,同时建好存储mapper.xml、mapper接口文件、pojo类的文件夹

Mybatis通过generatorConfig.xml自动生成代码_第1张图片

generatorConfig的配置文件


  

 
    
    
    
        
        
        
            
            
        
        
        
        
		  
        
            
        
        
        
            
            
        
        
        
            
    
        
        
            
        
        
		

 然后在generators目录下打开命令窗口执行‘

java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite

 

二在项目上mybatis自动生成代码

  编写generatorConfig.XML 同上

代码执行

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

 

遇到的问题:1.生成时如果generatorConfig.XML文件第一行前面有空格会报错。

                     2.报错 没有为集成身份验证配置驱动程序。

                                如果用的sqlserver URL当中用了integratedSecurity=true;;就不能用SQLServer的用户名和密码登录,去掉后                                 问题解决

 

你可能感兴趣的:(SSM)