Recently I am learning Mybatis, similar to Hibernate, Mybatis also has inverse engineering that can directly generate code (mapping, xml, pojo) for easy and rapid development. I use the mybatis-generator-core-1.3.2.jar package. Here I am using mysql database.
1. Download mybatis-generator-core-1.3.2.jar and mysql-connector-java-5.1.13-bin.jar. You can download http://maven.outofmemory.cn/org.mybatis.generator/mybatis-generator-core/1.3.2/
2. Create a new folder, move the mybatis-generator-core-1.3.2.jar and mysql-connector-java-5.1.13-bin.jar downloaded in step 1 to the folder, and create a new src folder in the root directory of the folder.
3. Create a new txt text document in the root directory of the folder and write the code:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
Then change the filename suffix of the txt text document to bat.
4. Create a new generatorConfig.xml and configure the reverse engineering information in it as follows:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><classPathEntry location="mysql-connector-java-5.1.13-bin.jar"/><context id="DB2Tables" targetRuntime="MyBatis3"><commentGenerator><property name="suppressDate" value="true"/><property name="suppressAllComments" value="true"/></commentGenerator><!-- Configure database connection--><jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/login" userId="root" password="root"></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false"/></javaTypeResolver><!-- Configure the generated pojo entity class--> <javaModelGenerator targetPackage="tse.model" targetProject="src"><property name="enableSubPackages" value="true"/><property name="trimStrings" value="true"/></javaModelGenerator><!-- Configure the generated xml --><sqlMapGenerator targetPackage="tse.mapping" targetProject="src"><property name="enableSubPackages" value="true"/></sqlMapGenerator><!-- Configure the generated mapping interface--><javaClientGenerator type="XMLMAPPER" targetPackage="tse.mapping" targetProject="src"><property name="enableSubPackages" value="true"/></javaClientGenerator><!-- Configure the table for reverse engineering, tableName can match all tables with wildcard %--><table tableName="login" domainObjectName="Login" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table></context></generatorConfiguration>
Remember to modify the configuration of the database connection of the jdbcConnection tag and the tableName property of the table tag. If all tables in your database want to reverse engineering, you can directly set the tableName value to % to match all tables. However, the domainObjectName property must be removed at this time.
OK, through the above steps, the entire directory structure should be like this
The src folder is still an empty folder
At this time, run the bat file in the root directory, and you can see that the code you want has been generated in the src directory.
The above is the relevant knowledge of the use of Mybatis inverse engineering introduced to you by the editor. It mainly introduces the use of inverse engineering. At this time, a friend will ask, how can I make the inverse engineering generate the code format I defined by myself? Don't worry, I will talk about the modification and packaging of mybatis-generator-core-1.3.2.jar package in the next article. Interested friends continue to follow this site!