Chinaunix首页 | 论坛 | 博客
  • 博客访问: 126582
  • 博文数量: 47
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 243
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-18 11:45
文章分类

全部博文(47)

文章存档

2017年(2)

2015年(33)

2014年(12)

分类: Java

2014-12-03 17:22:08

1.下载MyBatis的Generator工具mybatis-generator-core-1.3.2.jar包
2.配置自动生成代码所需的xml配置文件

  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "">


 
 

 
 
   
 
       
       
 


   
   
   


   
   
       
       
   

    
   
   
       
     
       
     
   

    
   
   
     
   

    
   
   
     
   

    
   
            enableCountByExample="false" enableUpdateByExample="false"
        enableDeleteByExample="false" enableSelectByExample="false"
        selectByExampleQueryId="false">
   
   


 


3.执行main方法进行生成
public static void main(String[] args) {
        List warnings = new ArrayList();
        boolean overwrite = true;
        String genCfg = "/mybatisGen.xml";
        File configFile = new File(GenMain.class.getResource(genCfg).getFile());
        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 (Exception e) {
            e.printStackTrace();
        }
    }
注意:
    生成后的方法updateByPrimaryKeySelectiveupdateByPrimaryKey区别
    updateByPrimaryKeySelective:只是更新新的model中不为空的字段
    updateByPrimaryKey:将为空的字段在数据库中置为NULL
阅读(1460) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~