Mybatis-generator的基础使用

2022年8月24日18:34:27 周二
环境:IntelliJ IDEA 2020.2.4版本


前言

  学习使用mybatis-generator快速生成Mybatis实体、Mapper接口和映射文件的基础配置方法,生成方式为使用Maven插件。

1 数据准备

  事先在数据库中准备若干表项,见下图。
在这里插入图片描述
  以第一个表 tb_area 为例,其中字段内容详情见下图。
在这里插入图片描述

2 Mabatis-generator的配置

  新建项目:选择 webapp 模板(见下图),填写项目名称后 Next,最后点击 Finish
image.png
  加载完成后目录结构和界面样式见下图,可在项目包上右键选择 New -> Directory 可创建完整的 Maven 包结构。
image.png
  项目目录添加完成后,在 src/main/java 包下新建项目包 edu/mint,并创建两个子包 daoentity (有的项目称 pojo,本质相同,只需配置时作相应更改)。在 src/main/resources 下添加 generatorConfig.xmldatasource.properties 配置文件。
  然后在 pom.xml 文件中引入 Maven 插件:mybatis-generator-maven-plugin
  上述操作完成后的项目结构见下图。
在这里插入图片描述

3 Mabatis-generator的使用

  选择右侧工具栏的 Maven 选项,双击 Pluginsmybatis-generator 底下的 mybatis-generator:generate (如下图蓝色部分所示)。
在这里插入图片描述
  构建成功后编辑器底部终端显示构建结果,见下图。
在这里插入图片描述
  原来空的 daoentity 包下都生成了对应的接口和实体类,resources 包下生成了 mappers 包,见下图。
在这里插入图片描述
  以表 tb_area 的构建结果为例。下图为 entity 包下的 Area 实体类,各属性与表格字段相对应。
在这里插入图片描述
  下图为 dao 包下的 AreaMapper 接口内容。
在这里插入图片描述
  AreaMapper.xml 映射文件内容见下图。
在这里插入图片描述
  至此,我们成功配置并使用了Mybatis-generator。

附录

  完整配置代码和配置文件内容如下:


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>org.example</groupId>
    <artifactId>mbg_o2o</artifactId>
    <version>1.0-SNAPSHOT</version>
	
	<!-- 添加mybatis-generator-maven-plugin -->
    <build>
        <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>
</project>

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>
    <!--导入属性配置-->
    <properties resource="datasource.properties"></properties>

    <!--指定特定数据库的jdbc驱动jar包的位置-->
    <classPathEntry location="${db.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">

        <!-- 可选配置,旨在创建class时,对注释进行控制(取消字段生成后的注释) -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的数据库连接 -->
        <jdbcConnection
                driverClass="${db.driverClassName}"
                connectionURL="${db.url}"
                userId="${db.username}"
                password="${db.password}">
        </jdbcConnection>

        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java"> (mac或linux环境下路径)-->
        <javaModelGenerator targetPackage="edu.mint.entity" targetProject="./src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加 构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">
        <!-- <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources"> (mac或linux环境下路径)-->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->

        <!-- targetPackage:mapper接口dao生成的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="edu.mint.dao" targetProject=".\src\main\java">
<!--        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java"> (mac或linux环境下路径)-->
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!--
            指定数据库表
                tableName数据库表名
                domainObjectName生成的实体类名
                是否需要mapper配置文件加入sql的where条件查询,需要将enableCountByExample等设为true,会生成一个对应domainObjectName的Example类
         -->
        <table tableName="tb_area" domainObjectName="Area" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="tb_head_line" domainObjectName="HeadLine" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="tb_local_auth" domainObjectName="LocalAuth" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="tb_person_info" domainObjectName="PersonInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="tb_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="tb_product_category" domainObjectName="ProductCategory" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="tb_product_img" domainObjectName="ProductImg" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="tb_shop" domainObjectName="Shop" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="tb_shop_category" domainObjectName="ShopCategory" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="tb_wechat_auth" domainObjectName="WechatAuth" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

datasource.properties(需要事先下载 `mysql-connector-java-5.1.6.jar` ,本例将其放在项目中了,放在任意位置都可以,第一行指定其绝对路径就可以)
db.driverLocation=D:\\JavaWorkSpace\\o2o\\lib\\mysql-connector-java-5.1.6.jar
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/o2o?characterEncoding=utf-8
db.username=root
db.password=xxx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值