Mybatis逆向工程 MyBatis Generator 收集

MyBatis Generator (MBG) 是一个为 MyBatis 框架设计的代码生成器,能够自动生成 mapper.xml、接口及实体类。支持基本的 CRUD 操作和 QBC 查询,但复杂的 SQL 需手动编写。MBG 可通过命令行、Ant、Maven、Java 程序以及 Eclipse 插件等方式运行。配置文件通常为 XML,也可使用 Java 配置。代码示例展示了如何从 Java 程序中调用 MBG,并提供了一个基于 XML 配置文件的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

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);
		}

}

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

在这里插入图片描述

33

出现问题

import org.mybatis.spring.annotation.MapperScan;
import tk.mybatis.spring.annotation.MapperScan;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值