mybatis-generator代码生成器使用
一、什么是代码生成器
代码生成器指:通过 工具可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
二、mybatis-generator的使用
官网:http://mybatis.org/generator/index.html
MyBatis生成器(MBG)是MyBatis的代码生成器。它将为MyBatis的所有版本生成代码。它将内省一个数据库表(或多个表),并将生成可用于访问表的工件。这减轻了设置对象和配置文件以与数据库表进行交互的麻烦。MBG试图对简单CRUD(创建,检索,更新,删除)的大部分数据库操作产生重大影响。您仍将需要手工编写SQL和对象代码以进行联接查询或存储过程。
MBG会根据其配置方式以不同的样式和不同的语言生成代码。例如,MBG可以生成Java或Kotlin代码。MBG可以生成MyBatis3兼容的XML-尽管现在认为MBG是旧版使用。生成的代码的较新样式不需要XML。 前提条件: MBG除了JRE之外没有其他依赖项。需要Java 8或更高版本。另外,还需要一个实现DatabaseMetaData接口的JDBC驱动程序,尤其是getColumns和 getPrimaryKeys方法。
引入依赖:修改pom.xml,添加mybatis-generator-maven-plugin插件
<plugin>
<groupId>org.mybatis.generator
</groupId>
<artifactId>mybatis-generator-maven-plugin
</artifactId>
<version>1.3.5
</version>
<configuration>
<configurationFile>src/main/resources/generator-config.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>
</dependencies>
</plugin>
配置生成代码的配置文件generator-config.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>
<classPathEntry location="E:\MYSQL\jar\mysql-connector-java-8.0.15.jar"/>
<context id="mysql" defaultModelType="flat" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="false"/>
<property name="javaFileEncoding" value="UTF-8"/>
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8"
userId="root"
password="password">
</jdbcConnection>
<javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.flyfly.springbootmvc.pojo" targetProject="src/main/java">
<property name="constructorBased" value="false"/>
<property name="enableSubPackages" value="true"/>
<property name="immutable" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.flyfly.springbootmvc.mapper" type="XMLMAPPER"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="users" domainObjectName="Users" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
执行插件
执行成功 成功生成 如果要同时生成多个表的pojo及mapper则在配置文件generator-config.xml配置多个表即可
<table tableName="users" domainObjectName="Users" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
<table tableName="student" domainObjectName="Student" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>