springboot整合Mybatis-Generator

这个作业属于哪个课程 班级链接
这个作业要求在哪里 作业要求
这个作业的目标 1. 技术概述
2. 技术详述
3. 问题与解决
4. 总结
其他参考文献 参考一/dalaoyang
参考二/FoolFox_
参考三/xyz@Easion

一、技术概述

描述这个技术是做什么的/什么情况下会使用到这个技术,学习该技术的原因,技术的难点在哪里。控制在50-100字内。

  • Mybatis-Generator用于自动生成dao层、model层、mapper.xml,并且可以自动实现单表增删改查等基本原子操作和其拓展操作。
  • 原因:提高开发效率,减少一些重复性操作,为开发提供便利。
  • 难点:配置文件的理解和编写。

二、技术详述

描述你是如何实现和使用该技术的,要求配合代码和流程图详细描述。可以再细分多个点,分开描述各个部分。

1.整合Mybatis

  • 引入maven依赖包,包括mybatis相关依赖包和mysql驱动包。

  • mybatis-generator-maven插件的配置
  • 保证application.yml里面有数据库连接的配置。并配置mybatis的xml文件存放位置。
  • 配置Mybatis的Mapper类文件的包扫描路径

2.编写XML配置文件实现Mybatis Generator代码生成配置

  • 在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>

    <!-- 引入配置文件 -->
    <properties resource="mybatisGeneratorinit.properties"/>

    <!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->
    <classPathEntry location="${jdbc_location}"/>

    <!-- 一个数据库一个context,defaultModelType="flat" 大数据字段,不分表 -->
    <context id="MysqlTables" targetRuntime="MyBatis3Simple" >

        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->
        <property name="autoDelimitKeywords" value="true"/>

        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="utf-8"/>

        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
        <!-- <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/> -->

        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>

        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        <!-- 注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳-->
        </commentGenerator>


        <!-- 【数据库连接信息】 -->
        <!-- 数据库连接配置的connectionURL不能直接使用&,需将“&”改为“&amp;”。。吐血 -->
        <jdbcConnection driverClass="${jdbc_driver}"
                        connectionURL="${jdbc_url}"
                        userId="${jdbc_user}"
                        password="${jdbc_password}">
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>

        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体/模型类地址 -->
        <javaModelGenerator targetPackage="${pojo_package}" targetProject="${project}">
            <!-- 是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对modal添加构造函数 -->
            <property name="constructorBased" value="true" />
            <!-- 从数据库返回的值去掉前后空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成map.xml 文件存放地址 -->
        <sqlMapGenerator targetPackage="${mapper_location}" targetProject="${resources}">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 生成接口dao -->
        <javaClientGenerator targetPackage="${dao_package}" targetProject="${project}" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 enableSelectByPrimaryKey相应的配置表示是否生成相应的接口 -->
        <!-- <table tableName="report" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true">     
        	<property name="useActualColumnNames" value="true"/>
        </table> -->

        <!--
        <table tableName="admin"
              enableCountByExample="true"
              enableUpdateByExample="true"
              enableDeleteByExample="true"
              enableSelectByExample="true"
              selectByExampleQueryId="true"

              enableSelectByPrimaryKey="true"
              enableUpdateByPrimaryKey="true"
              enableDeleteByPrimaryKey="true">
           如果以下设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法,
                比如BORN_DATE,生成的属性名字就是BORN_DATE,而不会是bornDate
           <property name="useActualColumnNames" value="true" />
           数据库表主键
           <generatedKey column="id" sqlStatement="Mysql" identity="true" />
       </table>
       <table tableName="user" enableCountByExample="true" enableUpdateByExample="true"
              enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"
              enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
              enableDeleteByPrimaryKey="true">
           <property name="useActualColumnNames" value="true"/>
           <generatedKey column="id" sqlStatement="Mysql" identity="true" />
       </table>
       -->

        <!--
       <table tableName="admin">
           <property name="useActualColumnNames" value="true"/>
       </table>
       <table tableName="user">
           <property name="useActualColumnNames" value="true"/>
       </table>
       -->


        <!--本次生成结束后-->
        <!--
        <table tableName="user" enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"
               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true">
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="uid" sqlStatement="Mysql" identity="true" />
        </table>

        <table tableName="paper" enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"
               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true">
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="pid" sqlStatement="Mysql" identity="true" />
        </table>

        <table tableName="keyword" enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"
               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true">
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="kpid" sqlStatement="Mysql" identity="true" />
        </table>
        -->

    </context>
</generatorConfiguration>
  • 配置文件的同目录下创建:mybatisGeneratorinit.properties
#Mybatis Generator configuration
#dao类和实体类的位置
project =src/main/java
#mapper文件的位置
resources =src/main/resources
#JDBC dirver 具体位置
jdbc_location =G:\\.m2\\repository\\mysql\\mysql-connector-java\\8.0.18\\mysql-connector-java-8.0.18.jar

#数据库连接信息
jdbc_driver =com.mysql.cj.jdbc.Driver
jdbc_url =jdbc:mysql://localhost/xxx?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=Etc/GMT-8
jdbc_user =root
jdbc_password =password

#根据数据库中的表生成对应的pojo类、dao、mapper
pojo_package =com.practice.pairproject.pojo
dao_package =com.practice.pairproject.dao
mapper_location =mapper

3.执行

  • 法一:在idea中添加一个mybatis generator maven插件启动选项,点击Run。选择Edit Configuration… ,点击加号"+"添加,选择maven,填写名称(这里用mybatis generator)。命令行:mybatis-generator:generate -e
  • 法二:利用maven引入的Mybatis-Generator插件

三、问题与解决

技术使用中遇到的问题和解决过程。要求问题的描述和解决有一定的内容,不能草草概括。要让遇到相关问题的人看了你的博客之后能够解决该问题。

  • 在Maven侧边栏找不到mybatis-generator,无法双击:1.一定要将mybatis-generator插件的配置放在plugins标签中。2.<configuration>中需要加上<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>去标明配置文件的位置。
  • 报错 - [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:generate (default-cli) on project mall: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:generate failed:将jdbc_location路径的“/”改为“\”即可。
  • 报错 - XML Parser Error on line 149: 元素类型为 "context" 的内容不完整, 它必须匹配 "(property,plugin,commentGenerator?,(connectionFactory|jdbcConnection),javaTypeResolver?,javaModelGenerator,sqlMapGenerator?,javaClientGenerator?,table+)"。:配置文件generatorConfig.xml中<generatorConfiguration><context>标签内一定要有<table>标签,对应数据库的某一个表,否则无法执行。

四、总结

  • 通过了解Mybatis-Generator的配置文件各个部分的标签、属性及其属性值的意思,可以很方便的进行逆向工程的实现,可以自动生成dao、model、mapper.xml文件,并且可以控制mapper中要实现哪些原子操作和拓展操作,为搭建框架提供了很大便利。在之后的项目中,也都可以使用Mybatis-Generator来生成代码。相关的工具还有MyBatis-Plus,其在 MyBatis 的基础上只做增强不做改变,也可以提高开发效率,之后可进行进一步的了解、学习。

参考

见博客顶部

posted @ 2021-06-26 21:07  J-Letoo  阅读(312)  评论(0)    收藏  举报