IDEA使用 mybatis generator 自动生成mapper文件和model文件

  1. 在pom.xml 文件中添加mybatis-generator-maven-plugin

    <!-- mybatis generator 自动生成代码插件 -->
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.5</version>
                    <configuration>
                        <configurationFile>src/main/resources/myBatisGeneratorConfig.xml</configurationFile>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.mybatis.generator</groupId>
                            <artifactId>mybatis-generator-core</artifactId>
                            <version>1.3.5</version>
                        </dependency>
                    </dependencies>
                </plugin>
    
  2. 点击图片中红色圈出的按钮,将此mybatis-generator-maven-plugin装载进此项目

    8YPDPg.png

  3. 在resources文件下,添加myBatisGeneratorConfig.xml文件

    <?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>
    
        <!-- 本地数据库驱动程序jar包的全路径 -->
        <classPathEntry location="D:\WenJian\Maven\maven_rep\mysql\mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar"/>
    
        <context id="context" targetRuntime="MyBatis3">
    
            <!--定义生成的java类的编码格式-->
            <property name="javaFileEncoding" value="UTF-8" />
    
            <!-- 数据库的相关配置 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/whqbs" userId="root" password="qwertyuiop"/>
    
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
               NUMERIC 类型解析为java.math.BigDecimal -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
    
            <!-- 实体类生成的位置 -->
            <javaModelGenerator targetPackage="com.tit.springbootdemo01.model" targetProject="src/main/java">
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
    
            <!-- *Mapper.xml 文件的位置 -->
            <sqlMapGenerator targetPackage="com.tit.springbootdemo01.mapper" targetProject="src/main/java">
            </sqlMapGenerator>
    
            <!-- Mapper 接口文件的位置 -->
            <javaClientGenerator targetPackage="com.tit.springbootdemo01.mapper" targetProject="src/main/java" type="XMLMAPPER">
            </javaClientGenerator>
    
            <!-- 相关表的配置   tableName 表名 -->
            <table tableName="userinfo" enableSelectByExample="true"
                   enableDeleteByExample="true" enableCountByExample="true"
                   enableUpdateByExample="true" selectByExampleQueryId="true">
                <property name="ignoreQualifiersAtRuntime" value="false" />
                <property name="useActualColumnNames" value="false" />
            </table>
            <table tableName="orgdepartment" enableSelectByExample="true"
                   enableDeleteByExample="true" enableCountByExample="true"
                   enableUpdateByExample="true" selectByExampleQueryId="true">
                <property name="ignoreQualifiersAtRuntime" value="false"/>
                <property name="useActualColumnNames" value="false"/>
            </table>
            <table tableName="user" enableSelectByExample="true"
                   enableDeleteByExample="true" enableCountByExample="true"
                   enableUpdateByExample="true" selectByExampleQueryId="true">
                <property name="ignoreQualifiersAtRuntime" value="false"/>
                <property name="useActualColumnNames" value="false"/>
            </table>
            <table tableName="logreg" enableSelectByExample="true"
                   enableDeleteByExample="true" enableCountByExample="true"
                   enableUpdateByExample="true" selectByExampleQueryId="true">
                <property name="ignoreQualifiersAtRuntime" value="false"/>
                <property name="useActualColumnNames" value="false"/>
            </table>
        </context>
    </generatorConfiguration>
    
  4. 此时配置已完成,点击红色圈出的地方,即可自动生成,切记要打开数据库
    8YiAdf.png

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐