1. Introduce and configure mybatis-generator in pxm.xml
Add the org.mybatis.generator sample code under plugins under the build node as follows:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dcrenl.test</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <name>test</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <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> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <!--加入下面这个依赖--> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
Second, create a new mybatis-generator configuration xml file under the resources folder, as follows:
<? 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 " > <!- Configuration generator- > < generatorConfiguration > <!- classPathEntry: JDBC driver of the database, replace it with your own drive location optional- > <!- <classPathEntry location = "C: \ Users \ dcrenl \ .m2 \ repository \ mysql \ mysql-connector-java \ 8.0.19 \ mysql-connector-java-8.0.19.jar" /> -> <!- One database, one context, defaultModelType = "flat" large data field, regardless of table- > <context id="MysqlTables"targetRuntime = "MyBatis3" defaultModelType = "flat" > <!- Automatically identify database keywords, default false, if set to true, according to the keyword list defined in SqlReservedWords; generally keep the default values, encountered database keywords (Java Keywords), use columnOverride to override- > < property name = "autoDelimitKeywords" value = "true" /> <!- Encoding of the generated Java file- > < property name = "javaFileEncoding" value = "utf-8 " /> <!- beginningDelimiter and endingDelimiter: indicate the symbol used to mark the database object name in the database, such as ORACLE is double quotes, MYSQL defaults to` backquotes; -> < 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" /> <-! comment -> < commentGenerator > < Property name = "suppressAllComments" value = "to true" /> <-! Are uncommented -> < property name = "suppressDate" value = "false" /> <!- Whether to generate comment generation timestamp- > </ commentGenerator > <!- jdbc connection- > < jdbcConnection driverClass = "com.mysql.cj. jdbc.Driver " connectionURL= "jdbc: mysql: // localhost: 3306 / test? serverTimezone = UTC & amp; setUnicode = true & amp; characterEncoding = utf8" userId = "root" password = "root" > <!- Prevent the generation of tables from other libraries- > < property name = "nullCatalogMeansCurrent" value = "true" /> </ jdbcConnection > <!- type conversion- > < javaTypeResolver > <!- whether to use bigDecimal, false can automatically convert the following types (Long, Integer, Short , etc.) -> < property name = "forceBigDecimals" value="false"/> </ JavaTypeResolver > <-! Generating entity classes address -> < javaModelGenerator targetPackage = "com.dcrenl.examinersystem.entity" targetProject = "the src / main / Java" > <-! Whether to allow schema package as a suffix - -> < property name = "enableSubPackages" value = "false" /> <!- Remove the space before and after the value returned from the database- > < property name = "trimStrings" value = "true" /> </ javaModelGenerator > < ! -Generate map.xml file storage address- > <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成接口dao(mapper) --> <javaClientGenerator targetPackage="com.dcrenl.examinersystem.mapper" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> <property name= "nullCatalogMeansCurrent" value = "true" /> </ javaClientGenerator > <!- There can be multiple tables, each table in the database can write a table, tableName indicates the database table to match, or in the tableName property By using the% wildcard to match all database tables, only the matching table will automatically generate the file enableSelectByPrimaryKey The corresponding configuration indicates whether to generate the corresponding interface- > < table tableName = "t_%" enableCountByExample = "false" enableUpdateByExample = "false " enableDeleteByExample =" false " enableSelectByExample =" false " selectByExampleQueryId =" false " enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" > <property name="useActualColumnNames" value="false"/> </table> </context> </generatorConfiguration>
3. Click the "Maven" column on the right side of IDEA, find "mybatis-generator" in "Plugins", expand to find "mybatis-generator: generate", right click, "Run Maven Build" generates