SSM framework-Automatically creating code using MyBatis Generator

Source: Internet
Author: User

These two days need to use the MyBatis code automatically generated functionality, because MyBatis belongs to a semi-automatic ORM framework, so the main task is to configure mapping mapping file, but because the handwriting map file is very error-prone, so you can use the MyBatis generator automatically generated entity classes, DAO interfaces, and mapping mapping files. This can save a lot of effort, copy the generated code to project engineering.

There are many ways to use auto-generation, and you can install plug-ins in eclipse, but the following is the way I think it is easy, the simplest, no plug-ins, just the next few jar packs, put them under a directory.


Generate the required files and jar packages for your code:


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

There are jar packages for the MyBatis framework, database driver jar packages, and MyBatis generator jar packages. The Generatorconfig.xml is a file that needs to be configured by us, configured as follows:


[HTML]View Plaincopy print?
  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.1.1/db_124 " userid=" dem " password=" dem ">
  16. </jdbcconnection>
  17. <javatyperesolver>
  18. <property name="forcebigdecimals" value= "false"/>
  19. </javatyperesolver>
  20. <!--generate the package name and location of the model--
  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 mapping file--
  26. <sqlmapgenerator targetpackage="test.mapping" targetproject="src">
  27. <property name="enablesubpackages" value="true"/>
  28. </sqlmapgenerator>
  29. <!--Generate DAO's package name and location--
  30. <javaclientgenerator type="Xmlmapper" targetpackage= "test. Idao " targetproject="src ">
  31. <property name="enablesubpackages" value="true"/>
  32. </javaclientgenerator>
  33. <!--the table TableName to be generated is the table name or view name in the database Domainobjectname is the entity class name--
  34.         <table < span class= "attribute" >tablename= "user_info_t"   Domainobjectname= "User"  enablecountbyexample= "false"  enableupdatebyexample= "false"  enabledeletebyexample= "false"  enableselectbyexample= "false"  selectbyexamplequeryid= "false" ></table >  
  35. </Context>
  36. </generatorconfiguration>


When these are completed, simply open the console and enter the Lib directory to execute the script:

Java-jar Mybatis-generator-core-1.3.2.jar-configfile Generatorconfig.xml-overwrite

Can.


So after the build, you can find the corresponding folder in the SRC directory, each table will correspond to three files (entity class, interface, configuration file).


SSM framework-Automatically creating code using MyBatis Generator

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.