Automatic generation of Table instance classes and mapping files using Mybatis-generator

Source: Internet
Author: User

We often use MyBatis to do the program code level to the database operation, but need to write a large number of Table instance class and mapping file, now use tool Mybatis-generator to achieve the above file automatic generation, the following is a brief introduction to the use of the method.

1. Create a project

In order to download the jar package is convenient, I create a maven project called MyBatis to apply Mybatis-generator.

  

2. Modify the Pom.xml file to download the dependent jar package

<Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelversion>4.0.0</modelversion>  <groupId>Com.liuwei</groupId>  <Artifactid>Springmybatis</Artifactid>  <version>0.0.1-snapshot</version><Dependencies>     <!--Add MyBatis Core Package -     <Dependency>          <groupId>Org.mybatis</groupId>          <Artifactid>MyBatis</Artifactid>          <version>3.2.8</version>     </Dependency>     <!--add MySQL driver pack -     <Dependency>          <groupId>Mysql</groupId>          <Artifactid>Mysql-connector-java</Artifactid>          <version>5.1.34</version>     </Dependency>     <!--adding junit unit test Packages -        <Dependency>            <groupId>Junit</groupId>            <Artifactid>Junit</Artifactid>            <version>4.12</version>            <Scope>Test</Scope>        </Dependency>        <!--mybatis configuration file Generation tool -        <Dependency>            <groupId>Org.mybatis.generator</groupId>            <Artifactid>Mybatis-generator-core</Artifactid>            <version>1.3.2</version>        </Dependency></Dependencies></Project>
View Code

3. Write the Generator.xml configuration file and place it in the project root directory

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 <generatorconfiguration>4     <!--Database driver Package location -5     <Classpathentry Location= "C:\Users\dm1-10473\.m2\repository\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar"  /> 6     <ContextID= "Db2tables"Targetruntime= "MyBatis3">7         <Commentgenerator>8             < Propertyname= "Suppressallcomments"value= "true" />9         </Commentgenerator>Ten         <!--database link URL, user name, password - One          <jdbcconnectionDriverclass= "Com.mysql.jdbc.Driver"Connectionurl= "Jdbc:mysql://localhost:3306/spring_mybatis"userId= "root"Password= "123">  A         </jdbcconnection> -         <Javatyperesolver> -             < Propertyname= "Forcebigdecimals"value= "false" /> the         </Javatyperesolver> -         <!--generates the package name and location of the entity class, which is configured to place the generated entity classes under the Me.springmybatis.domain package - -         <JavamodelgeneratorTargetpackage= "Me.mybatis.domain"Targetproject= "C:\Users\dm1-10473\workspace\mybatis\src\main\java"> -             < Propertyname= "Enablesubpackages"value= "true" /> +             < Propertyname= "Trimstrings"value= "true" /> -         </Javamodelgenerator> +         <!--generate the SQL map file package name and location, where you configure the generated SQL mapping file to be placed under me.springmybatis.mapping this package - A         <SqlmapgeneratorTargetpackage= "Me.mybatis.mapping"Targetproject= "C:\Users\dm1-10473\workspace\mybatis\src\main\java"> at             < Propertyname= "Enablesubpackages"value= "true" /> -         </Sqlmapgenerator> -         <!--generates the DAO's package name and location, where it is configured to place the generated DAO class under the Me.springmybatis.dao package - -         <Javaclientgeneratortype= "Xmlmapper"Targetpackage= "Me.mybatis.dao"Targetproject= "C:\Users\dm1-10473\workspace\mybatis\src\main\java"> -             < Propertyname= "Enablesubpackages"value= "true" /> -         </Javaclientgenerator> in         <!--to generate those tables (change tablename and Domainobjectname) - -         <TableTableName= "T_user"Domainobjectname= "User"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false" /> to     </Context> + </generatorconfiguration>
Generator.xml

4. Creating databases and Tables

1 Create DATABASESpring_mybatis;2  UseSpring4_mybatis3;3 4 DROP TABLE IF EXISTST_user;5 CREATE TABLET_user (6   user_id Char( +) not NULL,7   user_name varchar( -)DEFAULT NULL,8User_birthday DateDEFAULT NULL,9User_salaryDouble DEFAULT NULL,Ten   PRIMARY KEY(user_id) One) ENGINE=InnoDBDEFAULTCHARSET=UTF8;
View Code

5. Execute the Build command

Java-jar C:\Users\dm1-10473\.m2\repository\org\mybatis\generator\mybatis-generator-core\1.3.2\ Mybatis-generator-core-1.3.2.jar-configfile Generator.xml-overwrite

Specifically, you can modify the command based on the path where the jar package is located, and I wrote a bat script to execute

6. The relevant files are automatically generated when the execution is complete.

This solves a lot of repetitive work.

Automatic generation of Table instance classes and mapping files using Mybatis-generator

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.