SpringBoot整合mybatis-generator插件流程详细讲解

mybatis-generator 插件

mybatis 相关依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.25</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.15</version>
</dependency>

mybatis-generator 插件,自动生成mybatis所需要的 dao、bean、mapper.xml文件。

<build>
    <plugins>
        <!-- mybatis 插件 -->
        <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>
    </plugins>
</build>

创建generatorConfig.xml 文件,是这个插件的配置文件

需要第八行<classPathEntry location=“项目mysql-connection-java-版本.jar 包的绝对路径”

<?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="application.properties"/>
  <!-- mysql驱动的位置 这个是MySQL连接的jar包,你需要指定你自己计算机上的jar包的位置-->
  <classPathEntry location="E:\Java\IDEA\code\mall\src\main\resources\lib\mysql-connector-java-8.0.25.jar" />
  <context id="MysqlTables" targetRuntime="MyBatis3">
    <property name="autoDelimitKeywords" value="true"/>
    <!--可以使用``包括字段名,避免字段名与sql保留字冲突报错-->
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
    <!-- 是否生成注释 -->
    <commentGenerator>
      <!-- 是否生成注释代时间戳 -->
      <property name="suppressDate" value="true"/>
      <!-- 是否去除自动生成的注释 true:是 : false:否 -->
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--数据库链接地址账号密码-->
    <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
      connectionURL="${spring.datasource.url}"
      userId="${spring.datasource.username}"
      password="${spring.datasource.password}">
      <property name="nullCatalogMeansCurrent" value="true"/>
    </jdbcConnection>
    <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
         NUMERIC 类型解析为java.math.BigDecimal -->
    <javaTypeResolver>
      <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!-- 生成实体类地址 这里需要你改动,其中 targetPackage 需要根据你自己创建的目录进行改动 -->
    <javaModelGenerator targetPackage="${generator.javaModel-targetPackage}"
      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="true"/>
    </javaModelGenerator>
    <!--生成mapper映射文件存放位置-->
    <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
      <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>
    <!--生成Dao类存放位置-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="${generator.mappers}"
      targetProject="src/main/java">
      <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>
    <!--生成对应表及类名-->
    <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 -->
    <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>

然后修改 application.properties 文件,增加一点配置项。

当然,也可以选择直接在 generatorConfig.xml 文件中书写,但想要运行整个项目,也是必须配置mysql的。

#mysql配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
#mybatis
#指定mapper.xml文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
#设置pojo类型别名
mybatis.type-aliases-package=com.daxiong.mall.pojo
#Generator配置
#生成pojo位置
generator.javaModel-targetPackage=com.daxiong.mall.pojo
#生成接口位置
generator.mappers=com.daxiong.mall.mapper

启动插件生成文件:打开maven工具栏-》选择当前项目名-》Plugins-》mybatis-generator -》双击运行 mybatis-generator:generate 。

此时,便会**自动生成**mybatis所需要的 dao、bean、mapper.xml文件

自动生成的mapper接口示例:

// 根据 主索引字段 删除
int deleteByPrimaryKey(Integer id);
// 插入
int insert(Order record);
// 可选择性插入(值为null的字段不插入,使用默认值)
int insertSelective(Order record);
// 根据 主索引字段 查询
Order selectByPrimaryKey(Integer id);
// 可选择性更新(值为null的字段不插入,使用默认值)
int updateByPrimaryKeySelective(Order record);
// 更新
int updateByPrimaryKey(Order record);

通知MyBatis,mapper接口存放的位置:

方法一:为 主启动类添加注解:

@MapperScan(basePackages = “com.daxiong.mall.mapper”):告诉 ,mapper 接口放在了哪里。

方法二:为为每个 mapper 接口添加注解:

@Mapper:让此接口在Spring Ioc 初始化时生成 bean,以便其他类注入使用。

到此这篇关于SpringBoot整合mybatis-generator插件流程详细讲解的文章就介绍到这了,更多相关SpringBoot整合mybatis-generator内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • IDEA创建SpringBoot项目整合mybatis时mysql-connector-java报错异常的详细分析

    目录 报错如图: 解决方式一: 解决方式二: 总结 For artifact {com.mysql:mysql-connector-j:null:jar}: The version cannot be empty.报错异常分析: 报错如图: 在pom.xml文件中 会是报红状态,我一直以为是导入不完全,是mysql-connector-java没有写全,所以一直解决不了,最后发现mysql-connector-java自8.0.31后更名为mysql-connector-j了. 现在分析一下它给

  • 手把手教你SpringBoot整合Mybatis

    目录 Mybatis的简单介绍 1 环境搭建 2 整合方式一:注解版 2.1 配置 2.2 编码 2.3 测试 3 整合方式二:XML版 3.1 配置 3.2 编码 3.3 测试 4 总结 Mybatis的简单介绍 ​ MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .2013年11月迁移到Github. ​ iBATIS一词来源于“internet”

  • SpringBoot整合Mybatis之各种查询、模糊查询、批量删除、动态表名操作

    目录 一.普通查询 1. 若查询出的数据只有一条 2. 若查询出的数据有多条 二.模糊查询 like "%"#{name}"%" 三.批量删除 in (${ids}) 一.普通查询 1. 若查询出的数据只有一条 a>可以通过实体类对象接收 <!-- Dish getDishByName(@Param("name") String name);--> <select id="getDishByName"

  • SpringBoot整合Mybatis与MybatisPlus方法详细讲解

    目录 一.整合MyBatis操作 1.配置模式 2.注解模式 3.混合模式 二.整合 MyBatis-Plus 完成CRUD 1.什么是MyBatis-Plus 2.整合MyBatis-Plus 3.CRUD功能 一.整合MyBatis操作 官网:MyBatis · GitHub SpringBoot官方的Starter:spring-boot-starter-* 第三方的starter的格式: *-spring-boot-starter <dependency> <groupId>

  • SpringBoot整合TKMyBatis实现单表增删改查操作

    目录 什么是TKMybatis SpringBoot整合TKMybatis 实体类注解 TKMapper接口如何使用 基本增删改操作 批量查询和删除 批量添加 自定义查询条件Example Example 条件设置 Example 使用 什么是TKMybatis TKMybatis 是基于Mybatis 框架开发的一个工具,内部实现了对单表的基本数据操作,只需要简单继承 TKMybatis 提供的接口,就能够实现无需编写任何 sql 即能完成单表操作. SpringBoot整合TKMybatis

  • SpringBoot整合Mybatis Generator自动生成代码

    目录 1.创建SpringBoot项目 2. mybatis-generator-maven插件的配置 3. 项目结构构建 4. application.yml配置 5. generatorConfig.xml配置 7. 选择 Mybatis Generator 启动,自动在dao.entity.mapper包下生成代码 Mybatis是目前主流的ORM框架,相比于hibernate的全自动,它是半自动化需要手写sql语句.接口.实体对象,后来推出的Generator自动生成代码,可以帮我们提高

  • Springboot整合mybatis的步骤

    前期工作 1.导入mybatis整合依赖 <!-- mybatis整合 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> 2.连接数据库 3.连接完

  • 使用sts工具、SpringBoot整合mybatis的详细步骤

    SpringBoot 集成 Mybatis 框架 一.1.SpringBoot 集成 Mybatis 的基本步骤 第一步:添加依赖: 第二步:配置数据源: 第三步:扫描接口包. 二.详细的集成步骤如下: 1.第一步:添加依赖: 添加依赖:除了常规依赖外,需要加入 Mybatis 代码如下(示例): <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XM

  • SpringBoot整合Thymeleaf小项目及详细流程

    目录 1.项目简绍 2.设计流程 3.项目展示 4.主要代码 1.验证码的生成 2.userController的控制层设计 3.employeeController控制层的代码 4.前端控制配置类 5.过滤器 6.yml配置 7.文章添加页面展示 8.POM.xml配置类 9.employeeMapper配置类 10.UserMapper配置类 1.项目简绍 本项目使用SpringBoot开发,jdbc5.1.48 Mybatis 源码可下载 其中涉及功能有:Mybatis的使用,Thymel

  • SpringBoot整合mybatis结合pageHelper插件实现分页

    SpringBoot整合mybatis分页操作 SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper, 关于pageHelper的介绍,请查看官方文档: https://pagehelper.github.io/ 1.使用前配置 关于pageHelper的使用配置,主要有以下2个步骤: 1.1.在pom文件中导入pageHelper依赖 <dependency> <groupId>com.github.pagehelper&

  • 详解SpringBoot整合MyBatis详细教程

    1. 导入依赖 首先新建一个springboot项目,勾选组件时勾选Spring Web.JDBC API.MySQL Driver 然后导入以下整合依赖 <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> &

  • Spring SpringMVC,Spring整合MyBatis 事务配置的详细流程

    整合思路 (1)SSM是什么? Spring,SpringMVC,Mybastis (2)思路 搭建整合的环境,初始化环境 搭建Spring环境,配置完成并测试 (service层) 再使用Spring整合MyBatis框架,并测试(Dao层) 最后使用Spring整合SpringMVC框架,并测试(web层) SSM搭建环境 (1)数据库创建ssm (2)创建maven工程 (3)git (创建.gitignore来过滤不用提交的文件) (4)依赖框架 (5)log4j.properties

  • SpringBoot整合MyBatis超详细教程

    1.整合MyBatis操作 前面一篇提到了SpringBoot整合基础的数据源JDBC.Druid操作,实际项目中更常用的还是MyBatis框架,而SpringBoot整合MyBatis进行CRUD也非常方便. 下面从配置模式.注解模式.混合模式三个方面进行说明MyBatis与SpringBoot的整合. 1.1.配置模式 MyBatis配置模式是指使用mybatis配置文件的方式与SpringBoot进行整合,相对应的就有mybatis-config.xml(用于配置驼峰命名,也可以省略这个文

  • springboot整合mybatis流程详解

    目录 1.mybatis是什么 2.整合 2.1 导入依赖 2.2 创建包和类 2.3 在application.yaml配置mybatis 3.使用注解版mybaits 4.实战过程 1.mybatis是什么 MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射.MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作.MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型.接口和 Java POJO(Plain Old Java

随机推荐