Mybatis逆向工程

博客介绍了MyBatis逆向工程,它可针对单表自动生成执行所需代码。企业实际开发中常用从数据库表生成Java代码的方式,且开发前数据库已设计好。还进行了逆向工程演示,包括新建Maven工程、配置pom.xml和generatorConfig.xml等步骤。

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

什么是逆向工程

代码都放出来了

Mybatis需要程序员自己编写SQL语句,mybatis官网提供逆向工程,可以针对单表自动生成mybatis执行所有需要代码(mapper.java,mapper.xml,po)

企业实际开发中,常用的逆向工程方式:
数据库表来生产Java代码,
企业开发中,是先对数据库进行设计,也就是编码之前数据库已经有了。

逆向工程演示
1新建一个maven工程
2配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sgl</groupId>
    <artifactId>Generator</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

3配置resource文件加下的generatorConfig.xml
1)配置mysql 驱动jar包路径.用了绝对路径

路径在你的本地仓库 的jar包
mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar
知道本地仓库就能找到 版本可以不用选5.1.38

在这里插入图片描述
2)数据库连接
在这里插入图片描述
3)数据表对应的model层
4)sql mapper 映射配置文件
5)mybatis3中的mapper接口
6)数据表进行生成操作 schema:相当于库名; tableName:表名;
domainObjectName:对应的DO

<?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>
    <!-- (1)配置mysql 驱动jar包路径.用了绝对路径 -->
    <classPathEntry location=
     "G:\0-maven\repository-BD\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar" />

    <context id="scm_mysql_tables" targetRuntime="MyBatis3">
        <!-- 防止生成的代码中有很多注释,加入下面的配置控制 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>

        <!-- (2)数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/mybatis01?characterEncoding=UTF-8"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- (3)数据表对应的model层  -->
        <javaModelGenerator targetPackage="com.hd.po"
                            targetProject="src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- (4)sql mapper 映射配置文件 -->
        <sqlMapGenerator targetPackage="com.hd.mapper"
                         targetProject="src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- (5)mybatis3中的mapper接口 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.hd.mapper"
                             targetProject="src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- (6)数据表进行生成操作 schema:相当于库名; tableName:表名;
            domainObjectName:对应的DO
        -->
        <table schema="mybatis" tableName="user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table schema="mybatis" tableName="orders" domainObjectName="Orders"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

4使用maven插件

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值