利用mybatis-generator-core的jar包自动生

2016-10-19  本文已影响0人  暮雨随风

1、用Idea创建一个Maven项目,在Maven项目中添加依赖和插件。然后点击Maven Projects--install,完成对依赖的包的导入。此项目需要mybatis-generator-core.jar、mysql-connector-java.jar包等,可以从Maven中央仓库下载。由于不能上传代码,所以将代码提交到了github:

https://github.com/juxiemushu/Mybatis_generator_core.git

2、在resources资源目录下创建一个Config文件夹,用于存储初始化的数据库信息,以及代码生成需要的 xml 文件。

其中 init.properties 的代码如下:

javapath:指定了生成的

#Mybatis Generator configuration

javapath=src/main/java

xmlpath=src/main/resources

classPath=G:/mysql-connector-java-5.1.39.jar

jdbc_driver=com.mysql.jdbc.Driver

jdbc_url=jdbc:mysql://127.0.0.1:3306/db_exam?characterEncoding=utf8

jdbc_user=root

jdbc_password=wsp19940812

代码生成配置文件 mbgConfiguration.xml ,在配置文件中可以配置数据库的连接,代码生成的包名,与数据库对应的表等信息。

3、创建一个Java类,用于读取配置文件,生成代码,内容如下:

public classGenerator {

public static voidmain(String[] args){

List warnings =newArrayList();

booleanoverwrite =true;

String path ="/Config/mbgConfiguration.xml";

File configFile =newFile(Generator.class.getResource(path).getFile());

ConfigurationParser cp =newConfigurationParser(warnings);

Configuration config =null;

try{

config = cp.parseConfiguration(configFile);

}catch(IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}catch(XMLParserException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

DefaultShellCallback callback =newDefaultShellCallback(overwrite);

MyBatisGenerator mybatisGenerator =null;

try{

mybatisGenerator =newMyBatisGenerator(config,callback,warnings);

}catch(InvalidConfigurationException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

try{

mybatisGenerator.generate(null);

}catch(SQLException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}catch(InterruptedException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

4、运行步骤3中的Java类,如股票运行正常,则在相应的包中会出现自动生成的代码。

上一篇 下一篇

猜你喜欢

热点阅读