Mybatis逆向工程配置

本文介绍Mybatis逆向工程的配置,如有错误,请您纠正

基本概念

mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo…),可以让程序员将更多的精力放在繁杂的业务逻辑上。注意:只能对单表生成基本的增删改查操作,多表间的复杂方法不适用

项目创建以及jar包导入

创建项目

只需创建一个简单的JavaHelloWorld项目即可,命名为MybatisGenerator

导入jar包

只需要导入两个包
在这里插入图片描述

配置generatorConfig.xml

在src目录下创建generatorConfig.xml
文件头



配置信息



    
        
        
        

        
            
        

        "com.xxl.backoffice.model" targetProject=".\src">
            
            
        

        "com.xxl.backoffice.mapper"  targetProject=".\src">
            
        

        "com.xxl.backoffice.mapper"  targetProject=".\src">
            
        

        "user" domainObjectName="User" >
        

编写Main函数

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;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List warnings = new ArrayList();
        boolean overwrite = true;
        File configFile = new File("src/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);


    }
}

使用

运行Main函数,按照路径生成model和mapper,将java文件拷贝到项目中即可

你可能感兴趣的:(Mybatis逆向工程配置)