MyBatis Generator的简单使用方法示例

 更新时间:2021年02月12日 10:08:05   作者:转行当司机  
这篇文章主要给大家介绍了关于MyBatis Generator的简单使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

添加配置文件

在项目resource目录下创建mybatis-generator文件夹

创建文件夹

在文件夹下创建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="mybatis-generator/generator.properties"/>
 <!-- 连接数据库jar包的路径-->
 <!--<classPathEntry location="d:/java/JavaTools/mysql-connector-java-5.1.48/mysql-connector-java-5.1.48-bin.jar"/>-->
 <context id="DB2Tables" targetRuntime="MyBatis3">
  <commentGenerator>
   <property name="suppressDate" value="true"/>
   <!-- 是否去除自动生成的注释 true:是 : false:否 -->
   <property name="suppressAllComments" value="true"/>
  </commentGenerator>

  <!--数据库连接参数 -->
  <jdbcConnection
    driverClass="${driverClassName}"
    connectionURL="${url}"
    userId="${username}"
    password="${password}">
  </jdbcConnection>

  <javaTypeResolver>
   <property name="forceBigDecimals" value="false"/>
  </javaTypeResolver>

  <!-- 实体类的包名和存放路径 -->
  <javaModelGenerator targetPackage="com.shop.order.bean" targetProject="src/main/java">
   <property name="enableSubPackages" value="true"/>
   <property name="trimStrings" value="true"/>
  </javaModelGenerator>

  <!-- 生成映射文件*.xml的位置-->
  <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
   <property name="enableSubPackages" value="true"/>
  </sqlMapGenerator>

  <!-- 生成DAO的包名和位置 -->
  <javaClientGenerator type="XMLMAPPER" targetPackage="com.shop.order.mapper" targetProject="src/main/java">
   <property name="enableSubPackages" value="true"/>
  </javaClientGenerator>

  <!-- tableName:数据库中的表名或视图名;domainObjectName:生成的实体类的类名-->
  <table tableName="book" domainObjectName="Book"
    enableCountByExample="false"
    enableUpdateByExample="false"
    enableDeleteByExample="false"
    enableSelectByExample="false"
    selectByExampleQueryId="false"/>
  <!-- 可以添加多个需要生产代码的实体-->
  <!--
    <table tableName="xxx" domainObjectName="xxx"
      enableCountByExample="false"
      enableUpdateByExample="false"
      enableDeleteByExample="false"
      enableSelectByExample="false"
      selectByExampleQueryId="false"/>
    ...
    <table tableName="xxx" domainObjectName="xxx"
      enableCountByExample="false"
      enableUpdateByExample="false"
      enableDeleteByExample="false"
      enableSelectByExample="false"
      selectByExampleQueryId="false"/>
  -->
 </context>
</generatorConfiguration>

在文件夹下创建generator.properties配置文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/store?useUnicode=true&characterEncoding=UTF-8&relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull
username=root
password=root

配置Maven

pom.xml中引入依赖

<build>
    <plugins>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.5</version>
        <configuration>
          <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
        <executions>
          <execution>
            <id>Generate MyBatis Artifacts</id>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
          </dependency>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

执行Maven插件

双击运行mybatis-generator:generate

在这里插入图片描述

控制台输出结果,生产mapper和bean文件

在这里插入图片描述

总结

到此这篇关于MyBatis Generator简单使用方法的文章就介绍到这了,更多相关MyBatis Generator使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java超详细精讲数据结构之bfs与双端队列

    Java超详细精讲数据结构之bfs与双端队列

    广搜BFS的基本思想是: 首先访问初始点v并将其标志为已经访问。接着通过邻接关系将邻接点入队。然后每访问过一个顶点则出队。按照顺序,访问每一个顶点的所有未被访问过的顶点直到所有的顶点均被访问过。广度优先遍历类似与层次遍历
    2022-07-07
  • Java实现航空航班管理系统

    Java实现航空航班管理系统

    这篇文章主要为大家详细介绍了Java实现航空航班管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • 关于SpringBoot微服务发布与部署的三种方式

    关于SpringBoot微服务发布与部署的三种方式

    SpringBoot 框架只提供了一套基于可执行 jar 包(executable jar)格式的标准发布形式,但并没有对部署做过多的界定,而且为了简化可执行 jar 包的生成,SpringBoot 提供了相应的 Maven 项目插件,需要的朋友可以参考下
    2023-05-05
  • SpringBoot中的ApplicationRunner与CommandLineRunner问题

    SpringBoot中的ApplicationRunner与CommandLineRunner问题

    这篇文章主要介绍了SpringBoot中的ApplicationRunner与CommandLineRunner问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • Spring boot2X Consul如何使用Feign实现服务调用

    Spring boot2X Consul如何使用Feign实现服务调用

    这篇文章主要介绍了spring boot2X Consul如何使用Feign实现服务调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • Bean Searcher配合SpringBoot的使用详解

    Bean Searcher配合SpringBoot的使用详解

    这篇文章主要介绍了Bean Searcher配合SpringBoot的使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • Java中的运算符你知道多少

    Java中的运算符你知道多少

    这篇文章主要为大家详细介绍了Java中的运算符,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • cmd中javac和java使用及注意事项详解

    cmd中javac和java使用及注意事项详解

    这篇文章主要介绍了cmd中javac和java使用及注意事项详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • Spring BeanDefinition收集过程示例详解

    Spring BeanDefinition收集过程示例详解

    这篇文章主要为大家介绍了Spring BeanDefinition收集过程示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Java详细分析讲解HashMap

    Java详细分析讲解HashMap

    在java开发中,HashMap是最常用、最常见的集合容器类之一,下面一起温故一下,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06

最新评论