当前位置: 首页 > news >正文

使用MyBatis生成器

在编写项目时,我们常常需要与MySQL进行连接,在此基础上,如果我们需要使用数据库中的数据,就需要先创建对应的实体类,然后再编写对应的SQL语句,但是这一部分操作是有便捷操作的

导入依赖

我们需要现在 pom 文件中导入对应的MyBatis生成器依赖

<!-- 将这一部分导入到 properties 中 -->
<!--  MyBatis生成器  -->
<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>
<!-- 将这一部分导入到 plugins 中 -->
<!-- mybatis 生成器插件 -->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>${mybatis-generator-plugin-version}</version>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <phase>deploy</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <!-- 相关配置 -->
    <configuration>
        <!-- 打开日志 -->
        <verbose>true</verbose>
        <!-- 允许覆盖 -->
        <overwrite>true</overwrite>
        <!-- 配置文件路径 -->
        <configurationFile>
            src/main/resources/mybatis/generatorConfig.xml
        </configurationFile>
    </configuration>
</plugin>

添加完之后,让 maven 重新加载,让依赖成功导入

根据设置的配置文件路径添加配置文件

在 pom 文件中加入的依赖中,我们加入了一个配置文件路径,我们需要根据这个文件路径进行文件创建.后续需要通过这个文件来使用生成器

依赖中的地址: src/main/resources/mybatis/generatorConfig.xml (这个地址可自定义,但是自定义一定要记得修改依赖中设置的地址)

<generatorConfiguration>
    <!--  驱动包路径,location中路径替换成自己本地路径  -->
    <classPathEntry location="本地驱动包地址"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--  禁用自动生成的注释  -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!--  连接配置(自己的数据库连接配置)  -->
        <!--  driverClass5.0配置: com.mysql.jdbc.Driver  -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/table_name?characterEncoding=utf8&amp;useSSL=false"
                        userId="root" password="password"> </jdbcConnection>
        <javaTypeResolver>
            <!--  小数统一转为BigDecimal  -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--  实体类生成位置  -->
        <javaModelGenerator targetPackage="com.example.forum.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--  mapper.xml生成位置  -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--  DAO类生成位置  -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.forum.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--  配置生成表与实例, 只需要修改表名tableName, 与对应类名domainObjectName 即可 -->
        <table tableName="t_article" domainObjectName="Article" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false">
            <!--  类的属性用数据库中的真实字段名做为属性名, 不指定这个属性会自动转换 _ 为驼峰命名规则 -->
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="t_article_reply" domainObjectName="ArticleReply" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="t_board" domainObjectName="Board" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="t_message" domainObjectName="Message" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="t_user" domainObjectName="User" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

那么我们要怎么找到本地的驱动包呢?

        1.打开 此电脑 ,找到 user 文件夹

        2.在文件夹中找到 .m2 文件夹

        3.找到 repository

        4.双击打开之后,在文件夹里面搜索 mysql 文件夹

        5.找到自己使用的版本,复制对应的 jar 包的文件路径

运行

将所有文件配置好之后,我们打开项目,找到 maven 键,打开 Plugins ,找到 mybatis-generator ,并双击 mybatis-generator:generate

控制台出现以下信息,并且开始创建 实例类 和 对应的 mapper.xml 文件那就是配置成功了

问题处理

根元素匹配

如果在运行生成器的时候控制台出现了图片中的问题,那么我们需要在配置文件中加入以下代码

<!--  以下代码加在 generatorConfiguration 之前 -->
<?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">

相关文章:

  • ZOJ 1012 Mainframe
  • 关于XML映射器的基本问题
  • 【线性代数】2矩阵
  • DC-8靶机渗透测试全过程
  • Unity学习part2
  • python从入门到进去
  • [高等数学] 分部积分法
  • 计网-数据链路层
  • 【华为OD机考】华为OD笔试真题解析(10)--字符串重新排序
  • 使用 Python 爬虫和 FFmpeg 爬取 B 站高清视频
  • 【Python】错误异常
  • 【深度学习】计算机视觉(CV)-目标检测-DETR(DEtection TRansformer)—— 基于 Transformer 的端到端目标检测
  • DAY04 Object、Date类、DateFormat类、Calendar类、Math类、System类
  • 什么是计算机总线?
  • 【大模型】DeepSeek 高级提示词技巧使用详解
  • Android Studio报错:Could not get unknown property ‘kotlin_version‘
  • Kubernetes控制平面组件:etcd高可用集群搭建
  • c语言(初阶函数)完结
  • 函数防抖和节流
  • AI 语言模型发展史:统计方法、RNN 与 Transformer 的技术演进
  • 巴基斯坦外长:近期军事回应是自卫措施
  • 玉渊谭天丨中方为何此时同意与美方接触?出于这三个考虑
  • 视频丨习近平同普京在主观礼台出席红场阅兵式
  • 商务部再回应中美经贸高层会谈
  • 国家发改委副主任谈民营经济促进法:以法治的稳定性增强发展的确定性
  • 【社论】三个“靠谱”为市场注入确定性