IDEA uses Mybatis Generator to automatically generate part of the code

IDEA uses Mybatis Generator to automatically generate part of the code

Description: Use Mybatis Generator code generation configuration to simply generate some back-end code, including (entity class, Mapper, Mapper.xml file)

1. Generate maven project
Insert image description here

2. Edit the pom.xml file and add the required dependencies.
Insert image description here
The configurationFile in the build does not need to be added. This path is the configuration file path of the production code.
3. Add the generatorConfig.xml configuration file.
Find the folder src/resources in the project and add the generatorConfig.xml configuration file. The following operation uses the Mac version as a template:

<?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>
    <!-- 数据库驱动:选择我们的本地硬盘上面的数据库驱动包 ,我这里放在C盘-->
    <classPathEntry  location="/Users/bmht/.m2/repository/mysql/mysql-connector-java/8.0.19/mysql-connector-java-8.0.19.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <!-- 防止生产代码注释过多,添加去除注释的配置 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库连接驱动类,URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/system"
                        userId="root"
                        password="zjb1988zjb">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成(实体)模型的包名和位置-->
        <javaModelGenerator targetPackage="com.zjb.cloud.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成XML映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO接口的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zjb.cloud.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="sys_user" domainObjectName="SysUser"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

The path of classPathEntry is the local computer path. In the window system, the location in classPathEntry can be replaced with the mysql driver address on the hard disk.
4. Generate code.
Insert image description here
Find Maven on the right side of IDEA, find mabatis-genertaor:generator in the project, right-click Run Maven Build to generate code.
Insert image description here

Reference document:
https://blog.csdn.net/qq_36356379/article/details/103476325

Guess you like

Origin blog.csdn.net/weixin_38863607/article/details/122796309