Use mybatis generator to automatically create code

Source: Internet
Author: User

If you have used the Eclipse plug-in of Hibernate to automatically create the DaO file, you can easily understand the content described below. If you have not used hibernate, it is okay. The following describes how to use the Eclipse plug-in of mybatis 3 to automatically generate related files.

 

Eclipse plug-in installation address: http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/

 

Attachment link installation package, link Installation Method reference http://maimode.iteye.com/admin/blogs/1164524

 

For more information about mybatis generator, see: http://code.google.com/p/mybatis/wiki/Generator

 

After installing the plug-in, you will find that the mybatis option is added to file-New-Other in eclipse, indicating that the plug-in is successfully installed.

 

 

How to Use plug-ins

 

In any project, use the Wizard to create the generatorconfig. xml file (the name can be modified) and modify the file content, mainly to set the parameters related to the connection data:

 

 

XML Code  
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype generatorconfiguration public "-// mybatis.org//dtd mybatis Generator configuration 1.0 // en" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  3. <Generatorconfiguration>
  4. <Context ID = "context1">
  5. <Jdbcconnection driverclass = "oracle. JDBC. driver. oracledriver "connectionurl =" JDBC: oracle: thin: @ 192.168.2.21: 1521: orcl "userid =" ATFM "Password =" ATFM "/>
  6. <Javamodelgenerator targetpackage = "com. topsci. ATFM. Persistence. mybatis. Model" targetproject = "ATFM"/>
  7. <Sqlmapgenerator targetpackage = "com. topsci. ATFM. Persistence. mybatis. mapper" targetproject = "ATFM"> </sqlmapgenerator>
  8. <Javaclientgenerator targetpackage = "com. topsci. ATFM. Persistence. mybatis. Client" targetproject = "ATFM" type = "xmlmapper"/>
  9. <Table schema = "" tablename = "atfm_route_ctrl"> </table>
  10. <Table tablename = "syn_track_est" domainobjectname = "atfmtrack"> </table>
  11. </Context>
  12. </Generatorconfiguration>

 

 

According to the name, the general meaning should be displayed.

 

After you have configured the information for connecting to the database and table, you can use the plug-in to automatically generate code.

 

 

Click the option in. If the configuration is correct, the related files are automatically created.

There are three types of files:

Client package and mapper interface file

Model package, Entity Bean File

Mapper package, mapper XML file

 

How to use these automatically generated files

 

First, add the XML file under the Mapper package to the sqlmapper file of mybatis.

 

 

Then use the following in the program:

 

 

Java code  
  1. Public list <trackbean> selecttrackonroute (string routename ){
  2. List <trackbean> RT = NULL;
  3. Sqlsession session = NULL;
  4. Try {
  5. Session = sqlsessionfactory. opensession ();
  6. Atfmtrackmapper mapper = session. getmapper (atfmtrackmapper. Class );
  7. // Construct query Conditions
  8. Atfmtrackexample example = new atfmtrackexample ();
  9. Example. createcriteria ()
  10. . Androuteis (routename );
  11. // Query
  12. List <atfmtrack> List = Mapper. selectbyexample (example );
  13. // Package it into trackbean
  14. RT = This. totrackbean (list );
  15. } Catch (exception e ){
  16. E. printstacktrace ();
  17. Logger. Error (E. getmessage ());
  18. } Finally {
  19. If (session! = NULL)
  20. Session. Close ();
  21. }
  22. Return RT;
  23. }

 

If the where condition is complex, you can also customize the query condition. In the preceding example, androuteis (routename) is the custom query condition. You can

The specific internal class criteria of example custom query conditions:

 

 

Java code  
  1. Public criteria androuteis (string routename ){
  2. Stringbuffer sb = new stringbuffer ("point_name in" +
  3. "(Select P. Point from route_point P where P. Route = '" + routename + "')" +
  4. "And flight_no in" +
  5. "(Select D. flight_no from syn_aftn_dynamic_recent D" +
  6. "Where D. Route like '%" + routename + "% ')");
  7. Addcriterion (sb. tostring ());
  8. Return this;
  9. }

 

We may worry that once we re-Execute generate, the code we write will be lost, no,The plug-in will not modify or discard our own code.

 

Once you have mastered how to use the plug-in, it is important to use the xxxexample class. In this way, you do not need to write tedious mapper XML 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.