Reverse Engineering of mybatis and reverse engineering of mybatis
To facilitate development, mybatis provides reverse engineering, that is, our programmers do not need to write mapper interfaces and mapper. xml files. These tasks can be completed by mybatis tools.
Development steps:
1) Import mybatis reverse engineering jar package mybatis-generator-core-1.3.2.jar (go to the official website download) + mybatis jar package (core package + dependency package)
2) In order to use reverse engineering, we need to define the database first, because reverse engineering is generated based on the database table!
Here, I use SQL _table. SQL and SQL _data. SQL to import data to the created mybatis database (create database mybatis created in mysql). The import method is: source e:/SQL _table. SQL; source e:/SQL _data. SQL; for import.
3) Open the html homepage of the reverse engineering jar package and follow the steps below:
Then click in to copy and paste the configuration file information to a new generatorCongig. xml file in the java project (a few modifications are made here)
<? 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> <context id =" testTables "targetRuntime =" MyBatis3 "> <! -- Database connection information driver connection address username and password --> <jdbcConnection driverClass = "com. mysql. jdbc. driver "connectionURL =" jdbc: mysql: // localhost: 3306/mybatis "userId =" root "password =" 169500 "> </jdbcConnection> <javaTypeResolver> <property name =" forceBigDecimals "value =" false "/> </javaTypeResolver> <! -- Generate the position of the po class --> <javaModelGenerator targetPackage = "cn. itcast. ssm. po "targetProject = ". \ src "> <property name =" enableSubPackages "value =" true "/> <property name =" trimStrings "value =" true "/> </javaModelGenerator> <! -- Er ing file generation location --> <sqlMapGenerator targetPackage = "cn. itcast. ssm. mapper "targetProject = ". \ src "> <property name =" enableSubPackages "value =" true "/> </sqlMapGenerator> <! -- Mapper Interface Generation location --> <javaClientGenerator type = "XMLMAPPER" targetPackage = "cn. itcast. ssm. mapper "targetProject = ". \ src "> <property name =" enableSubPackages "value =" true "/> </javaClientGenerator> <! -- Specify a database table --> <table tableName = "items"> </table> <table tableName = "orders"> </table> <table tableName = "orderdetail"> </ table> <table tableName = "user"> </table> </context> </generatorConfiguration>
4) Find the core code (mapper. xml and mapper interfaces can be generated)
Copy the code (as follows). I wrote a main method to run
package cn.itcast.generator;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.mybatis.generator.api.MyBatisGenerator;import org.mybatis.generator.config.Configuration;import org.mybatis.generator.config.xml.ConfigurationParser;import org.mybatis.generator.internal.DefaultShellCallback;public class GeneratorSqlMap {public static void main(String[] args) {GeneratorSqlMap generatorSqlMap=new GeneratorSqlMap();try {generatorSqlMap.generator();} catch (Exception e) {e.printStackTrace();}}private void generator() throws Exception {List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("generatorConfig.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);}}
5) run the code above and you can see that there are two more packages in the project directory:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.