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();
}
}
注意:
生成后的方法updateByPrimaryKeySelective与updateByPrimaryKey区别:
updateByPrimaryKeySelective:只是更新新的model中不为空的字段
updateByPrimaryKey:将为空的字段在数据库中置为NULL
阅读(1493) | 评论(0) | 转发(1) |