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
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Doctype generatorconfiguration public "-// mybatis.org//dtd mybatis Generator configuration 1.0 // en" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
- <Generatorconfiguration>
- <Context ID = "context1">
- <Jdbcconnection driverclass = "oracle. JDBC. driver. oracledriver "connectionurl =" JDBC: oracle: thin: @ 192.168.2.21: 1521: orcl "userid =" ATFM "Password =" ATFM "/>
- <Javamodelgenerator targetpackage = "com. topsci. ATFM. Persistence. mybatis. Model" targetproject = "ATFM"/>
- <Sqlmapgenerator targetpackage = "com. topsci. ATFM. Persistence. mybatis. mapper" targetproject = "ATFM"> </sqlmapgenerator>
- <Javaclientgenerator targetpackage = "com. topsci. ATFM. Persistence. mybatis. Client" targetproject = "ATFM" type = "xmlmapper"/>
- <Table schema = "" tablename = "atfm_route_ctrl"> </table>
- <Table tablename = "syn_track_est" domainobjectname = "atfmtrack"> </table>
- </Context>
- </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
- Public list <trackbean> selecttrackonroute (string routename ){
- List <trackbean> RT = NULL;
- Sqlsession session = NULL;
- Try {
- Session = sqlsessionfactory. opensession ();
- Atfmtrackmapper mapper = session. getmapper (atfmtrackmapper. Class );
- // Construct query Conditions
- Atfmtrackexample example = new atfmtrackexample ();
- Example. createcriteria ()
- . Androuteis (routename );
- // Query
- List <atfmtrack> List = Mapper. selectbyexample (example );
- // Package it into trackbean
- RT = This. totrackbean (list );
- } Catch (exception e ){
- E. printstacktrace ();
- Logger. Error (E. getmessage ());
- } Finally {
- If (session! = NULL)
- Session. Close ();
- }
- Return RT;
- }
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
- Public criteria androuteis (string routename ){
- Stringbuffer sb = new stringbuffer ("point_name in" +
- "(Select P. Point from route_point P where P. Route = '" + routename + "')" +
- "And flight_no in" +
- "(Select D. flight_no from syn_aftn_dynamic_recent D" +
- "Where D. Route like '%" + routename + "% ')");
- Addcriterion (sb. tostring ());
- Return this;
- }
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