Mybatis逆向工程 MyBatis Generator 收集
https://github.com/mybatis/generator/releases
http://www.mybatis.org/generator/configreference/xmlconfig.html
http://mybatis.org/generator/running/runningWithJava.html
Mybatis逆向工程是什么?
MyBatis Generator简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件(mapper.xml),接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写。
官方文档:http://mybatis.org/generator/
http://mybatis.org/generator/running/running.html
MyBatis Generator (MBG) can be run in the following ways:
From the command prompt with an XML configuration
As an Ant task with an XML configuration
As a Maven Plugin
From another Java program with an XML configuration
From another Java program with a Java based configuration
As an Eclipse Feature
Running MyBatis Generator With Java
MyBatis Generator (MBG) may be invoked directly from Java. For configuration, you may use either an XML configuration file, or configure MBG completely with Java.
Running MBG from Java with an XML Configuration File
The following code sample shows how to call MBG from Java with an XML based configuration. It does not show exception handling, but that should be obvious from the compiler errors 😃
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.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);
Notes:
Configuration file properties may be passed to the parser as a parameter on the ConfigurationParser constructor. If not passed explicitly, the JVM system properties will be searched for the value of configuration file properties. For example, the property generated.source.dir can be accessed in the configuration file with the escape sequence ${generated.source.dir}
If a property is specified in the configuration file and is not resolved, then the escaped property string will be passed “as is” into the generated code.
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;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class CodeH5Generator {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<>();
// 如果已经存在生成过的文件是否进行覆盖
boolean overwrite = true;
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(CodeH5Generator.class.getResourceAsStream("/generator/generatorH5Config.xml"));
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator generator = new MyBatisGenerator(config, callback, warnings);
generator.generate(null);
}
}
import java.io.InputStream;
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;
public class CodeGenerator {
public static void main(String[] args) {
try {
new CodeGenerator().generator();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void generator() throws Exception {
System.out.println("================华丽分割线 ================");
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("generator/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
System.out.println(warnings);
System.out.println("================代码生成成功================");
}
}
出现问题
import org.mybatis.spring.annotation.MapperScan;
import tk.mybatis.spring.annotation.MapperScan;
这两个到时候 怎么选择呢?