MybatisGenerator使用

一、MyBatis Generator是什么?

MyBatis Generator (MBG) 是 MyBatis 的代码生成器。它能够根据数据库表,自动生成 java 实体类、dao 层接口(mapper 接口)及mapper.xml文件。这大大减少了我们建立实体类、dao 接口及 mapper 映射文件的繁琐任务,可以节省很多时间,专注于业务层代码的开发。

二、Mybatis Generator 的maven插件使用

1.依赖引入

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <!--引入mysql连接器  版本号需与添加的依赖一致-->
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
    </dependencies>
    <!--添加扫描generatorConfig.xml文件-->
    <configuration>
        <configurationFile>${basedir}/src/main/resources/generatorConfig.xml
        </configurationFile>
        <overwrite>true</overwrite>
        <verbose>true</verbose>
    </configuration>
</plugin>

2.配置generatorConfig.xml文件

在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>
    <!--    连接数据库jar包的路径-->
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库连接信息:驱动类、链接地址、用户名、密码 -->
        <jdbcConnection
                driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/mall?			                             useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=UTC"
                userId="root"
                password="password">
            <!--高版本的 mysql-connector-java 需要设置 nullCatalogMeansCurrent=true-->
            <!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题-->
            <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>

        <javaTypeResolver>
            <!--类型解析器-->
            <!-- 默认false,把jdbc decimal 和 numeric 类型解析为integer -->
            <!-- true,把jdbc decimal 和 numeric 类型解析为java.math.bigdecimal-->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 实体类的包名和存放路径 -->
        <javaModelGenerator targetPackage="com.example.springboot.pojo" 		                         targetProject="src/main/java">
            <!-- 是否让schema作为包后缀 默认是false会在 po 目录下在创建一个 “数据库名” 的文件夹,生成				的 po 会放在该文件夹下,也就是说会多一层目录
            -->
            <property name="enableSubPackages" value="true"/>
            <!-- 从数据库返回的值被清理前后的空格-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成映射文件*.xml的位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成Mapper接口的包名和位置
            type="XMLMAPPER" 会将接口的实现放在 mapper.xml中,也推荐这样配置。
            type="ANNOTATEDMAPPER",接口的实现通过注解写在接口上面
         -->
        <javaClientGenerator type="XMLMAPPER"                                            			targetPackage="com.example.springboot.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- tableName:数据库中的表名或视图名;domainObjectName:生成的实体类的类名-->
        <table tableName="user_base_info" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>


3.使用

在这里插入图片描述

效果展示

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值