mybatis学习笔记(10): mybatis逆向工程

什么是逆向工程

MyBatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话,难免会很麻烦,所以mybatis官方提供了一个逆向工程,可以针对单表自动生成mybatis执行所需要的代码(包括mapper.xml、mapper.java、po..)。一般在开发中,常用的逆向工程方式是通过数据库的表生成代码。


使用逆向工程

使用MyBatis的逆向工程,需要导入逆向工程的jar包,我用的是mybatis-generator-core-1.3.2.jar,还需要导入数据库驱动包。下面开始总结一下MyBatis逆向工程的使用步骤。


新建一个工程

我们要新建一个java工程,这个工程专门用来使用逆向工程生成代码的。
工程目录如下:
mybatis学习笔记(10): mybatis逆向工程_第1张图片

1. 配置逆向工程的配置文件
generatorConfig.xml:



<generatorConfiguration>
    <context id="test" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin">plugin>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin">plugin>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin">plugin>
        <commentGenerator>
            
            
            <property name="suppressDate" value="true" />
            
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
            password="root">
        jdbcConnection>
        <javaTypeResolver>
            
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.mybatis.po"
            targetProject=".\src">
            
            <property name="enableSubPackages" value="true" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="com.mybatis.mapper"
            targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.mybatis.mapper" targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>

        
        <table schema="" tableName="tbl_employee" domainObjectName="Employee">table>

        
    context>
generatorConfiguration>

从上面的配置文件中可以看出,配置文件主要做的几件事是:

  • 连接数据库,这是必须的,要不然怎么根据数据库的表生成代码呢?
  • 指定要生成代码的位置,要生成的代码包括po类,mapper.xml和mapper.java
  • 指定数据库中想要生成哪些表

执行逆向工程生成代码

配置文件都搞定了,然后就执行以下程序即可生成代码了,生成的java程序,下载的逆向工程文档中都有示例,如下:

public class GeneratorSqlmap {

    public void generator() throws Exception{

        List warnings = new ArrayList();
        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 {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

执行完后刷新就可以看到生成的代码文件了:
mybatis学习笔记(10): mybatis逆向工程_第2张图片


jar包和相关参考文件可以取官网找:
https://github.com/mybatis/generator

完整代码:
https://download.csdn.net/download/abc997995674/10518823

你可能感兴趣的:(mybatis)