This article brings you an introduction to mybatis reverse engineering (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
First create a test class:
Write the following code in the main method:
List <String> warnings = new ArrayList <String>(); boolean overwrite = true; File configFile = new File("generator.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback,warnings); myBatisGenerator.generate(null);
Create a new generator.xml file in the project directory:
<?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> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true"></property> </commentGenerator> <!-- 设定数据库连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="123"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成 bean 存放的位置 --> <javaModelGenerator targetPackage="com.ujiuye.bean" targetProject=".\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的mapper文件的位置 --> <sqlMapGenerator targetPackage="com.ujiuye.mapper" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成的mapper.xml 对应的那个mapper接口的存放位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.ujiuye.mapper" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 设定反向生成的表 --> <table tableName="Person"></table> <table tableName="car"></table> <table tableName="card"></table> </context> </generatorConfiguration>
The last table tag is the name of the table in your own database; the connection information of the database needs to be modified by yourself
Executing the test class will automatically generate the data in the table tag set above The corresponding entity classes, dao layer interfaces and corresponding mapper mappings in the table
In addition, please download the required jar package yourself: https:// github.com/Pei-Qi/mybatis_jar
The above is the detailed content of Introduction to mybatis reverse engineering (code example). For more information, please follow other related articles on the PHP Chinese website!