作者:窝大华_221 | 来源:互联网 | 2021-09-27 08:20
这篇文章主要介绍了Mybatisgenerator如何自动生成代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
开发项目的时候,表很多,是不可能一点点的自己去写xml ,dao文件的,这里就需要用到代码的自动生成工具了。
第一步:导入jar包,当然,这之前,基本环境,像mybatis,数据库之类的都得搭建好。
org.mybatis.generator
mybatis-generator-core
1.3.2
第二步:创建启动的 xml配置文件mybatis-generator-config.xml
<&#63;xml version="1.0" encoding="UTF-8"&#63;>
第三步,写个主方法,去运行就行了
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
/**
* @author szy
* @version 创建时间:2018-6-4 下午9:26:54
*
*/
public class MybatisGenerator {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
MybatisGenerator generatorSqlmap = new MybatisGenerator();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
public void generator() throws Exception{
List warnings = new ArrayList();
boolean overwrite = true;
//指定 逆向工程配置文件
File cOnfigFile= new File("E:/java-workspace/myblog/src/main/resources/mybatis-generator-config.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);
}
}
完成,刷新下就能看到代码都出来了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。