Analysis and simple tutorial of reverse engineering of MyBatis (with code)

Source: Internet
Author: User
Tags generator

Reverse engineering of MyBatis

One: What is retrograde engineering.

MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually sets parameters and gets the result set. MyBatis can use simple XML or annotations to configure and map native information, mapping interfaces and Java POJOs (Plain old Java Objects, ordinary Java objects) to records in a database. When the database table is more, duplicate create Pojo object and simple database table (CRUD) operation Mapper, inefficient, the official gave the use of MyBatis generator used to reverse the generation of the database table Pojo and mapper files, greatly facilitated development.

II: a simple tutorial

    • Add plug-ins in Maven's Pom.xml

 <packaging>jar</packaging><dependencies> <!--adding dependency on MyBatis-<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </ dependency> <dependency> <groupId>org.mybatis</groupId> &LT;ARTIFACTID&G t;mybatis-spring</artifactid> </dependency> <dependency> <groupid>com.git         Hub.miemiedev</groupid> <artifactId>mybatis-paginator</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactid>pa Gehelper</artifactid> </dependency> <!--MySql--<dependency> &L T;groupid>mysql</groupid> <artifactId>mysql-connector-java</artifactId> </depend Ency> <!--Connecting Pool-<dependency> <groupId>com.alibaba</groupId> <artifactid>dr uid</artifactid> </dependency> <dependency> <groupid>org.mybatis.generato R</groupid> <artifactId>mybatis-generator-core</artifactId> &LT;VERSION&GT;1.3.6&L t;/version> </dependency> </dependencies> <build> <plugins> <                !--MyBatis Reverse Engineering-<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</vers Ion> <configuration> < location!--profile--<configurationf Ile>src/main/resources/generatorconfig.xml</configurationfile> <verbose>true</verbose                    ><overwrite>true</overwrite> </configuration> </plugin> </plugi Ns> </build>
    • Create configuration file Generatorconfig.xml

<?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> <!--introducing a database configuration file for easy modification--& Lt;properties resource= "Jdbc.properties"/> <!--database driver files need to be modified todo--> <classpathentry location = "/volumes/tool/java_tool/maven/maven_repository/mysql/mysql-connector-java/5.1.32/ Mysql-connector-java-5.1.32.jar "/> <context id=" context "targetruntime=" Mybatis3simple "> <commentGen Erator> <!--whether to remove auto-generated comments, true: Yes; false: no---<property name= "suppressallcomments" value= " True "/> <property name=" Suppressdate "value=" false "/> </commentGenerator> <!--number Information about the Library connection: driver class, connection address, user name, password---<jdbcconnection driverclass= "${jdbc.driverclassname}" conn Ectionurl= "${jdbC.url} "userid=" ${jdbc.username} "password=" ${jdbc.password} "/> &L t;! --default false, parse the JDBC decimal and NUMERIC types to integer,--> <!--to True to parse the JDBC decimal and NUMERIC types into Java.math.BigDeci Mal-<!--not required, type processor, conversion between database type and Java type Control--<javaTypeResolver> <property Nam E= "Forcebigdecimals" value= "false"/> </javaTypeResolver> <!--generate PO class location configuration generated entity Package--<        !--targetpackage: Generated entity package location, default in src directory-<!--targetproject: relative path stitching results src/main/java/one/domain <javamodelgenerator targetpackage= "One.domain" targetproject= "Src/main/java" > <!--enablesubpackages : Do you want the schema to be the suffix of the package--<property name= "Enablesubpackages" value= "false"/> <!--the value returned from the database is cleaned Front and rear spaces-<property name= "Trimstrings" value= "true"/> </javaModelGenerator> <!- -The entity package corresponds to the map file location and name, which is stored by defaultIn the SRC directory, the <sqlmapgenerator targetpackage= "Mapperxml" targetproject= "Src/main/resources" > & lt;! --Enablesubpackages: Do you want the schema as the package suffix--<property name= "Enablesubpackages" value= "false"/> &lt ;/sqlmapgenerator> <!--Targetpackage:mapper interface generated location--<javaclientgenerator targetpackage= "one.ma            Pper "type=" Xmlmapper "targetproject=" Src/main/java "> <!--enablesubpackages: Do you want the schema to be a suffix of the package--         <property name= "Enablesubpackages" value= "false"/> </javaClientGenerator> <!--configuration Table-- <!--Schema: Do not fill-in <!--tableName: Table name todo--> <!--enablecountbyexample, Enableselectbye Xample, Enabledeletebyexample, Enableupdatebyexample, selectbyexamplequeryid:--> <!--Remove Auto-generated annotation examples--&L T;table tablename= "Tb_user" enablecountbyexample= "false" enabledeletebyexample= "false" Enableselectbyexample = "false" EnableupdatEbyexample= "false"/> </context></generatorConfiguration> 
    • Run the generator class to generate

I've already done the project. Download to run

Related Article

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.