first, the new test project
Create a new MAVEN project MybatisDemo2, modify Pom.xml to introduce dependencies. Dependencies added on the previous section
<Dependency> <groupId>Org.mybatis.generator</groupId> <Artifactid>Mybatis-generator-core</Artifactid> <version>1.3.2</version></Dependency>
Build Add Plugin
<Build> <Plugins> <plugin> <groupId>Org.mybatis.generator</groupId> <Artifactid>Mybatis-generator-maven-plugin</Artifactid> <version>1.3.5</version> <Configuration> <ConfigurationFile>${basedir}/src/main/resources/config/mybatis-generator-config.xml</ConfigurationFile> <verbose>True</verbose> <Overwrite>True</Overwrite> </Configuration> <Dependencies> <Dependency> <groupId>Mysql</groupId> <Artifactid>Mysql-connector-java</Artifactid> <version>5.1.21</version> </Dependency> </Dependencies> </plugin> </Plugins></Build>
ConfigurationFile node specifies the path to the MyBatis generator configuration file
Second, add MBG configuration file
Running MBG relies heavily on a profile, which is the key to learning MBG. Official detailed configuration instructions: http://www.mybatis.org/generator/configreference/xmlconfig.html
We configured the directory ${basedir}/src/main/resources/mybatis-generator-config.xml for the mybatis generator configuration file in the previous step, and now create the XML file in the corresponding directory. The contents are as follows:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE generatorconfigurationpublic "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "Http://myba Tis.org/dtd/mybatis-generator-config_1_0.dtd "><generatorconfiguration><!--the Help documentation for the MyBatis build tool can be seen: - <!--English: http://www.mybatis.org/generator/usage/mysql.html - <!--English: http://www.mybatis.tk/ - <!--English: http://mbg.cndocs.tk/ -<ContextID= "Context"Targetruntime= "MyBatis3"><!--set the encoding format -< Propertyname= "Javafileencoding"value= "UTF-8"/> <jdbcconnectionDriverclass= "Com.mysql.jdbc.Driver"Connectionurl= "Jdbc:mysql://localhost:3306/store"userId= "root"Password= "root"/><!--the domain module location to be generated -<JavamodelgeneratorTargetpackage= "Com.data.pojo"Targetproject= "Src/main/java"> < Propertyname= "Enablesubpackages"value= "true"/> < Propertyname= "Trimstrings"value= "true"/> < Propertyname= "Comments"value= "true"/> </Javamodelgenerator><!--the location of the Mapper.xml file to generate -<SqlmapgeneratorTargetpackage= "Mapper"Targetproject= "Src/main/resources"> < Propertyname= "Enablesubpackages"value= "true"/> </Sqlmapgenerator><!--the Mapper interface class to generate -<JavaclientgeneratorTargetpackage= "Com.data.mapper"Targetproject= "Src/main/java"type= "Xmlmapper"> < Propertyname= "Enablesubpackages"value= "false"/> </Javaclientgenerator> <TableTableName= "category"Enablecountbyexample= "true"Enabledeletebyexample= "true"Enableselectbyexample= "true"Enableupdatebyexample= "true"> <Generatedkeycolumn= "Id"SQLStatement= "MySQL"Identity= "true"/> </Table> </Context></generatorconfiguration>
Several of these key nodes:
1.jdbcConnection: Each context must have a jdbcconnection node that specifies the database connection properties required for the database connection.
2. Javamodelgenerator: Used to define the settings that generate the model class.
3.sqlMapGenerator: Used to define the properties of the SQL map generator
4. Javaclientgenerator: Used to define the properties of the Java client code generator. The Java Client Builder generates Java interfaces and classes that can simply use the generated Java model and XML mapping files.
5.table: Used to select a table from the database.
third, run the plug-in
The IDE I'm using is IntelliJ idea, and you can double-click Mybatis-generator:gennerate on the MAVEN panel to automatically generate MyBatis related files.
After execution, the effect is as follows:
Getting Started with Java [7]-mybatis Generator (MBG) automatically generate Mybatis code