Mybatis automatically generates plug-ins

1. First, there must be an associated database and table. If not, you can create one yourself, and you can put the .sql file directly in the project:

2. After you have the database table, create a mybatis configuration file anywhere in the project (preferably at the same level as the project's properties file), and name it whatever you want.

Then modify the configuration:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration        PUBLIC "-// MyBatis Generator Configuration 1.0//EN"        ""><!-- 配置生成器 --><generatorConfiguration>    <!--执行generator插件生成文件的命令: call mvn mybatis-generator:generate -e -->    <!-- 引入配置文件 -->    <properties resource=""/>    <classPathEntry location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.25\mysql-connector-java-8.0.25.jar" />    <!-- 一个数据库一个context -->    <!--defaultModelType="flat" 大数据字段,不分表 -->    <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;        一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->        <property name="autoDelimitKeywords" value="true" />        <!-- 生成的Java文件的编码 -->        <property name="javaFileEncoding" value="utf-8" />        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->        <property name="beginningDelimiter" value="`" />        <property name="endingDelimiter" value="`" />         <!-- 格式化java代码 -->        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>        <!-- 格式化XML代码 -->        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />         <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />         <!-- 注释 -->        <commentGenerator >            <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->        </commentGenerator>         <!-- jdbc连接 -->        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}" />        <!-- 类型转换 -->        <javaTypeResolver>            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>         <!-- 生成实体类地址 -->        <javaModelGenerator targetPackage="" targetProject="src/main/java" >            <property name="enableSubPackages" value="false"/>            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <!-- 生成mapxml文件 -->        <sqlMapGenerator targetPackage="" targetProject="src/main/java" >            <property name="enableSubPackages" value="false" />        </sqlMapGenerator>        <!-- 生成mapxml对应client,也就是接口dao -->        <javaClientGenerator targetPackage="" targetProject="src/main/java" type="XMLMAPPER" >            <property name="enableSubPackages" value="false" />        </javaClientGenerator>        <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 -->        <table catalog="game" tableName="user" domainObjectName="User" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">            <property name="useActualColumnNames" value="true" />            <!-- 数据库表主键 -->            <generatedKey column="id" sqlStatement="Mysql" identity="true" />        </table>    </context></generatorConfiguration>

Note: The order of properties, classPathEntry and context in generatorConfiguration cannot be changed. Among them, properties is the configuration file that needs to be imported, which is the properties file of the project; classPathEntry is the connector path of mysql; the properties in the context that need to be modified are:

For the properties of jdbcConnection, you can directly reference the key value of the properties file with ${};

javaModelGenerator, sqMapGenrator and javaClientGenerator are the generated entity class, mapxml file, and the path address of the interface dao, which need to be modified;

The table name needs to be modified, and the property useActualColumnNames in the table is set to true to make the fields in the entity class consistent with the database column names.

After configuration, enter pom.xml to add dependencies as shown in the figure:

<build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>            <plugin>                <groupId>org.mybatis.generator</groupId>                <artifactId>mybatis-generator-maven-plugin</artifactId>                <version>1.3.2</version>                <configuration>                    <configurationFile>src/main/resources/mybatis-config.xml</configurationFile>                    <verbose>true</verbose>                    <overwrite>true</overwrite>                </configuration>            </plugin>        </plugins>    </build>

The path of the configurationFile is the file path of the mybatis configuration file.

Save after finishing, then click the MAVEN menu on the right side of IDEA, find the plug-in, and select run

After success, you will find that the corresponding mapper and bean have been generated!