Use idea to build mapper and Pojo_ databases based on MAVEN projects using Mybatis-generator-plugin

Source: Internet
Author: User
Tags log4j

Original address


Although MyBatis is very convenient, but want to write all the mapper still very tiring, fortunately mybatis the official introduction of automation tools, can be based on the database and the definition of a good configuration directly generated DAO layer and all the following code, very convenient.
First WOM We build a maven project, I don't write it in detail here.
Mybatis-generator Use configuration

Open the Pom.xml file and add 3 dependencies and Mybatis-generator plug-ins, respectively, 1.mybatis3.xjar Package 2. Reverse engineering Core Package 3. Database connection pack 4.log4j.jar for output logs

<build> <plugins> <plugin> <!--with maven MyBatis plugin If you do not add a dependency package to the plugin, you will not find the relevant jar package, plugin outside the jar package, he will not go to find and execute, so the plugin run
                depend on the jar configuration all inside--> <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <dependency> <groupid>log4j&lt ;/groupid> <artifactId>log4j</artifactId> <version>1.
                        2.17</version> </dependency> <dependency>
                        <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <versIon>3.2.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</artifact
                Id> <version>5.1.30</version> </dependency>

 </dependencies> </plugin> </plugins> </build>

Create the reverse engineering configuration file Generatorconfig.xml under the Src/main/resources package as follows:

<?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=" Testtables
            "Targetruntime=" MyBatis3 > <commentGenerator> <!--whether to remove automatically generated comments true: false: No--> <property name= "Suppressallcomments" value= "true"/> </commentGenerator> <!--database connection Information: Driver class, connection address, username, password--> <jdbcconnection driverclass= "com.mysql.jdbc.Driver" Connect Ionurl= "Jdbc:mysql://localhost:3306/mybatis" userid= "root" password= "123qw"
            E "> </jdbcConnection> <!--<jdbcconnection driverclass=" Oracle.jdbc.OracleDriver " Connectionurl= "JDBC:ORACLE:THIN:@127.0.0.1:1521:YYCG" userid= "YYCG" PASSWord= "YYCG" > </jdbcConnection>--> <!--default false, resolves JDBC DECIMAL and NUMERIC types to integers, to Tru e when the JDBC DECIMAL and NUMERIC types are resolved to Java.math.BigDecimal--> <javaTypeResolver> &LT;PR Operty name= "Forcebigdecimals" value= "false"/> </javaTypeResolver> <!--targetproject: Generating PO Class
            Location--> <javamodelgenerator targetpackage= "po" targetproject= "src" >
            <!--enablesubpackages: Let schema be the suffix of the package--> <property name= "Enablesubpackages" value= "false"/> <!--the space--> <property name= "trimstrings" value= "true"/> before and after the value returned from the database is cleaned </jav Amodelgenerator> <!--the location where Targetproject:mapper map files are generated--> <sqlmapgenerator targetpackage= "Mapper
            "targetproject=" src > <!--enablesubpackages: Let schema be the suffix of the package--> <property name= "EnaBlesubpackages "value=" false "/> </sqlMapGenerator> <!--targetpackage:mapper where the interface is generated-->
                             <javaclientgenerator type= "Xmlmapper" targetpackage= "Mapper" targetproject= "src" > <!--enablesubpackages: Let schema be the suffix of the package--> <property name= " Enablesubpackages "value= false"/> </javaClientGenerator> <!--Specify database table--> <tabl E tablename= "Items" ></table> <table tablename= "Orders" ></table> <table tablename= " OrderDetail "></table> <!--<table schema=" "Tablename=" Sys_user "></table> <tabl E schema= "" Tablename= "Sys_role" ></table> <table schema= "" Tablename= "Sys_permission" ></table&gt
        ; <table schema= "tablename=" Sys_user_role "></table> <table schema=" "Tablename=" sys_role_permission "></table>--> <table tablename= "t_warningsetting" domainobjectname= "Warningsetting" enableCountByExample= "false" E Nableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexampleque
            Ryid= "false" ></table> <!--The fields of some tables need to specify the Java type <table schema= "Tablename=" > <columnoverride column= "" javatype= ""/> </table>--> </context> </generatorconfigura

 Tion>

Where to modify:

Javamodelgenerator, generate the position of the PO class
Location of Sqlmapgenerator,mapper Map file generation
Where the Javaclientgenerator,mapper interface is built
Table, whose TableName property corresponds to the corresponding tables in the database

Click on the MAVEN Projects tab to the right of idea and follow the diagram below
Here to write a picture description

After running the plug-in, the final engineering directory structure is as follows:
Here to write a picture description

The application of Mybatis-generator

Mybatis-generator tend to create a common project such as a, by running reverse engineering to generate the corresponding mapper and PO and then copying the two packages to the Web project we created using the SSM framework instead of using reverse engineering directly in the Web project.

By running the above program, we quickly generated the corresponding PO class and mapper through the table in the database, instead of our programmers to write the corresponding PO class and Mapper, for us to bring a great convenience, so this must learn, In the subsequent development as long as the use of mybatis where we will be through MyBatis reverse engineering automatically generate mapper and PO class for us.

Original address


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.