MyBatis the use of generator generated code tools

Source: Internet
Author: User

MyBatis Generator Generate code tool for use, with demo

When you use Hibernate, you can easily generate Model,dao, and map configuration files. In MyBatis, there are generators, namely MyBatis Generator, abbreviated MBG. Let's introduce the use of MBG.

Download Mybatis-generator-core-1.3.1-bundle. zip , unzip to get Mybatis-generator-core-1.3.1.jar, which is the jar package of the generator, Add Mybatis-3.0.6.jar and Mybatis-generator-core-1.3.1.jar to the project Lib, and then write a generator's configuration file Generatorconfig.xml.

Introduce Genernatorconfig.xml

<?xml version= "1.0" Encoding= "UTF-8"?>

<! DOCTYPE generatorconfiguration
Public "-// MyBatis Generator Configuration 1.0//en"
"Http://" >

<properties resource= "Com/yuan/mybatis/mbg/util/"/>
<classpathentry location= "${classpath}"/>
<context id= "MBG" targetruntime= "MyBatis3" defaultmodeltype= "conditional" >
<plugin type= "Org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<plugin type= "Org.mybatis.generator.plugins.MapperConfigPlugin" >
<property name= "FileName" value= "Generatedmapperconfig.xml"/>
<property name= "Targetpackage" value= "Com.yuan.mybatis.mbg.util"/>
<property name= "Targetproject" value= "${targetproject}"/>
<property name= "Suppressallcomments" value= "true"/>
<jdbcconnection driverclass= "${driverclass}"
Connectionurl= "${connectionurl}" userid= "${userid}" password= "${password}" >
<property name= "Forcebigdecimals" value= "false"/>

<javamodelgenerator targetpackage= "${modelpackage}"
targetproject= "${targetproject}" >
<property name= "Enablesubpackages" value= "true"/>

<sqlmapgenerator targetpackage= "${sqlmapperpackage}"
targetproject= "${targetproject}" >
<property name= "Enablesubpackages" value= "true"/>

<javaclientgenerator type= "Xmlmapper"
Targetpackage= "${daomapperpackage}" targetproject= "${targetproject}" >
<property name= "Enablesubpackages" value= "true"/>
<table schema= "Minghan" tablename= "Sys_group" domainobjectname= "group" >
<generatedkey column= "groupId" sqlstatement= "MySQL"
Identity= "true"/>
<table schema= "Minghan" tablename= "Sys_admin" domainobjectname= "admin"
Enablecountbyexample= "false" enableupdatebyexample= "false"
Enabledeletebyexample= "false" enableselectbyexample= "false"
Selectbyexamplequeryid= "false" >


Where a profile is referenced in the properties element for ease of porting, You simply modify the resource's path value and the value in the to make the build operation.

The code is as follows:


Where Targetproject is the source code of the project to store the location

Classpath is the location of the database driver package, Modelpackage is the package that holds the model entity, the corresponding database table, Sqlmapperpackage is the XML configuration file that holds the entity mappings, Daomapperpackage is where the mapper interface is stored.

Note: These packages can be built in advance.

If you do not want to generate MyBatis annotation information, you can set the value of Suppressallcomments in Commentgenerator to True,

Table elements correspond to database tables.

Such as

<table schema= "Minghan" tablename= "Sys_admin" domainobjectname= "admin" enablecountbyexample= "false" Enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "False" ></table>

The schema is the database name, TableName is the corresponding database table, Domainobjectname is the entity class to be generated, if you want to mapper the configuration file to join the SQL where condition query, The enablecountbyexample can be set to true, which will generate a corresponding Domainobjectname example class, Enablecountbyexample, and so on, when set to False, The corresponding example class is not generated.

Once the configuration file is well-equipped,

To generate code, you can either use the command , or you can write a class with main to run, the following provides a running class with main

Package com.yuan.mybatis.mbg.util;

Import java.sql.SQLException;
Import java.util.ArrayList;
Import java.util.List;

Import Org.mybatis.generator.api.MyBatisGenerator;
Import org.mybatis.generator.config.Configuration;
Import Org.mybatis.generator.config.xml.ConfigurationParser;
Import org.mybatis.generator.exception.InvalidConfigurationException;
Import org.mybatis.generator.exception.XMLParserException;
Import Org.mybatis.generator.internal.DefaultShellCallback;

public class Mybatisgeneratortool {
public static void Main (string[] args) {
list<string> warnings = new arraylist<string> ();
Boolean overwrite = true;
String gencfg = "/generatorconfig.xml"; SRC in the first level directory
File ConfigFile = new file (MyBatisGeneratorTool.class.getResource (gencfg). GetFile ());
Configurationparser cp = new Configurationparser (warnings);
Configuration config = null;
try {
Config = cp.parseconfiguration (configfile);
} catch (IOException e) {
E.printstacktrace ();
} catch (Xmlparserexception e) {
E.printstacktrace ();
Defaultshellcallback callback = new Defaultshellcallback (overwrite);
Mybatisgenerator mybatisgenerator = null;
try {
Mybatisgenerator = new Mybatisgenerator (config, callback, warnings);
} catch (Invalidconfigurationexception e) {
E.printstacktrace ();
try {
Mybatisgenerator.generate (NULL);
} catch (SQLException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} catch (Interruptedexception e) {
E.printstacktrace ();

Run this class to generate the entity classes that correspond to the database tables, the mapper configuration file, the Mapper interface, and then test the availability of the build.

MyBatis the use of generator generated code tools

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: 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.