• Mybatis逆向工程 MyBatis Generator 收集


    Mybatis逆向工程 MyBatis Generator 收集

    https://github.com/mybatis/generator/releases

    http://www.mybatis.org/generator/configreference/xmlconfig.html

    Running MyBatis Generator With Java

    http://mybatis.org/generator/running/runningWithJava.html

    Mybatis逆向工程是什么?
    MyBatis Generator简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件(mapper.xml),接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写。
    官方文档:http://mybatis.org/generator/

    MyBatis 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

    11

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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.

    22

    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);
    		}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    33

    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("================代码生成成功================");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    1

    在这里插入图片描述

    33

    出现问题

    import org.mybatis.spring.annotation.MapperScan;
    import tk.mybatis.spring.annotation.MapperScan;
    
    • 1
    • 2

    这两个到时候 怎么选择呢?

  • 相关阅读:
    Java for循环语句
    读书笔记之C Primer Plus 2
    自动化您的Instagram帐户的程序InstaBot Pro 7.0.2
    Stream方法的介绍
    Web APIs Web APIs第六天
    51单片机入门——数模\模数转换
    idea 快捷键 自己也可以修改
    隔离这几天开发了一个带控制台的OAuth2授权服务器分享给大家
    RegNet架构复现--CVPR2020
    学习记忆——宫殿篇——记忆宫殿——数字编码——记忆数字知识点
  • 原文地址:https://blog.csdn.net/wowocpp/article/details/125886347