MyBatis generator automatically generate Model,mapper and other files

Source: Internet
Author: User

MyBatis Generator Introduction

MyBatis Generator Chinese Document http://mbg.cndocs.tk/

MyBatis Generator (MBG) is a MyBatis code generator MyBatis and IBATIS. He can generate code for each version of MyBatis, and Ibatis 2.2.0 later code. He can introspect the database's tables (or tables) and then generate the underlying objects that can be used to access (multiple) tables. This makes it unnecessary to create objects and configuration files when interacting with database tables. MBG solves some simple crud (insert, query, UPDATE, DELETE) operations that have the greatest impact on database operations. You still need to write SQL and objects to federated queries and stored procedures.

Adding plugins to the POM

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationfile>src/main/resources/mybatis-generator/generator-config.xml
</configurationFile>
<verbose>true</verbose>
<overwrite>false</overwrite>
<skip>false</skip>
</configuration>
<executions>
<execution>
<id>generate MyBatis artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</plugin>

Add files to Resource

Generator.properties

Jdbc.driverclass=com.mysql.jdbc.driver
Jdbc.connectionurl=jdbc:mysql://localhost:3306/mybatis
Jdbc.userid=root
jdbc.password=1234

Tables in the database

Generator-config.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>
<!--database-driven
<properties resource= "Mybatis-generator/generator.properties" ></properties>

<context id= "Default" Targetruntime= "MyBatis3" >
<!--control annotations When you create a class--
<commentGenerator>
<property name= "Suppressdate" value= "true"/>
<property name= "Suppressallcomments" value= "true"/>
</commentGenerator>
<!--database link Address account password--
<jdbcconnection driverclass= "${jdbc.driverclass}" connectionurl= "${jdbc.connectionurl}" userId= "${jdbc.userId} "Password=" ${jdbc.password} ">
</jdbcConnection>
<!--type processor, conversion between database type and Java type Control--
<javatyperesolver >
<property name= "Forcebigdecimals" value= "false"/>
</javaTypeResolver>
<!--generate model class storage location-
<javamodelgenerator targetpackage= "Com.dakewang.dto" targetproject= "Src/main/java" >
<!--If you add a constructor to the model--
<property name= "constructorbased" value= "false"/>
<!--whether to allow child packages, i.e. TargetPackage.schemaName.tableName
<property name= "Enablesubpackages" value= "false"/>
< If the model object created by the!--is immutable, the resulting model object will not have setter methods, only construction methods--
<property name= "Immutable" value= "false"/>
<!--add a parent class to the model--
<!--<property name= "Rootclass" value= "Com.foo.louis.Hello"/>-->
<!--whether to trim data for columns of class char type-
<property name= "Trimstrings" value= "true"/>
</javaModelGenerator>
<!--generate Map file location, generate the corresponding Sqlmap file for each database table--
<sqlmapgenerator targetpackage= "Com.dakewang.mapper" targetproject= "Src/main/java" >
<property name= "Enablesubpackages" value= "false"/>
</sqlMapGenerator>

<!--client code to generate easy-to-use code for model objects and XML configuration files
Type= "Annotatedmapper" to generate Java Model and annotation-based mapper objects
Type= "Mixedmapper" to generate the annotated Java Model and the corresponding Mapper object
Type= "Xmlmapper", generate Sqlmap XML file and stand-alone mapper interface
-
<!--generate a DAO class storage location-
<javaclientgenerator targetpackage= "Com.dakewang.mapper" targetproject= "Src/main/java" type= "Mixedmapper" >
<property name= "Enablesubpackages" value= "false"/>
</javaClientGenerator>



<!--generate the corresponding table and class name--
<table tablename= "Student_info" domainobjectname= "Student" enablecountbyexample= "false" enableupdatebyexample= " False "enabledeletebyexample=" false "enableselectbyexample=" false "Selectbyexamplequeryid=" false "></table >
<!--the table TableName to be generated is the table name or view name in the database Domainobjectname is the entity class name--


</context>
</generatorConfiguration>

Start run

If the add succeeds, click Run Generator, then the console output information

Process finished with exit code 0

It will then automatically generate

MyBatis generator automatically generate Model,mapper and other files

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.