MyBatis how to automatically generate Java classes, configuration files?

Source: Internet
Author: User

In fact, nothing can be generated automatically, but someone else has already written, you call it.

So if you want to mybatis automatically generate Java classes, configuration files, etc., you must have some configuration and some jar packages. Of course, these configurations are simple as well.

In order to have a preliminary understanding, I first listed the required documents:

It is more important to label red. Okay, let's get started.

1. First, you need to build a table in the database, just build a few on the good.

2. Download the Mybatis-generator-core Package

: http://search.maven.org/

Then search Mybatis-generator-core to download

3. Download the Mysql-connector-java Package

Presumably everyone is familiar with this package, because it is the Java language driver package of MySQL

Because we produce the configuration file based on the database table, of course we need to connect to the database, that is, the driver package is required.

: http://search.maven.org/

Then search for Mysql-connector-java, select the version you want to download

4.generator.xml file

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 4 <generatorconfiguration>5  <!--clas<font size= "" color= "" ></font>spathentry: JDBC Driver for database -6   <Classpathentry Location= "D:\study\spring-mvc-mybatis-maven\mysql-connector-java-5.0.3-bin.jar" />7  8   <ContextID= "Db2tables"Targetruntime= "MyBatis3">9  <!--Remove Auto-generated annotations -Ten  <Commentgenerator> One   < Propertyname= "Suppressallcomments"value= "true" /> A  </Commentgenerator> -      <!--URL user name password for database - -     <jdbcconnectionDriverclass= "Com.mysql.jdbc.Driver" the Connectionurl= "Jdbc:mysql://localhost:3306/sy" - userId= "root" - Password= "MySQL"> -     </jdbcconnection> +  -     <Javatyperesolver> +       < Propertyname= "Forcebigdecimals"value= "false" /> A     </Javatyperesolver> at   -  <!--Build the package name and location of the model: where the code is automatically generated, - -     <JavamodelgeneratorTargetpackage= "Com.sy.model"Targetproject= "D:\study\spring-mvc-mybatis-maven\src"> -       < Propertyname= "Enablesubpackages"value= "true" /> -       < Propertyname= "Trimstrings"value= "true" /> -     </Javamodelgenerator> in      -  <!--automatically generate the package name and location of the mapping file - to     <SqlmapgeneratorTargetpackage= "Com.sy.mapping"Targetproject= "D:\study\spring-mvc-mybatis-maven\src"> +       < Propertyname= "Enablesubpackages"value= "true" /> -     </Sqlmapgenerator> the  <!--generate DAO's package name and location - *     <Javaclientgeneratortype= "Xmlmapper"Targetpackage= "Com.sy.dao"Targetproject= "D:\study\spring-mvc-mybatis-maven\src"> $       < Propertyname= "Enablesubpackages"value= "true" />Panax Notoginseng     </Javaclientgenerator> -   the  <!--tableName: A database table for automatic code generation; Domainobjectname: JavaBean class name that corresponds to a database table - +  <!--to generate those tables (change tablename and Domainobjectname) - A   <TableTableName= "Tbug"Domainobjectname= "Bug"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> the   <TableTableName= "Tmenu"Domainobjectname= "Menu"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> +   <TableTableName= "Tonline"Domainobjectname= "Online"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> -   <TableTableName= "Tresource"Domainobjectname= "Resource"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> $   <TableTableName= "Trole"Domainobjectname= "Role"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> $   <TableTableName= "Trole_tresource"Domainobjectname= "Roleresource"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> -   <TableTableName= "Tuser"Domainobjectname= "User"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> -   <TableTableName= "Tuser_trole"Domainobjectname= "Userrole"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> the   </Context> -   Wuyi </generatorconfiguration>
Generator.xml

In the Generator.xml file there are very detailed comments, we can see that we need to do

(1) Modify the path of MySQL driver, the path of the small series is as follows:

<classpathentry location= "D:\study\spring-mvc-mybatis-maven\mysql-connector-java-5.0.3-bin.jar"/>

(2) Modify the URL user name password for the database

(3) Modify the path to save the automatically generated files, the path of the small series is

<javamodelgenerator targetpackage= "Com.sy.model" targetproject= "D:\STUDY\SPRING-MVC-MYBATIS-MAVEN\SRC" >

Here you can build a src empty folder, and then the full path copy covers the path of the small series.

 Note: The Model,mapper,dao three paths here need to be modified, otherwise you will not find the path.

5. OK, the preparation is done, a command can be generated

First CMD enters the path to the Generator.xml file as

CD D:

CD Study\spring-mybatis-maven

(Win 7 has the shortcut to enter:

In the Generator.xml directory, right-click: Open the Command Window here (W))

To run the command:

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

Did it succeed? Take a look at the newly created SRC folder, and see if you have the code already.

  

  

  

MyBatis how to automatically generate Java classes, configuration files?

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.