大飞_dafei

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

mybatis-generator 自动生成dao、sql.xml、实体类

mybatis-generator 自动生成dao、sql.xml、实体类

01)pom引入

复制代码
<build>
    <plugins>
        <!--  自动生成了dao、sql、xml、实体类 -->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.7</version>
            <configuration>
                <overwrite>true</overwrite>
            </configuration>
        </plugin>

    </plugins>
</build>
复制代码

02)配置 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>
    <!--  构建命令:mvn mybatis-generator:generate  -->
    <!--    windows下路径和Mac路径不一样,需要自己替换-->
    <classPathEntry
            location="C:\Users\fei\.m2\repository\mysql\mysql-connector-java\8.0.27\mysql-connector-java-8.0.27.jar"/>

    <context id="MysqlTables" targetRuntime="MyBatis3">

        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 注释 -->
        <commentGenerator >
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
        </commentGenerator>

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/temp_demo"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="com.example.fei.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成map.xml文件 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成map xml对应client,也就是接口dao -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.fei.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表 -->
        <table tableName="yz_book" domainObjectName="Book" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false">
            <!-- 数据库表主键 -->
            <generatedKey column="id" sqlStatement="Mysql" identity="true" />
        </table>

        <table tableName="yz_foo" domainObjectName="Foo" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false">
            <!-- 数据库表主键 -->
            <generatedKey column="id" sqlStatement="Mysql" identity="true" />
        </table>

        <!-- 配好表后在pom.xml目录下执行mvn mybatis-generator:generate -->

    </context>
</generatorConfiguration>
复制代码

03)控制台执行构建命令

#在pom.xml目录下执行mvn mybatis-generator:generate
mvn mybatis-generator:generate

 

 

mybatis-generator官网

 

posted on   大飞_dafei  阅读(264)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2020-05-02 vue 插槽slot _fei
2020-05-02 vue 父子组件之间访问 this.$refs 和 this.$parent _fei
2020-05-02 vue组件之间传值(02)__子组件传值父组件 emit _fei
点击右上角即可分享
微信分享提示