IT@程序员猿媛

Mybatis自动生成实体类和Mapper文件

2019-04-28  本文已影响26人  X兄

Mybatis自动生成实体类和Mapper文件
我们在做项目时,少不了用到Mybatis框架,更少不了创建实体类和书写sql语句,当数据库表很多时,手动创建会耗时耗力,做着大量重复性的工作,很让人头疼,下面我将介绍如何配置利用Mybatis插件自动创建生成。

  1. 在pom.xml中添加如下依赖:
<!--mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.11</version>
    </dependency>
    <!--mybatis-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>
<build>
    <finalName>springboot-mybatis</finalName>
  <plugins>
<!--设置生成实体类和mapper的插件-->
        <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.2</version>
          <configuration>
            <verbose>true</verbose><!--允许移动生成文件-->
            <overwrite>true</overwrite><!--允许自动覆盖文件-->
            <tableNames>tableName</tableNames> <!--你要生成的表名-->
          </configuration>
          <dependencies>
            <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>8.0.11</version>
            </dependency>
          </dependencies>
        </plugin>
  </plugins>
</build>
  1. 在resources目录下创建generatorConfig.properties文件,并添加如下的配置:
#生成实体类的所需数据连接参数
driverClass= com.mysql.cj.jdbc.Driver
connectionURL=jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
username=root
password=123456
# 生成的实体类和包的路径
modelPackage=com.demo.tools.domain
daoPackage=com.demo.tools.persistence
  1. 在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="generatorConfig.properties"/>

    <context id="mysqlTables" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>

        <commentGenerator>
            <property name="type" value="DEFAULT"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
            <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
            <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}"
                        userId="${username}"
                        password="${password}">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="${modelPackage}" targetProject="MAVEN">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成DAO的包名和位置-->
        <sqlMapGenerator targetPackage="${sqlMapPackage}" targetProject="MAVEN">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="${daoPackage}" targetProject="MAVEN">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <table tableName="tableName" domainObjectName="TableName" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

注意在上面的"<table tableName='tableName'"....>把你要生成的表名换过去,比如你要生成person表,你就这样写:

 <table tableName="person" domainObjectName="Person" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
  1. 在IDEA内置的Terminal中输入命令:mvn clean mybatis-generator:generate
    如图,即可成功:


    success

然后到你的target目录下,找到你要生成的文件,复制到你的项目里就好了!!!

image.png
上一篇 下一篇

猜你喜欢

热点阅读