mybatis-使用自动生成(根据数据库反向生成pojo、映射文件,映射接口)

1.在pom.xml中导入依赖和插件

<dependencies>
<!--    导入自动生成依赖-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.0</version>
        </dependency>
    </dependencies>
    <build>
<!--     导入自动生成插件-->
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <!-- 在控制台打印执行日志 -->
                    <verbose>true</verbose>
                    <!-- 重复生成时会覆盖之前的文件-->
                    <overwrite>true</overwrite>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.47</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

 2.导入generatorConfig.xml配置文件

在pom.xml中设置的src/main/resources/的位置下导入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:起个名字 -->
    <!-- targetRuntime:设置生成的文件适用于那个 mybatis 版本 -->
    <context id="default" targetRuntime="MyBatis3">
       <!-- optional,指在创建class时,对注释进行控制-->
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的数据库连接 smbms 为数据库名字-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/zmall?useUnicode=true&amp;characeterEncoding=utf-8&amp;serverTimezone=UTC"
                        userId="root"
                        password="123"></jdbcConnection>
        <!--非必须,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <!-- 默认情况下数据库中的 decimal,bigInt 在 Java 对应是 sql 下的 BigDecimal 类 -->
            <!-- 不是 double 和 long 类型 -->
            <!-- 使用常用的基本类型代替 sql 包下的引用类型 -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- targetPackage:生成的实体类所在的包 -->
        <!-- targetProject:生成的实体类所在的硬盘位置 -->
        <javaModelGenerator targetPackage="com.zking.zmall.pojo"
                            targetProject="src/main/java">
            <!-- 是否允许子包 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对modal添加构造函数 -->
            <property name="constructorBased" value="false"/>
            <!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>
        <!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.zking.zmall.mapper" targetProject="src/main/java">
            <!-- 针对 oracle 数据库的一个配置,是否把 schema 作为字包名 -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!-- tableName是数据库中的表名,domainObjectName是生成的JAVA模型名,后面的参数不用改,要生成更多的表就在下面继续加table标签 -->

        <table tableName="zmall_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="zmall_user_address" domainObjectName="UserAddress"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="zmall_product" domainObjectName="Product"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="zmall_product_category" domainObjectName="ProductCategory"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="zmall_order" domainObjectName="Order"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="zmall_order_detail" domainObjectName="OrderDetail"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="zmall_kill" domainObjectName="Kill"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="zmall_news" domainObjectName="News"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>

    </context>
</generatorConfiguration>

3.在generatorConfig.xml中修改数据库连接信息

1.指定 数据库、用户、密码

connectionURL="jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characeterEncoding=utf-8&amp;serverTimezone=UTC"

userId="root" password="123">

2.修改实体类生成所在包

<!-- targetPackage:生成的实体类所在的包 -->

<!-- targetProject:生成的实体类所在的硬盘位置 -->

<javaModelGenerator targetPackage="com.zking.zmall.pojo" targetProject="src/main/java"> <!-- 是否允许子包 -->

3.绑定数据库中的表

<!-- tableName是数据库中的表名,domainObjectName是生成的JAVA模型名,后面的参数不用改,要生成更多的表就在下面继续加table标签 -->

<table tableName="zmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

 

<table tableName="smbms_user" domainObjectName="User"
       enableCountByExample="false" enableUpdateByExample="false"
       enableDeleteByExample="false" enableSelectByExample="false"
       selectByExampleQueryId="false"></table>

<table tableName="smbms_role" domainObjectName="Role"
       enableCountByExample="false" enableUpdateByExample="false"
       enableDeleteByExample="false" enableSelectByExample="false"
       selectByExampleQueryId="false"></table>

<table tableName="smbms_bill" domainObjectName="Bill"
       enableCountByExample="false" enableUpdateByExample="false"
       enableDeleteByExample="false" enableSelectByExample="false"
       selectByExampleQueryId="false"></table>

<table tableName="smbms_provider" domainObjectName="Provider"
       enableCountByExample="false" enableUpdateByExample="false"
       enableDeleteByExample="false" enableSelectByExample="false"
       selectByExampleQueryId="false"></table>

<table tableName="smbms_address" domainObjectName="Address"
       enableCountByExample="false" enableUpdateByExample="false"
       enableDeleteByExample="false" enableSelectByExample="false"
       selectByExampleQueryId="false"></table>

生成前看src下有没有该结构没有自行调整 

 

4.运行插件指令生成即可 

 

  • 21
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Mybatis-Plus是一个基于Mybatis的增强工具,它提供了一些实用的功能,其中包括自动生成POJO的功能。使用Mybatis-Plus可以方便地生成POJO类,减少手动编写POJO的工作量,提高开发效率。生成POJO类可以根据数据库表结构自动生成,可以通过注解或XML配置进行自定义。 ### 回答2: MyBatis-Plus是在MyBatis框架的基础上增加了一系列的扩展功能,其中之一就是自动生成pojo的功能。 MyBatis-Plus的代码生成器可以自动生成数据库中每个表对应的实体类(POJO)、Mapper接口以及XML文件。这个功能可以大大降低程序员的开发量,提高开发效率。 使用MyBatis-Plus代码生成生成POJO的步骤如下: 1. 配置MyBatis-Plus的代码生成器。在pom.xml文件中添加MyBatis-Plus和MySQL驱动的依赖,然后在resources目录下创建generator.properties配置文件,配置数据库连接信息、包名、作者名等信息。 2. 编写生成器代码。在同一个配置文件中,配置输出路径、生成哪些模块(Controller、Service、ServiceImpl、Mapper等)、生成哪些表等信息。 3. 运行代码生成器。在generator文件夹下,运行MybatisPlusGeneratorApplication类即可开始生成POJO生成POJO代码默认是不包含注释的,可以在generator.properties文件中配置添加注释的规则。 总之,MyBatis-Plus自动生成POJO的功能可以大大减少程序员的开发工作量,提高开发效率,在写Java代码时已经成为了一个很常见的实践,非常值得推荐。 ### 回答3: MyBatis-Plus是针对MyBatis框架的增强工具,提供了许多方便使用的功能。其中,自动生成POJOMyBatis-Plus的一个重要特性。 自动生成POJO,可以帮助我们简化程序开发的过程,提高开发效率。 MyBatis-Plus自动生成POJO主要分为两种方式:基于代码生成器和基于IDE插件。 基于代码生成器,我们可以通过配置文件配置输出路径、父包名、数据库连接等参数,然后自动生成对应的实体类、mapper接口、mapper.xml文件等一系列文件,这样就省去了我们手写这些繁琐的代码的工作。 基于IDE插件,我们可以在IDE中安装MyBatis-Plus插件,通过插件提供的代码生成功能,自动根据数据库表结构生成对应的实体类、mapper接口、mapper.xml文件文件。 无论是哪种方式,MyBatis-Plus自动生成POJO都需要我们在配置文件中对数据库进行配置。在配置文件中,我们需要提供数据库连接信息,以及实体类所在的包名、映射文件所在的包名、数据表名等参数。MyBatis-Plus还提供了一些高级配置选项,如是否支持AR模式、生成的注释模板等,可以根据不同的需求进行调整。 需要注意的是,MyBatis-Plus自动生成POJO虽然可以大大简化我们的开发工作,但是有些情况下会存在一些问题。比如,数据库表结构发生变化时,我们需要手动修改对应的实体类和映射文件。此外,在生成过程中,MyBatis-Plus可能无法满足我们的需求,需要我们手动修改生成代码。因此,我们需要在使用MyBatis-Plus自动生成POJO的过程中,谨慎对待,灵活运用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值