Use mybatis generator to automatically create code

Source: Internet
Author: User

The Automatic Generation of mybatis code is required in the past two days. Because mybatis is a semi-automatic ORM framework, the main task is to configure the mapping ing file, however, because handwritten ing files are prone to errors, you can use the mybatis generator to automatically generate object classes, Dao interfaces, and mapping ing files. This saves a lot of effort to copy the generated code to the project.

There are many ways to use automatic generation. you can install plug-ins in eclipse. However, I think this method is easy and simple, and you do not need to install plug-ins, you only need to put the following JAR packages in a directory.

 

Generate the files and jar packages required by the Code:

(File: http://download.csdn.net/detail/u012909091/7206091)

The jar package of the mybatis framework, the jar package of the database driver, and the jar package of the mybatis generator. The generatorconfig. xml file must be configured as follows:

 

 

[HTML]View plaincopyprint?
  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. <Generatorconfiguration>
  6. <! -- Database-driven -->
  7. <Classpathentry location = "mysql-connector-java-5.1.25-bin.jar"/>
  8. <Context ID = "db2tables" targetruntime = "mybatis3">
  9. <Commentgenerator>
  10. <Property name = "suppressdate" value = "true"/>
  11. <! -- Whether to remove automatically generated comments true: Yes: false: No -->
  12. <Property name = "suppressallcomments" value = "true"/>
  13. </Commentgenerator>
  14. <! -- Database link URL, user name, password -->
  15. <Jdbcconnection driverclass = "com. MySQL. JDBC. Driver" connectionurl = "JDBC: mysql: // 125.221.225.113/db_demao" userid = "Demao" Password = "Demao">
  16. </Jdbcconnection>
  17. <Javatyperesolver>
  18. <Property name = "forcebigdecimals" value = "false"/>
  19. </Javatyperesolver>
  20. <! -- Generate the model package name and location -->
  21. <Javamodelgenerator targetpackage = "test. Domain" targetproject = "src">
  22. <Property name = "enablesubpackages" value = "true"/>
  23. <Property name = "trimstrings" value = "true"/>
  24. </Javamodelgenerator>
  25. <! -- Generate the package name and location of the ing file -->
  26. <Sqlmapgenerator targetpackage = "test. Mapping" targetproject = "src">
  27. <Property name = "enablesubpackages" value = "true"/>
  28. </Sqlmapgenerator>
  29. <! -- Generate the package name and location of Dao -->
  30. <Javaclientgenerator type = "xmlmapper" targetpackage = "test. idao" targetproject = "src">
  31. <Property name = "enablesubpackages" value = "true"/>
  32. </Javaclientgenerator>
  33. <! -- The tablename of the table to be generated is the table name or view name in the database. domainobjectname is the object class name. -->
  34. <Table tablename = "user_info_t" domainobjectname = "user" metadata = "false" metadata = "false" enabledeletebyexample = "false" enableselectbyexample = "false" selectbyexamplequeryid = "false"> </ table>
  35. </Context>
  36. </Generatorconfiguration>

 

 

After the preceding steps are completed, you only need to open the console, enter the lib directory, and execute the script:

Java-jar mybatis-generator-core-1.3.2.jar-configfile generatorconfig. XML-overwrite

You can.

In this way, you can find the corresponding folder under the src directory. Each table corresponds to three files (entity classes, interfaces, and configuration files ).

Use mybatis generator to automatically create code

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.