IDEA自动生成实体类XML

本文介绍了如何通过IDEA和Mybatis Generator插件自动化生成Entity、Mapper文件。首先,在pom.xml中添加插件依赖和配置,接着配置generatorConfig.xml,指定数据库连接信息和生成目标。然后,通过Maven Plugins执行生成任务。最后,展示生成结果。通过这个过程,可以快速便捷地完成代码的初始化工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过IDEA可以自动生成entity,mapper,一共分为三步骤
一:pom文件导入所需插件依赖
二:配置generatorConfig.xml
三:通过maven-plugins执行生成
1.导入依赖

 <build>
        <plugins>
            <plugin>
                <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.41</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--配置generator自动生成的路径文件的位置-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <!--允许移动生成文件-->
                    <verbose>true</verbose>
                    <!--允许自动覆盖文件,正式开发中不允许自动覆盖-->
                    <overwrite>false</overwrite>
                </configuration>
            </plugin>
            <!--<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>-->
        </plugins>
    </build>

2.配置generatorConfig.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>
    <context id="Mysql" targetRuntime="MyBatis3">

        <commentGenerator>
            <!-- 去掉自动生成的注释 -->
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.200:3306/tjkcom"  userId="root" password="password"  />
        <!--生成DataObject类的存放地址-->
        <javaModelGenerator targetPackage="com.tjk.tjkcom.basic.entity" targetProject="src/main/java" >
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="mapping.basic" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成dao类存放位置-->
        <!--客户端代码,生成易于使用针对medel对象和xml配置文件的代码
                  type = "Annotatedmapper",生成java model 和基于注解的mapper对象
                  type = "Mixedmapper",生成基于注解的java model 和响应的mapper对象
                  type = "xmlmapper',生成sqlmapper xml文件和独立的mapper接口-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.tjk.tjkcom.basic.dao" targetProject="src/main/java"  >
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <table tableName="tb_mcominformation"  domainObjectName = "ComInformation" enableCountByExample = "false" enableUpdateByExample = "false" enableDeleteByExample = "false" enableSelectByExample = "false" selectByExampleQueryId = "false">
        </table>
        <!--<table tableName="tb_mbasicreferdtl"  domainObjectName = "MBasicReferDtl" enableCountByExample = "false" enableUpdateByExample = "false" enableDeleteByExample = "false" enableSelectByExample = "false" selectByExampleQueryId = "false">
        </table>-->
    </context>
</generatorConfiguration>

3.执行生成有俩种方式
一种是通过maven下的plugins去双击执行
在这里插入图片描述

另外一种就是通过命令
mybatis-generator:generate -e
在这里插入图片描述

4.结果如下
在这里插入图片描述

最后自动生成就完事了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值