idea集成 MyBatis Generator 插件,自动生成dao,model,sql map文件

本文介绍了如何在IntelliJ IDEA中集成MyBatis Generator插件,通过在pom.xml添加插件配置,并创建generatorConfig.xml配置文件,将插件设置在resource根目录下。完成集成后,可在IDEA的plugins中运行插件,自动生成DAO、Model和SQL Map文件。

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

1.集成到开发环境中
本文以maven管理的功能来举例,只需要将插件添加到pom.xml文件中即可。(注意此处是以plugin的方式,放在 中间即可)

org.mybatis.generator mybatis-generator-maven-plugin 1.3.2

2.编写配置文件 generatorConfig.xml
注意:在idea开发环境下,此文件需要放在resource根目录下,mybatis generator默认加载此目录的配置文件

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<classPathEntry
        location="C:\Program Files (x86)\MySQL\Connector.J 5.1\mysql-connector-java-5.1.40-bin.jar"/>
<!--数据库驱动oracle jar -->
<!--
<classPathEntry
        location="E:\app\Acer\product\11.2.0\client_2\jdbc\lib\ojdbc5.jar"/>-->
<context id="Tables" targetRuntime="MyBatis3">
    <!--去除注释 -->

    <commentGenerator>
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>

    <!--mysql数据库连接 -->

    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                    connectionURL="jdbc:mysql://localhost:3306/goods" userId="root"
                    password="root">
    </jdbcConnection>
    <!--oracle数据库连接 -->
    <!--
   <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
                   connectionURL="jdbc:oracle:thin:@172.16.36.23:1521/FOKF"
                   userId="YCGCE" password="YCGCE">
       <property name="remarksReporting" value="true"></property>
   </jdbcConnection>-->

    <!--默认false Java type resolver will always use java.math.BigDecimal if
        the database column is of type DECIMAL or NUMERIC. -->
    <javaTypeResolver>
        <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>

    <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) -->
    <javaModelGenerator targetPackage="com.model"
                        targetProject="E:\work7\goods\src\main\java">
        <property name="enableSubPackages" value="true"/>
        <property name="trimStrings" value="true"/>
    </javaModelGenerator>
    <!--生成SQLMAP文件 -->
    <sqlMapGenerator targetPackage="mapper"
                     targetProject="E:\work7\goods\src\main\resources">
        <property name="enableSubPackages" value="false"/>
    </sqlMapGenerator>
    <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" -->
    <javaClientGenerator type="XMLMAPPER"
                         targetPackage="com.dao"
                         targetProject="E:\work7\goods\src\main\java">
        <property name="enableSubPackages" value="false"/>
    </javaClientGenerator>

    <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 -->
    <table tableName="t_user" domainObjectName="User"
           enableCountByExample="false" enableUpdateByExample="false"
           enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"/>
    <table tableName="t_admin" domainObjectName="Admin"
           enableCountByExample="false" enableUpdateByExample="false"
           enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"/>
    <table tableName="t_book" domainObjectName="Book"
           enableCountByExample="false" enableUpdateByExample="false"
           enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"/>
    <table tableName="t_cartitem" domainObjectName="CartItem"
           enableCountByExample="false" enableUpdateByExample="false"
           enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"/>
    <table tableName="t_category" domainObjectName="Category"
           enableCountByExample="false" enableUpdateByExample="false"
           enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"/>
    <table tableName="t_order" domainObjectName="Order"
           enableCountByExample="false" enableUpdateByExample="false"
           enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"/>
    <table tableName="t_orderitem" domainObjectName="OrderItem"
           enableCountByExample="false" enableUpdateByExample="false"
           enableDeleteByExample="false" enableSelectByExample="false"
           selectByExampleQueryId="false"/>
</context>

集成工作完成了,只需在plugins中找到mybatis-generator plugin即可,双击运行或右击 运行都可。如下如所示:
在这里插入图片描述
附上工程目录结构及自动生成的文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack King

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值