Mybatis逆向工程开发指南

2018/11/12 9:46:47


添加依赖

    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.7</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.13</version>
        <scope>runtime</scope>
    </dependency>

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!-- 指定驱动包位置 注意事项:context中的标签存在顺序关系-->
    <classPathEntry location="E:\respository\mysql\mysql-connector-java\8.0.12\mysql-connector-java-8.0.12.jar" />

    <!-- 指定数据库连接参数  context各个属性存在排序关系-->
    <context id="DB2Tables" targetRuntime="MyBatis3"><!-- MyBatis3 MyBatis3Simple 不生成example -->

        <!-- 序列化接口的插件 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <!-- 重写toString方法的插件 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        <!--<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>-->

        <commentGenerator>
            <!-- 是否去除自动生成的注释(生成的注释是英文的) true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 配置数据库连接 mysql8.0的坑 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1/OVLS?useSSL=false&amp;serverTimezone=Asia/Shanghai&amp;allowPublicKeyRetrieval=true"
                        userId="root"
                        password="1258">

        </jdbcConnection>

        <!-- 这个元素的配置用来指定JDBC类型和Java类型如何转换 -->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 指定实体类存放位置 项目路径 -->
        <javaModelGenerator targetPackage="包名" targetProject="*\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 指定SQL定义的XML文件存放位置 -->
        <sqlMapGenerator targetPackage="所在目录"  targetProject="*\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 指定Mapper映射器存放位置 -->
        <!-- type:该属性用于选择一个预定义的客户端代码(可以理解为Mapper接口)生成器,用户可以自定义实现,需要继承org.mybatis.generator.codegen.AbstractJavaClientGenerator类,必选有一个默认的构造方法。 该属性提供了以下预定的代码生成器,首先根据<context>的targetRuntime分成三类:
            MyBatis3:
                ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件
                MIXEDMAPPER:XML和注解的混合形式,(上面这种情况中的)SqlProvider注解方法会被XML替代。
                XMLMAPPER:所有的方法都在XML中,接口调用依赖XML文件。
            MyBatis3Simple:
                ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件
                XMLMAPPER:所有的方法都在XML中,接口调用依赖XML文件。 -->
        <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="包名"  targetProject="*\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 指定数据库哪些表创建实体类、SQL文件、Mapper映射器 -->
       <!-- <table  tableName="%"
                enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false">
        </table>-->
        <table tableName="answer"  enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"/>
    </context>
</generatorConfiguration>

type:该属性用于选择一个预定义的客户端代码(可以理解为Mapper接口)生成器,用户可以自定义实现,需要继承org.mybatis.generator.codegen.AbstractJavaClientGenerator类,必选有一个默认的构造方法。 该属性提供了以下预定的代码生成器,首先根据的targetRuntime分成三类:
MyBatis3:

ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件
MIXEDMAPPER:XML和注解的混合形式,(上面这种情况中的)SqlProvider注解方法会被XML替代。
XMLMAPPER:所有的方法都在XML中,接口调用依赖XML文件。

MyBatis3Simple:

ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件
XMLMAPPER:所有的方法都在XML中,接口调用依赖XML文件。

运行启动方法类

public class RunMyBatisGenerator {
    public static void main(String[] args) throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = GetResources.getResources("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);
    }
}

IDEA获取resources目录下的文件

IDEA下使用File file = new File(“generatorConfig.xml”);的方式无法读取到resources目录下的文件因此编写一个通用工具类用于读取

public class GetResources {
    public static final File getResources(String fileName){
        return new File(GetResources.class.getClassLoader().getResource(fileName).getPath());
    }
}

猜你喜欢

转载自blog.csdn.net/YM_IlY/article/details/86685361