基于maven实现逆向工程
1.新建maven项目,选择:
注意:可能生出项目在index.jsp里会报错,这是因为项目没有导入Tomcat服务器,
2.在pom.xml里引入逆向工程依赖的jar包
org.mybatis.generator
mybatis-generator-core
1.3.5
org.mybatis
mybatis
3.4.2
mysql
mysql-connector-java
5.1.41
3.建包:
4.新建bgm.xml:
内容如何编写可以参考
1:http://www.mybatis.org/generator/configreference/xmlconfig.html
'''
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/crud?characterEncoding=utf-8" userId="root" password="q6687015">
targetPackage="com.qjp.crud.bean" targetProject=".\src\main\java">
targetPackage="com.qjp.crud.mapper" targetProject=".\src\main\java">
targetPackage="com.qjp.crud.mapper" targetProject=".\src\main\java">
'''
这里主要是编写pojo,mapper映射文件,dao的接口对应生成的位置,以及数据库表的生成策略。
5.基于java程序驱动运行MyBatis Generator:
新建一个java程序编写驱动程序
'''
package com.qjp.crud.test;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
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.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class Mybatisgenerator {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
// TODO Auto-generated method stub
List
boolean overwrite = true;
File configFile = new File("bgm.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);
}
}
'''
编写参考:http://www.mybatis.org/generator/running/running.html