SpringBoot中使用Mybatis Generator进行数据库表逆向工程自动生成实体类和mapping映射

1.首先在项目中创建一个GeneratorDisplay工具类,放在com.springbootdemo.util

package com.springbootdemo.util;
import java.io.File;
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 GeneratorDisplay {
    public void generator() throws Exception {
        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);
    }
    public static void main(String[] args) throws Exception {
        try {
            GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2.在项目中创建一个MyMapper顶级接口,将来所有自动生成的Mapper接口都会去继承该接口,放在com.springbootdemo.util下,需要特别强调的是,这个接口千万不要被Spring扫描到

package com.springbootdemo.util;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper extends Mapper, MySqlMapper {
    //特别注意,该接口不能被扫描到,否则会出错
}

3.需要说明的是,逆向工程的关键就是generatorConfig.xml这个配置文件,放在项目的根目录下(与src平级)



<generatorConfiguration>
    <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.springbootdemo.util.MyMapper"/>
        plugin>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/springbootdemo"
                        userId="admin"
                        password="admin">
        jdbcConnection>
        
        <javaModelGenerator targetPackage="com.springbootdemo.pojo" targetProject="src/main/java"/>
	
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
	
        <javaClientGenerator targetPackage="com.springbootdemo.mapper" targetProject="src/main/java"
 type="XMLMAPPER"/>
 	
	<table tableName="user">table>
    context>
generatorConfiguration>

这里需要改动的几个地方是:
标签下的应指向MyMapper接口;
标签需要配置数据库连接参数;
标签下指向自动生成的pojo类所在的包;
标签下指向自动生成的mapper.xml所在的静态资源文件夹路径;
标签下指向自动生成的mapper接口,该接口将自动继承MyMapper接口。
4.以上全部配置完成后,运行GeneratorDisplay工具类里的main方法即可,逆向工具会自动帮我们生成pojo类、对应mapper.xml配置文件和mapper接口
PS:感兴趣的同学,可以去我的GitHub下载相关源码阅读https://github.com/JochenGan

你可能感兴趣的:(Java技术)