前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot 及 MyBatis 逆向工程 生成mapper.xml,mapper接口及entity类

SpringBoot 及 MyBatis 逆向工程 生成mapper.xml,mapper接口及entity类

作者头像
Freedom123
发布2024-03-29 09:12:03
1310
发布2024-03-29 09:12:03
举报
文章被收录于专栏:DevOpsDevOps

MyBatis 提供了逆向工程,能够通过MySql 库中的表信息快速的生成 与数据库表相对应的 entity类,mapper.xml 及 mapper接口。  0. 数据库配置信息

代码语言:javascript
复制
 mysql://10.10.34.28:3306/gaea  root/123456

1. 数据库创建表

代码语言:javascript
复制
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
`id` int(32) NOT NULL AUTO_INCREMENT,
`userName` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`passWord` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`realName` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

2. 创建 Spring Boot 工程     通过IDEA创建Spring Boot工程,选择Web,DevOpt,MySql,JDBC,MyBatis,lombok 等模块,本工程需要使用swagger插件,增加swagger配置文件及pom.xml 依赖文件,关于swagger的配置,前面有专门章节介绍,请出门左拐,这里不作过多解释。3. 配置pom.xml dependencies节点下添加:

代码语言:javascript
复制
<!-- SpringBoot - MyBatis -->
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.3.0</version>
</dependency>
<!-- SpringBoot - MyBatis 逆向工程 -->
<dependency>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-core</artifactId>
  <version>1.3.2</version>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.40</version>
</dependency>

plugins节点下添加:

代码语言:javascript
复制
<plugin>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-maven-plugin</artifactId>
  <version>1.3.2</version>
  <configuration>
    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
    <overwrite>true</overwrite>
    <verbose>true</verbose>
  </configuration>
</plugin>

    注意:此处需要指定generatorConfig.xml 

4. 配置application.yml 文件

代码语言:javascript
复制
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://10.10.34.28:3306/gaea
    username: root
    password: 123456
server:
  port: 8080

  注意:指定jdbc驱动类型,rul及user,passwrod信息 5. 配置generatorConfig.xml 文件

代码语言:javascript
复制
<?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.yml" />
  <!-- mysql驱动的位置 这个是MySQL连接的jar包,你需要指定你自己计算机上的jar包的位置-->
  <classPathEntry location="G:/maven/localwarehouse/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar" />

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

    <!-- 注释 -->
    <commentGenerator>
      <!-- 是否生成注释代时间戳 -->
      <property name="suppressDate" value="true"/>
      <!-- 是否去除自动生成的注释 true:是 : false:否 -->
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>

    <!-- JDBC连接 其中connectionURL后面的newtest改为你创建的数据库,紧跟在后面是数据库连接的账户和密码-->
    <jdbcConnection
      driverClass="com.mysql.jdbc.Driver"
      connectionURL="jdbc:mysql://10.10.34.28:3306/gaea"
      userId="root"
      password="123456">
    </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="com.xxx.pojo" targetProject="src/main/java">
      <!-- 从数据库返回的值被清理前后的空格 -->
      <property name="trimStrings" value="true" />
      <!-- enableSubPackages:是否让schema作为包的后缀 -->
      <property name="enableSubPackages" value="false" />
    </javaModelGenerator>

    <!-- 生成mapper xml文件 这里不需要改动 -->
    <sqlMapGenerator targetPackage="com.xxx.mapper"  targetProject="src/main/resources">
      <!-- enableSubPackages:是否让schema作为包的后缀 -->
      <property name="enableSubPackages" value="false" />
    </sqlMapGenerator>

    <!-- 生成mapper xml对应Client   这里需要改动targetPackage,依据你自己的工程-->
    <javaClientGenerator targetPackage="com.xxx.mapper" targetProject="src/main/java" type="XMLMAPPER">
      <!-- enableSubPackages:是否让schema作为包的后缀 -->
      <property name="enableSubPackages" value="false" />
    </javaClientGenerator>

    <!-- 配置表信息 -->
    <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
        是否生成 example类 -->

    <!--       <table schema="newtest" tableName="category"
                  domainObjectName="Category" enableCountByExample="true"
                  enableDeleteByExample="true" enableSelectByExample="true"
                  enableUpdateByExample="true">
           </table>

           <table schema="newtest" tableName="product"
                  domainObjectName="Product" enableCountByExample="true"
                  enableDeleteByExample="true" enableSelectByExample="true"
                  enableUpdateByExample="true">
           </table>-->
    <!--生成对应表及类名-->
    <!-- table可以有多个,每个数据库中的表都可以写一个table,
    tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 -->
    <!--如果想生成一个表则tableName="table_name"-->
    <table tableName="%"
      enableCountByExample="true"
      enableUpdateByExample="true"
      enableDeleteByExample="true"
      enableSelectByExample="true"
      selectByExampleQueryId="true">
      <property name="useActualColumnNames" value="false" />
      <!-- 数据库表主键 -->
      <generatedKey column="id" sqlStatement="Mysql" identity="true" />
    </table>
  </context>
</generatorConfiguration>

    注意:(1) 需要配置 jdbc驱动信息,host,port,db库,user,password,还需要配置my            (2) 需要配置entity生成目录,mapper接口目录及resources/mapper 资源目录

6. 配置maven 启动参数     (0) 点击 edit configurations : 点击左上角 "+" 号,添加maven ,然后在右侧配置maven信息.     (1) 设置working directory: F:/Code/Altitude/datagaea01 #此处为你的pom.xml 目录     (2) 设置command line :mybatis-generator:generate -e -f pom.xml     (3) 设置profile : generator

7. 执行生成代码文件     点击执行程序,在idea 工程管理器 中生成相应的代码目录。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档