We are developing the SSM framework (not limited to this framework) in order to develop efficiency. Sometimes you have to improve your code speed. The same thing everyone has headaches, such as writing DAO, writing model, writing mapping and so on. Not only slow, but also an inattentive, will make mistakes.
Today, teach you a simple way to automatically generate DAO, model, and mapping methods, fast and easy to make mistakes!
First, you need to prepare the following tools:
A jar package, a configuration file. Download online, Baidu can! In the sibling directory, create a new empty src folder,
Next, open your database (here I am MySQL):
I have only one table here, is not very little ...
Then, using a text editor to open the Generator.xml,
Here I do the following instructions:
Line 5th, need to load the local absolute path of the database driver package, you use MySQL, load your local MySQL database driver package, Oracle with Oracle, I use the MAVEN repository of MySQL database driver package;
Line 11th, your database driver class, URL, user name and password, not much introduced.
Line 18th, targetpackage= "Yx.model", indicates the name of the model package that you generated in the project, targetproject= "E:\MODEL\GENERATOR\SRC indicates the location of the models you generated on your local computer, I'm storing it under the E:\MODEL\GENERATOR\SRC,
That is, the resulting DAO, mapping, and model are best placed under SRC, which is easy to copy to the Src/main/java folder in the project, if the E:\MODEL\GENERATOR\SRC path is specified above, And you do not have the SRC folder locally, you cannot
Generated.
Line 23rd generates the location of the mapping mapping file and package, with the same considerations as line 18th, the path problem is not mistaken.
The 27th line generates the DAO's package name and location, as noted above.
Lines 31st and 32: tablename= "User" is the name of the table you want to generate in the database for DAO, mapping, and model, and here I have a user table, if there are multiple tables, copy the table label and rename it. Domainobjectname= the name of the entity class generated by "user", I am the user here. The corresponding path is generated in the model
The path of the package. After some of the properties of false and so on, if not written, the generated code will be very messy! Or keep it as good!
Get ready for work!
Next, in the directory, shift+ right click here to open the Command Line window (win7 system):
Copy build statement: Java-jar mybatis-generator-core-1.3.2.jar-configfile Generator.xml-overwrite
Right-click in the Command Line window: Enter run:
Build DAO, Mapping and model successfully! Let's go down to E:\model\generator\src and see if we want it:
has been generated successfully! Then copy the Yx folder under SRC to the Eclipse Project under Src/main/java, OK!
Using generator to generate DAO, mapping, and model