说明:

(1)本篇博客的主要内容:

          ● 创建Spring Boot电商项目的工程:【mall】;

          ● 引入【mybatis-generator】插件;这个插件可以根据数据库表的内容,帮我们自动生成对应的实体类、Mapper接口、Mapper.xml文件;

          ● 同时,也可以看到我们在 【Spring Boot电商项目5:项目开发所需工具;(IDEA插件:【Maven Helper】,【Free Mybatis Plugin】;Postman;数据库可视化工具Navicat;)】中安装了【Free Mybatis Plugin】插件也生效了;这个插件的主要作用是:【在Mapper接口和Mapper.xml之间来回跳转】+【识别Mapper接口或Mapper.xml中的错误】;

(2)本篇博客需要注意的点:

          ● 我们在与数据库打交道的时候,最好要记得设置时区;

          ● 【mybatis-generator】这个插件其实挺给力的,其可以帮我们快速的创建实体类、mapper接口、mapper.xml等逆向文件;使我们不用去做一些八股文式的、“没有技术含量的”、繁重的创建工作,节省大量的时间;

目录

一:使用【IDEA集成的Spring Initializr】的方式,创建一Spring Boot工程;

二:首先,引入【Mybatis依赖】和【数据库驱动依赖】;

三:整合【mybatis-generator】插件;生成实体类、mapper接口、mapper.xml等逆向文件;

1.在pom.xml中,引入【mybatis-generator】插件;

2.创建并配置【mybatis-generator】插件的配置文件:generatorConfig.xml文件;

3.使用【mybatis-generator】插件生成实体类、mapper接口、mapper.xml等逆向文件;

4.附加说明:【Free Mybatis Plugin】插件也生效了:【在Mapper接口和Mapper.xml之间来回跳转】+【识别Mapper接口或Mapper.xml中的错误】;


一:使用【IDEA集成的Spring Initializr】的方式,创建一Spring Boot工程;

详细内容,可以参考【Spring Boot入门三:创建Spring Boot项目;(包括【Spring 官网start.spring.io】方式,【IDEA集成的Spring Initializr】方式)】;

springboot 插件式框架 springboot插件开发_springboot 插件式框架

springboot 插件式框架 springboot插件开发_后端_02

springboot 插件式框架 springboot插件开发_xml_03

springboot 插件式框架 springboot插件开发_springboot 插件式框架_04

springboot 插件式框架 springboot插件开发_后端_05

springboot 插件式框架 springboot插件开发_springboot 插件式框架_06

……………………………………………………

然后,差不多得等待一会,等待依赖的下载;

……………………………………………………

然后,启动项目:

springboot 插件式框架 springboot插件开发_springboot 插件式框架_07


二:首先,引入【Mybatis依赖】和【数据库驱动依赖】;

<!--mybatis相关的依赖-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.1</version>
		</dependency>
		<!--mysql相关依赖-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

说明:

(1)这儿如果有不明白的地方,可以参考【Spring Boot入门八:Spring Boot的一个全流程演示;(第一次见到了@Mapper注解)】;


三:整合【mybatis-generator】插件;生成实体类、mapper接口、mapper.xml等逆向文件;

mybatis-generator插件的主要作用是:mybatis.generator 简称 dalgen,解决mybatis代码自动生成的缺失。mybatis-generator-maven-plugin mybatis比较官方的代码生成器,生成d实体类、mapper接口、mapper.xml;且会生成大量mybatis动态sql。

1.在pom.xml中,引入【mybatis-generator】插件;

<plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.7</version>
        <configuration>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
</plugin>

说明:

(1)引入mybatis-generator插件的内容,不需要记忆,需要的时候,过来复制就行了;

springboot 插件式框架 springboot插件开发_xml_08

……………………………………………………

2.创建并配置【mybatis-generator】插件的配置文件:generatorConfig.xml文件;

springboot 插件式框架 springboot插件开发_数据库_09

<?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>
  <!-- 配置文件,放在resource目录下即可 -->
  <!--数据库驱动个人配置-->
  <classPathEntry
    location="E:/maven-repo/mysql/mysql-connector-java/8.0.18/mysql-connector-java-8.0.18.jar"/>
  <context id="MysqlTables" targetRuntime="MyBatis3">
    <property name="autoDelimitKeywords" value="true"/>
    <!--可以使用``包括字段名,避免字段名与sql保留字冲突报错-->
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
    <!-- optional,旨在创建class时,对注释进行控制 -->
    <commentGenerator>
      <property name="suppressDate" value="true"/>
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--数据库链接地址账号密码-->
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
      connectionURL="jdbc:mysql://127.0.0.1:3306/imooc_mall?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull"
      userId="root"
      password="12345">
      <property name="nullCatalogMeansCurrent" value="true"/>
    </jdbcConnection>
    <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
    <javaTypeResolver>
      <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>
    <!--生成Model类存放位置-->
    <javaModelGenerator targetPackage="com.imooc.mall.model.pojo"
      targetProject="src/main/java">
      <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
      <property name="enableSubPackages" value="true"/>
      <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
      <property name="trimStrings" value="true"/>
      <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
      <property name="immutable" value="false"/>
    </javaModelGenerator>
    <!--生成mapper映射文件存放位置-->
    <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
      <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>
    <!--生成Dao类存放位置-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.imooc.mall.model.dao"
      targetProject="src/main/java">
      <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>
    <!--生成对应表及类名-->
    <table schema="root" tableName="imooc_mall_cart" domainObjectName="Cart"
      enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_category" domainObjectName="Category" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_order" domainObjectName="Order" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_order_item" domainObjectName="OrderItem"
      enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_product" domainObjectName="Product" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_user" domainObjectName="User" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>

  </context>
</generatorConfiguration>

说明:

(1) 这儿报红,是正常的,不需要额外处理,目前没必要过于深究;

springboot 插件式框架 springboot插件开发_spring boot_10

(2)数据库驱动文件地址的配置,数据库驱动的位置需要修改下为自己本地maven仓库的驱动位置;因为在【Maven十:修改本地仓库地址;(修改settings.xml 并 在IDE中设置;这个博客东西很少,内容相对独立)】自己修改了maven本地仓库的地址,所以,自己这个项目中引入的MySQL驱动的下载位置就是:

springboot 插件式框架 springboot插件开发_后端_11

所以,这儿的数据库驱动位置需要修改为自己本机的maven仓库的数据库驱动的位置:

springboot 插件式框架 springboot插件开发_后端_12

(3)说明;

springboot 插件式框架 springboot插件开发_spring boot_13

(3)数据库连接字符串,数据库用户名和密码;

springboot 插件式框架 springboot插件开发_springboot 插件式框架_14

(4)需要注意,这儿的数据库需要设置下时区;虽然,这儿我们不是连接数据库,而只是【mybatis-generator】插件连接数据库,这儿还是需要设置时区的;

springboot 插件式框架 springboot插件开发_springboot 插件式框架_15

这也是一个经常遇到但又容易忽略的点,前面说过很多次,可以参考:【JDBC入门四:JDBC驱动分析:下载MySQL驱动;将驱动添加到工程中;加载驱动类(5步中的第1步)分析;创建数据库连接(5步中的第2步)分析;第1和第2步的案例和相关异常分析;】;【IDEA连接MySQL时,报“ Server returns invalid timezone.Go to ‘Advanced‘ tab and set ‘serverTimezone‘ ……”】,【IDEA连接MySQL时,报“ Server returns invalid timezone……”(这是在IDEA中设置时区啦,和上篇转载博客的在命令行中设置本质是一样的啦。)】;虽然,这几篇博客介绍的内容和本篇博客的内容,表象不是完全一样,但其本质都是一样的:即,当我们在与数据库打交道的时候,尽量不要忘记设置时区;

(5)设置:实体类、Mapper.xml文件、Mapper接口存放的位置;

springboot 插件式框架 springboot插件开发_spring boot_16

(6)设置:生成对应实体类和数据库表的对应;

springboot 插件式框架 springboot插件开发_springboot 插件式框架_17

3.使用【mybatis-generator】插件生成实体类、mapper接口、mapper.xml等逆向文件;

springboot 插件式框架 springboot插件开发_xml_18

springboot 插件式框架 springboot插件开发_后端_19

springboot 插件式框架 springboot插件开发_spring boot_20

springboot 插件式框架 springboot插件开发_springboot 插件式框架_21

springboot 插件式框架 springboot插件开发_spring boot_22

4.附加说明:【Free Mybatis Plugin】插件也生效了:【在Mapper接口和Mapper.xml之间来回跳转】+【识别Mapper接口或Mapper.xml中的错误】;

我们在【Spring Boot电商项目5:项目开发所需工具;(IDEA插件:【Maven Helper】,【Free Mybatis Plugin】;Postman;数据库可视化工具Navicat;)】中安装了【Free Mybatis Plugin】插件;这个插件的作用就是:方便我们在Mapper接口和Mapper.xml之间跳转,同时也可以帮助识别Mapper.xml中的错误;

此时,就可以看到【Free Mybatis Plugin】插件已经生效了:

作用1:这儿有绿色的三角:可以很好的在Mapper接口和Mapper.xml之间来回跳转:

springboot 插件式框架 springboot插件开发_spring boot_23

作用2:识别Mapper接口或Mapper.xml中的错误;

springboot 插件式框架 springboot插件开发_数据库_24