强大的MyBatis的逆向工程减少代码量(由表建类)

本文介绍如何使用逆向工程自动生成POJO、Mapper接口及映射文件,通过配置Maven项目、添加依赖与插件,以及修改generatorConfig.xml配置文件,实现数据库表到代码的快速转换。

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

把表——>逆向生成pojo,mapper接口,mapper映射文件

为什么要使用逆向工程?在开发过程中,我们需要针对表创建相应的pojo,mapper,mapper等相关的文件,而在实际现实生活中,对应的表的数量很大,为了提高工作效率,把重点放在业务等其他重要的任务中,于是逆向工程的使用,方便了不少!

逆向工程的核心

1.创建maven的project

2.添加逆向工程的依赖和逆向工程的插件(在pom.xml中)

 <dependencies>
        <!--逆向工程依赖-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
 </dependencies>


<!-- 逆向工程的插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.45</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <!--配置文件的路径  generatorConfig.xml可以去网上下载 -->
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>

generatorConfig.xml可以去网上下载 http://mybatis.org/generator/configreference/xmlconfig.html

3.在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="test" targetRuntime="MyBatis3">
        <!-- 一些工具 是否需要实现可序列化接口,是否需要实现tostring-->
       <!-- <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <!--<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>-->
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 我一般都去除,要不然生成的字段一大段注释-->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库链接 参数 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/需要添加的数据库名" 
                        userId="数据库用户名"
                        password="数据库密码">
        </jdbcConnection>
        <javaTypeResolver>
            <!-- This property is used to specify whether MyBatis Generator should
                force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
			NUMERIC 类型解析为java.math.BigDecimal -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置
            实体类的位置
        -->
        <javaModelGenerator targetPackage="com.itqf.pojo"
                            targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO(接口如mapper类)的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.itqf.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 要生成哪些表 -->
        <table tableName="dept" domainObjectName="Dept"></table>
        <table tableName="emp" domainObjectName="Emp"></table>
        <table tableName="salesman" domainObjectName="Sales"></table>
        <table tableName="user" domainObjectName="User"></table>
        <table tableName="items" domainObjectName="Items"></table>
    </context>
</generatorConfiguration>

4.生成相应的类

快速生成了我们需要的类及其对应的内部内容 

    

生成的XXXExample,Example提供了数据的有关操作

分页插件的使用

1.导入依赖

在pom.xml依赖依赖中

        <!--1.mybatis分页插件依赖-->
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper   -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.6</version>
        </dependency>

2.配置插件


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值