Mybatis:Mybatais Generator reverse engineering from scratch

Mybatis Generator reverse engineering from scratch ...
Build environment
Maven configuration
Configuration reverse engineering
Overall configuration
Run Mybatis Generator
reference material
Mybatis Generator reverse engineering from scratch

Build environment

  • Idea 2021.1
  • Maven
  • Spring Boot
  • Mysql

Maven configuration

<dependencies> <!--Spring BottMybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!--Mybatis reverse engineering --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <!--Mybatis Reverse engineering plug-in--> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--Specifies the configuration file for code generation. Optional, default generatorConfig.xml--> <!--<configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>--> <verbose>true</verbose> <!--Allow overwriting of generated files--> <overwrite>true</overwrite> </configuration> <!--Import here mysql After package,Reverse engineering does not require configuration <classPathEntry> Yes--> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> </dependencies> </plugin> </plugins> </build>

So far, we have completed the reverse engineering Maven configuration of Mybatis Generator

Configuration reverse engineering XML

First, create the generatorConfig.xml file in the src/resources folder

1 add header

<?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> <!-- mybatis Reverse configuration--> </generatorConfiguration>

1.2 external configuration files

1.2.1 create configuration

Create datasource.properties under src/resources folder

# datasource.properties #mysql db.driverClassName=com.mysql.cj.jdbc.Driver db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai db.username=root db.password=123456 # mysql jar package path db.driverLocation=...
1.2.2 introduction configuration
<!-- src/resource/generatorConfig.xml --> <properties resource="datasource.properties"/>

1.3 specify the jdbc driver jar package for the database classPathEntry

<!-- src/resource/generatorConfig.xml --> <!--Specifies the of a specific database jdbc drive jar Package location--> <!--If already in Maven Middle introduction mysql No configuration is required--> <classPathEntry location="$"/>

1.4 preparation context

The < context > tag is used to set the environment for generating a group of objects

<!-- src/resource/generatorConfig.xml --> <!-- id: Identifier, used to prompt when an error occurs targetRuntime: 1,MyBatis3: Default value, generated based on MyBatis3.x The contents of the above version include XXXBySample; 2,MyBatis3Simple: similar MyBatis3,Just don't generate XXXBySample; --> <context id="default" targetRuntime="MyBatis3"> </context>
1.4.1 control notes commentGenerator

This tag is mainly used to control the comments generated after generating java classes and mapper.xml files

<!-- src/resource/generatorConfig.xml --> <!--The following are optional--> <commentGenerator> <!--No timestamp is generated in the comment--> <property name="suppressDate" value="true"/> <!-- add to db Comments for fields in the table --> <property name="addRemarkComments" value="true"/> <!--Do not generate comments--> <property name="suppressAllComments" value="true"/> </commentGenerator>
1.4.2 configuring jdbc database connection jdbcConnection

This label is mandatory and is mainly used to connect to the database

<!-- src/resource/generatorConfig.xml --> <jdbcConnection>driverClass="$" <!--Attention here &Symbol to use &amp; replace,Otherwise it cannot be used--> connectionURL="$" userId="$" password="$"> <!--Higher version mysql-connector-java Need to set nullCatalogMeansCurrent=true--> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection>

Use a higher version of MysqlnullCatalogMeansCurrent matters needing attention

1.4.3 processor type javaTypeResolver

This tag is optional and is mainly used to configure the type conversion between JDBC and java. It only supports the conversion of BigDecimal type and time type

<!-- src/resource/generatorConfig.xml --> <javaTypeResolver> <!--Default to false false: hold JDBC in DECIMAL and NUMERIC Type resolves to Integer true: Same as above, but resolved as java.math.BigDecimal--> <property name="forceBigDecimals" value="true"/> <!--Default to false false: take JDBC The time type in resolves to java.util.Date true:take JDBC The time type of is resolved according to the following rules DATE -> java.time.LocalDate TIME -> java.time.LocalTime TIMESTAMP -> java.time.LocalDateTime TIME_WITH_TIMEZONE -> java.time.OffsetTime TIMESTAMP_WITH_TIMEZONE -> java.time.OffsetDateTime --> <property name="useJSR310Types" value="true"/> </javaTypeResolver>
1.4.4 Java class model generator javaModelGenerator

This label is required to generate a class with a primary key

<!-- src/resource/generatorConfig.xml --> <!--targetPackage Specify generated model Package name where the build is located,Generally pojo package targetProject Specify the path under the project --> <javaModelGenerator targetPackage="com.ayou.pojo" targetProject="./src/main/java"> <!-- Whether to allow sub packages, i.e targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- Right model Add constructor --> <property name="constructorBased" value="true"/> <!-- Class CHAR Type of column trim operation --> <property name="trimStrings" value="true"/> <!-- Established Model Is the object immutable or generated Model Object will not have setter Method, only construction method --> <property name="immutable" value="false"/> </javaModelGenerator>
1.4.5 SQL map XML file generator sqlMapGenerator

This label is required to configure the directory generated by Mapper.xml file

<!-- src/resource/generatorConfig.xml --> <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator>
1.4.6 Java interface Mapper class generator javaClientGenerator

This tag is mandatory and is mainly used to generate interface codes for Modal objects and Mapper.xml configuration files

<!-- src/resource/generatorConfig.xml --> <!-- type="ANNOTATEDMAPPER",generate Java Model And annotation based Mapper object type="MIXEDMAPPER",Generate annotation based Java Model And corresponding Mapper object type="XMLMAPPER",generate SQLMap XML Documentation and independent Mapper Interface --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.ayou.dao" targetProject="./src/main/java"> <!-- enableSubPackages:Whether to let schema As the suffix of the package,Another layer is created package,The generated classes are placed here package lower--> <property name="enableSubPackages" value="false" /> </javaClientGenerator>
Table 1.4.7 table

This tab is used to generate the corresponding Modal object and Mapper object according to the table. One table corresponds to one table. If you want to generate multiple tables, you need to configure multiple tables

<!-- src/resource/generatorConfig.xml --> <!-- schema Is the database name, oracle Configuration required, mysql No configuration is required. tableName Is the corresponding database table name domainObjectName Is the name of the entity class to be generated(Can not be specified) enableXXXByExample Default to true,by true A corresponding is generated Example Help class to help you query conditions. If you don't want it, it can be set as false --> <table tableName="" domainObjectName="" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!--Use actual column name,Default to false--> <!--<property name="useActualColumnNames" value="false" />--> </table>

When domainObjectName is not configured, it will convert the table name into class name according to Pascal naming method (hump)

enableXXXByExample defaults to true and only takes effect when targetRuntime="MyBatis3"

When targetRuntime="Mybatis3Simple",enableXXXByExample does not take effect whether it is true or false

Overall configuration

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> <!--Import attribute configuration--> <properties resource="datasource.properties"/> <!--Specifies the of a specific database jdbc drive jar Package location--> <!--If already in Maven Middle introduction mysql No configuration is required--> <!-- <classPathEntry location="$"/> --> <context id="default" targetRuntime="MyBatis3"> <!--Control comments --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc Database connection for --> <jdbcConnection--> driverClass="$" connectionURL="$" userId="$" password="$" </jdbcConnection> <!-- Not required, type processor, in database type and java Conversion control between types--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Model Model generator,Used to generate primary keys key Class, record class and query Example class targetPackage Specify generated model Package name where the build is located targetProject Specify the path under the project --> <javaModelGenerator targetPackage="route" targetProject="./src/main/java"> <!-- Whether to allow sub packages, i.e targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- Right model Add constructor --> <property name="constructorBased" value="true"/> <!-- Class CHAR Type of column trim operation --> <property name="trimStrings" value="true"/> <!-- Established Model Is the object immutable or generated Model Object will not have setter Method, only construction method --> <property name="immutable" value="false"/> </javaModelGenerator> <!--mapper The directory where the mapping file is generated generates the corresponding mapping file for each database table SqlMap file --> <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- targetPackage: mapper Interface dao Generated location --> <javaClientGenerator type="XMLMAPPER" targetPackage="route" targetProject="./src/main/java"> <!-- enableSubPackages:Whether to let schema As the suffix of the package --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> </context> </generatorConfiguration>

datasource.properties

# datasource.properties #mysql db.driverClassName=com.mysql.cj.jdbc.Driver db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai db.username=root db.password=123456 # mysql jar package path db.driverLocation=...

pom.xml (Maven)

<dependencies> <!--Mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!--Spring Boot Mybatis reverse engineering --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--Specifies the configuration file for code generation. Optional, default generatorConfig.xml--> <!-- <configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile> --> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <!--to configure mysql dependency after generatorConfig.xml No configuration required <classPathEntry> --> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> </dependencies> </plugin> </plugins> </build>

Run Mybatis Generator

Method 1

Using the Maven tool provided with Idea, double-click mybatis generator: generate to run (please ensure that the Maven project has been reloaded)

Method 2

Use the Maven command to open the DOS console in the root directory

mvn mybatis-generator:generate


The following content indicates that the construction is successful. Now our project has relevant Modal model classes and Mapper.xml

reference material

29 November 2021, 09:55 | Views: 3720

Add new comment

For adding a comment, please log in
or create account

0 comments