MyBatis can be understood as a semi-automated ORM Framework, through annotations or configuration XML mapping files to handwritten related SQL statements, and not as I introduced the ORM of the article as a whole object operation database additions and deletions. In fact, you will find that handwriting configuration XML mapping file is a very painful thing, because of this, MyBatis provides a generator tool, only need to configure the database connection string, the corresponding database driver package, and then execute the fixed-format command line statements can be done easily. Generator in: http://code.google.com/p/mybatis/wiki/Downloads?tm=2, need FQ download, here will provide generator file download at the end of the article.
First, generator file after decompression basic structure
1. src folder is the DAO, mapping, model file after the store is generated.
2, Generator.xml is the configuration file
3. Mybatis-generator-core-1.3.2.jar is a dependent jar package
4. The generated statement is a fixed-format command-line statement
Second, the configuration Generator.xml
The nodes that need to be modified are:
1, classpathentry database driver package location, physical absolute path
2, context\jdbcconnection database link URL, user name, password
3. Context\javamodelgenerator targetproject attribute, point to the SRC folder under Generator.xml folder, which represents the package name and location of the generated model
4, Context\sqlmapgenerator targetproject attribute, point to Generator.xml folder under the SRC folder, which represents the generated map file package name and location
5. Context\javaclientgenerator targetproject attribute, point to the SRC folder under Generator.xml folder, which represents the package name and location of the generated DAO
6, context\table TableName and Domainobjectname property, the name of the table name and the generated entity code of the database. Multiple tables on multiple context\table nodes
Sample files:
<?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> <!--Database driver Package location - <Classpathentry Location= "D:\94_JavaWorkbench\0_MavenRepository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar" /> <ContextID= "Mybatis3generator"Targetruntime= "MyBatis3"> <Commentgenerator> < Propertyname= "Suppressallcomments"value= "true" /> </Commentgenerator> <!--database link URL, user name, password - <jdbcconnectionDriverclass= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"Connectionurl= "Jdbc:sqlserver://localhost:1433;databasename=northwind"userId= "sa"Password=""> </jdbcconnection> <Javatyperesolver> < Propertyname= "Forcebigdecimals"value= "false" /> </Javatyperesolver> <!--Build the package name and location of the model - <JavamodelgeneratorTargetpackage= "Northwind.model"Targetproject= "D:\94_JavaWorkbench\0_MyBatis_Generator\src"> < Propertyname= "Enablesubpackages"value= "true" /> < Propertyname= "Trimstrings"value= "true" /> </Javamodelgenerator> <!--generated map file package name and location - <SqlmapgeneratorTargetpackage= "Northwind.mapping"Targetproject= "D:\94_JavaWorkbench\0_MyBatis_Generator\src"> < Propertyname= "Enablesubpackages"value= "true" /> </Sqlmapgenerator> <!--generate DAO's package name and location - <Javaclientgeneratortype= "Xmlmapper"Targetpackage= "Northwind.dao"Targetproject= "D:\94_JavaWorkbench\0_MyBatis_Generator\src"> < Propertyname= "Enablesubpackages"value= "true" /> </Javaclientgenerator> <!--to generate those tables (change tablename and Domainobjectname) - <TableTableName= "Categories"Domainobjectname= "Categories"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> <TableTableName= "Customers"Domainobjectname= "Customers"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> </Context></generatorconfiguration>
Iii. Generating related documents
Under the current folder into DOS, two methods (D:\cd do not introduce), as follows
1. Path box input cmd
2. Under current folder, press Shift right mouse button to enter DOS
After the DOS window appears, enter the command: Java-jar mybatis-generator-core-1.3.2.jar-configfile generator.xml-overwrite
Four, in the SRC folder to view the generated files, can be copied into the project.
Attached Generator file Download: Http://files.cnblogs.com/files/wucj/Generator.rar
MyBatis VII: Using the Generator tool