MyBatis Study--Reverse engineering

Source: Internet
Author: User

    • Brief introduction

  Mybaits requires programmers to write their own SQL statements,MyBatis The official reverse engineering can be automatically generated for a single table mybatis Execute the required code ( Mapper.java,mapper.xml , po.. ). in the enterprise actual development, the commonly used reverse engineering way: Because the database table generates the Java code. Hibernate also has reverse engineering, which can also reduce the number of programmer configuration things, only need to be in the production of reverse engineering files to check whether the development requirements can be met.

MyBatis website offers a variety of reverse engineering tools, such as: the use of ECLIPCE plug-ins, the use of Java programs and so on. Recommended way to use Java programs.

    • Java Program mode generates reverse engineering

 1.mapper Generate configuration file:

1 <generatorconfiguration>2     <ContextID= "Testtables"Targetruntime= "MyBatis3">3         <Commentgenerator>4             <!--whether to remove automatically generated comments true: Yes: false: No -5             < Propertyname= "Suppressallcomments"value= "false" />6         </Commentgenerator>7         <!--Database connection Information: Driver class, connection address, user name, password -8         <jdbcconnectionDriverclass= "Com.mysql.jdbc.Driver"9 Connectionurl= "Jdbc:mysql://localhost:3306/shop"userId= "root"Ten Password=""> One         </jdbcconnection> A         <!--<jdbcconnection driverclass= "Oracle.jdbc.OracleDriver" - connectionurl= "JDBC:ORACLE:THIN:@127.0.0.1:1521:YYCG" - userid= "YYCG" the password= "YYCG" > - </jdbcConnection> - -  -         <!--by default, the JDBC decimal and NUMERIC types are resolved to Integer, true when the JDBC decimal and + NUMERIC type resolves to java.math.BigDecimal - -         <Javatyperesolver> +             < Propertyname= "Forcebigdecimals"value= "false" /> A         </Javatyperesolver> at  -         <!--Targetproject: Where to generate the PO class - -         <JavamodelgeneratorTargetpackage= "Com.luchao.mybatis.first.po" - Targetproject= ". \src"> -             <!--enablesubpackages: Do you want the schema to be the suffix of the package - -             < Propertyname= "Enablesubpackages"value= "false" /> in             <!--The space returned from the database before and after the value is cleaned - -             < Propertyname= "Trimstrings"value= "true" /> to         </Javamodelgenerator> +         <!--Targetproject:mapper The location of the map file generation - -         <SqlmapgeneratorTargetpackage= "Com.luchao.mybatis.first.mapper"  the Targetproject= ". \src"> *             <!--enablesubpackages: Do you want the schema to be the suffix of the package - $             < Propertyname= "Enablesubpackages"value= "false" />Panax Notoginseng         </Sqlmapgenerator> -         <!--Targetpackage:mapper the location generated by the interface - the         <Javaclientgeneratortype= "Xmlmapper" + Targetpackage= "Com.luchao.mybatis.first.mapper"  A Targetproject= ". \src"> the             <!--enablesubpackages: Do you want the schema to be the suffix of the package - +             < Propertyname= "Enablesubpackages"value= "false" /> -         </Javaclientgenerator> $         <!--Specify database Tables - $         <TableTableName= "Items"></Table> -         <TableTableName= "Orders"></Table> -         <TableTableName= "OrderDetail"></Table> the         <TableTableName= "User"></Table> -         <!--<table schema= "" Tablename= "Sys_user" ></table>Wuyi <table schema= "" Tablename= "Sys_role" ></table> the <table schema= "" Tablename= "Sys_permission" ></table> - <table schema= "" Tablename= "Sys_user_role" ></table> Wu <table schema= "" Tablename= "Sys_role_permission" ></table> - -          About         <!--Some table fields require a Java type to be specified $ <table schema= "" Tablename= "" > - <columnoverride column= "" Javatype= ""/> - </table> - -     </Context> A </generatorconfiguration>

2. Execute the Build program

1  Public classGeneratorsqlmap {2 3      Public voidGenerator ()throwsexception{4 5list<string> warnings =NewArraylist<string>();6         BooleanOverwrite =true;7         //Where reverse engineering configuration files are located8File ConfigFile =NewFile ("Generatorconfig.xml"); 9Configurationparser CP =Newconfigurationparser (warnings);TenConfiguration config =cp.parseconfiguration (configfile); OneDefaultshellcallback callback =Newdefaultshellcallback (overwrite); AMybatisgenerator Mybatisgenerator =Newmybatisgenerator (config, - callback, warnings); -Mybatisgenerator.generate (NULL); the  -     }  -      Public Static voidMain (string[] args)throwsException { -         Try { +Generatorsqlmap Generatorsqlmap =NewGeneratorsqlmap (); - generatorsqlmap.generator (); +}Catch(Exception e) { A e.printstacktrace (); at         } -          -     } -  -}

3. Put the generated files in your own project for testing.

In summary, the general reverse engineering is used in the enterprise development in accordance with the code of their own company code generator, this can reduce the need to Pojo, mapper mapping files and Mapper interface programming, and then according to the specific needs of the modification.

MyBatis Study--Reverse engineering

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.