Mybatis BASICS (9) ---- reverse engineering, mybatis Reverse Engineering

Source: Internet
Author: User

Mybatis BASICS (9) ---- reverse engineering, mybatis Reverse Engineering
1. What is reverse engineering?

Mybaits requires programmers to write their own SQL statements. mybatis provides official reverse engineering to automatically generate the Code required for mybatis execution for a single table (mapper. java, mapper. xml, po ..)

In actual development, the commonly used reverse engineering method is to generate java code from the database table.

Ii. Download Reverse Engineering

Go to the official website to download reverse engineering. The latest version is 1.3.3. Official reverse engineering document

I use Version 1.3.2: jarpack.zip used by mybatis_generator.

Mybatis (((((.........rar

Iii. Usage

1. Generate the code configuration file

GeneratorConfig. xml configuration

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE generatorConfiguration 3 PUBLIC "-// mybatis.org//DTD MyBatis Generator Configuration 1.0 // EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <context id = "testTables" targetRuntime = "MyBatis3"> 8 <commentGenerator> 9 <! -- Whether to remove automatically generated comments true: Yes: false: No --> 10 <property name = "suppressAllComments" value = "true"/> 11 </commentGenerator> 12 <! -- Database connection information: Driver Class, connection address, user name, password. mysql is configured here. Of course, oracle and other databases can be configured --> 13 <jdbcConnection driverClass = "com. mysql. jdbc. driver "14 connectionURL =" jdbc: mysql: // localhost: 3306/mybatis "userId =" root "15 password =" root "> 16 </jdbcConnection> 17 18 <! -- The default value is false. The jdbc decimal and NUMERIC types are parsed to Integer. If the value is true, the jdbc decimal 19 and NUMERIC types are parsed to java. math. bigDecimal --> 20 <javaTypeResolver> 21 <property name = "forceBigDecimals" value = "false"/> 22 </javaTypeResolver> 23 24 <! -- TargetProject: location where the PO class is generated --> 25 <javaModelGenerator targetPackage = "com. mybatis. entity" 26 targetProject = ". \ src"> 27 <! -- EnableSubPackages: whether to use schema as the package suffix --> 28 <property name = "enableSubPackages" value = "false"/> 29 <! -- Space before and after the value returned from the database is cleared --> 30 <property name = "trimStrings" value = "true"/> 31 </javaModelGenerator> 32 <! -- TargetProject: location where the mapper ing file is generated --> 33 <sqlMapGenerator targetPackage = "com. mybatis. mapper" 34 targetProject = ". \ src"> 35 <! -- EnableSubPackages: whether to use schema as the package suffix --> 36 <property name = "enableSubPackages" value = "false"/> 37 </sqlMapGenerator> 38 <! -- TargetPackage: Location generated by the ER interface --> 39 <javaClientGenerator type = "XMLMAPPER" 40 targetPackage = "com. mybatis. mapper" targetProject = ". \ src"> 41 <! -- EnableSubPackages: whether to use schema as the package suffix --> 42 <property name = "enableSubPackages" value = "false"/> 43 </javaClientGenerator> 44 <! -- Specify database table --> 45 <table tableName = "items"> </table> 46 <table tableName = "orders"> </table> 47 <table tableName = "orderdetail"> </table> 48 <table tableName = "t_user"> </table> 49 </context> 50 </generatorConfiguration>

2. Execute the generated program

GeneratorSqlmap. java code:

1 import java. io. file; 2 import java. util. arrayList; 3 import java. util. list; 4 5 import org. mybatis. generator. api. myBatisGenerator; 6 import org. mybatis. generator. config. configuration; 7 import org. mybatis. generator. config. xml. configurationParser; 8 import org. mybatis. generator. internal. defaultShellCallback; 9 10 public class GeneratorSqlmap {11 12 public void generator () throws Exception {13 14 List <String> warnings = new ArrayList <String> (); 15 boolean overwrite = true; 16 // specify the reverse engineering configuration File 17 File configFile = new File ("generatorConfig. xml "); 18 ConfigurationParser cp = new ConfigurationParser (warnings); 19 Configuration config = cp. parseConfiguration (configFile); 20 DefaultShellCallback callback = new DefaultShellCallback (overwrite); 21 MyBatisGenerator myBatisGenerator = new MyBatisGenerator (config, 22 callback, warnings); 23 myBatisGenerator. generate (null); 24 25} 26 public static void main (String [] args) throws Exception {27 try {28 GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap (); 29 generatorSqlmap. generator (); 30} catch (Exception e) {31 e. printStackTrace (); 32} 33 34} 35 36}

The code above is in the official document, which supports many languages. You can go to the official website to see it. The following figure is taken from the official website.

After the configuration, run the Code directly to generate the corresponding object, er, and mapper. xml files in the corresponding directory of the configuration. (Similar to the entity ing of hibernate), you can also download the mybatis_generator plug-in to generate this method.

After the execution is complete, refresh the project to view the generated file.

3. Considerations for Reverse Engineering

If the mapper. xml file already exists and is regenerated, the content of the mapper. xml file will not be overwritten, but will be increased. As a result, mybatis parsing will fail.

Solution: Delete the generated er. xml file and regenerate it.

The entity and mapper. java files automatically generated by mybais are not directly overwritten rather than content appending.

4. project directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.