逆向工程Mybatis Generator代碼生成

本文介绍MyBatis逆向工程的使用方法,通过配置工具自动生成Mapper接口、Mapper XML及POJO等代码,减轻程序员编写SQL的工作负担,提高开发效率。

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

逆向工程Mybatis Generator代碼生成

逆向工程简介

    mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo…),可以让程序员将更多的精力放在繁杂的业务逻辑上。

    企业实际开发中,常用的逆向工程方式:由数据库的表生成java代码。

    之所以强调单表两个字,是因为Mybatis逆向工程生成的Mapper所进行的操作都是针对单表的,也许你可能会觉得那这就有点鸡肋了,但是在大型项目中,很少有复杂的多表关联查询,所以作用还是很大的。

逆向工程的使用

1.首先我们创建一个springboot项目,如何搭建springboot项目可参考:https://blog.csdn.net/blueAndSmile/article/details/109593345


2.pom.xml文件添加依赖
<build>
        <finalName>zsxt</finalName>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

3.项目结构:src---->main—>resources目录下创建generatorConfig.xml文件,如下图所示:
在这里插入图片描述

4.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 >
    <!-- mysql jar 文件位置 -->
    <classPathEntry location="D:\mybatis逆向生成\mysql-connector-java-5.1.46\mysql-connector-java-5.1.46/mysql-connector-java-5.1.46.jar" />
    <context id="store" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
            <!-- 是否去除所有自动生成的文件的时间戳,默认为false -->
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="数据库连接地址"
                        userId="数据库用户名"
                        password="数据库密码">
            <!--防止生成实体出现多余字符-->
            <property name="nullCatalogMeansCurrent" value="false" />
        </jdbcConnection>
        <!-- targetPackage:包名称(自定义)  targetProject:项目路径(自定义)   -->
        <!--定义model的包名称-->
        <javaModelGenerator targetPackage="com.ssmblog.entity" targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格  -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 配置生成相应的实体Mapper.xml,对于Mapper3.X我们需要把type="XMLMAPPER" -->
        <!-- targetPackage:包名称(自定义)  targetProject:项目路径(自定义)   -->
        <sqlMapGenerator targetPackage="com.ssmblog.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 配置生成相应的接口类,对应与Mapper.xml中的一系列CRUD方法SQL语句 -->
        <!-- targetPackage:包名称(自定义)  targetProject:项目路径(自定义)   -->
        <javaClientGenerator targetPackage="com.ssmblog.dao" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 需要生成的表 -->
        <table schema="" tableName="sys_user" domainObjectName="sysUser"
               delimitIdentifiers="true"
               delimitAllColumns="true"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table schema="" tableName="sys_user_post" domainObjectName="sysUserPost"
               delimitIdentifiers="true"
               delimitAllColumns="true"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table schema="" tableName="sys_user_role" domainObjectName="sysUserRole"
               delimitIdentifiers="true"
               delimitAllColumns="true"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

5.增加配置参数
Edit configurations---------->左上角加号-------->maven------->在command line添加配置参数:mybatis-generator:generate -e ----------------->点击ok
在这里插入图片描述
在这里插入图片描述

6.运行程序
可看到左边生成对应的实体,mapper,xml文件。右边添加需要生成对应的表在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大风吱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值