1. Configure mybatis-generator-Maven-plugin in POM. xml
<build> <plugins> <!-- mybatis-generator --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.1</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins></build>
2. generatorconfig. XML, which is placed under src/main/resource. You need to set ① database driver path, ② JDBC connection information, ③ package name and storage path of entity, XML, and Dao files (if it is set to a non-project path, copy it to the corresponding path under the project after generation), ④ name of the table to be generated (entity).
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype generatorconfiguration public "-// mybatis.org//dtd mybatis Generator configuration 1.0 //" http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd "> <generatorconfiguration> <! -- Classpathentry: Database JDBC driver --> <classpathentry location = "/users/nnxiaod/downloads/mysql-connector-java-5.1.21.jar"/> <context ID = "mysqltables" targetruntime = "mybatis3"> <! -- Note that the order here is determined and cannot be changed with the change --> <! -- Custom page plug-in <plugin type = "com. deppon. Foss. Module. helloworld. Shared. paginationplugin"/> --> <! -- Optional (0 or 1) --> <! -- Comment generator --> <commentgenerator> <! -- Whether to remove automatically generated comments true: Yes: false: No --> <property name = "suppressallcomments" value = "true"/> </commentgenerator> <! -- Required (1 Required) --> <! -- Database connection information: Driver Class, connection address, user name, password --> <jdbcconnection driverclass = "com. mySQL. JDBC. driver "connectionurl =" JDBC: mysql: // localhost: 3306/blog? Generatesimpleparametermetadata = true "userid =" root "Password =" 123456 "> </jdbcconnection> <! -- Optional (0 or 1) --> <! -- Type converter or add type parser --> <! -- The default value is false. The JDBC decimal and numeric types are parsed to integer true, and the JDBC decimal and numeric types are parsed to Java. math. bigdecimal --> <javatyperesolver> <property name = "forcebigdecimals" value = "false"/> </javatyperesolver> <! -- Required (1 Required) --> <! -- Java model generator --> <! -- Targetproject: the location where the code is automatically generated --> <javamodelgenerator targetpackage = "com. liufei. blog. model "targetproject ="/users/nnxiaod/documents/workspace/blog/src/main/Java "> <! -- Todo enablesubpackages: whether to use schema as the package suffix --> <property name = "enablesubpackages" value = "true"/> <! -- Space before and after the value returned from the database --> <property name = "trimstrings" value = "true"/> </javamodelgenerator> <! -- Required (1 Required) --> <! -- Map XML Builder --> <sqlmapgenerator targetpackage = "com. liufei. blog. persistence "targetproject ="/users/nnxiaod/documents/workspace/blog/src/main/Java "> <property name =" enablesubpackages "value =" true "/> </sqlmapgenerator> <! -- Optional (0 or 1) --> <! -- Mapper or Dao interface Builder --> <javaclientgenerator targetpackage = "com. liufei. blog. dao "targetproject ="/users/nnxiaod/documents/workspace/blog/src/main/Java "type =" xmlmapper "> <property name =" enablesubpackages "value =" true" /> </javaclientgenerator> <! -- Required (1... n) --> <! -- Pojo entity generator --> <! -- Tablename: database table used to automatically generate code; domainobjectname: JavaBean class name corresponding to the database table --> <! -- Schema indicates that the database name cannot be written. --> <Table schema = "data" tablename = "user" domainobjectname = "user" enableinsert = "true"> <! -- Ignore optional fields (0 or 1) --> <! -- <Ignorecolumn column = "is_use"/> --> <! -- // Whatever the field type, the generated class attributes are varchar. The optional (0 or 1) test is invalid --> <! -- <Columnoverride column = "city_code" jdbctype = "varchar"/> --> </table> </context> </generatorconfiguration>
3. Right-click "Run as", "Maven build...", and enter "mybatis-generator: Generate, run, OK" in "goals!
Mybatis-generator-meven-plugin