MyBatis how to automatically generate entity classes, mapper configuration files and DAO interfaces

Source: Internet
Author: User

1. First prepare the JAR package

https://github.com/mybatis/generator/releases download MyBatis Generator

After downloading the compressed package, open the jar package that you can see in the Lib directory that we need to add to the project reference

2. As with hibernate reverse generation, a configuration file is also required:

Generator.xml

<?XML version= "1.0" encoding= "UTF-8"?>      <!DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "Htt P://mybatis.org/dtd/mybatis-generator-config_1_0.dtd ">            <generatorconfiguration>    <!--The location of the database driver package, I'm here on the D drive. -        <Classpathentry Location= "D:\mysql-connector-java-5.1.0-bin.jar" />              <ContextID= "Mysql2tables"Targetruntime= "MyBatis3">      <!--database driver, connection URL, user name, password -      <jdbcconnectionDriverclass= "Com.mysql.jdbc.Driver"Connectionurl= "Jdbc:mysql://192.168.144.100:3306/networkdept"userId= "root"Password= "123456">          </jdbcconnection>                <Javatyperesolver>            < Propertyname= "Forcebigdecimals"value= "false" />          </Javatyperesolver>                <!--generate the package name and location of the entity class -       <JavamodelgeneratorTargetpackage= "Cn.networkdepartment.pojo"Targetproject= "src">            < Propertyname= "Enablesubpackages"value= "true" />            < Propertyname= "Trimstrings"value= "true" />          </Javamodelgenerator>                  <!--generate the package name and location of the DAO interface -    <SqlmapgeneratorTargetpackage= "Cn.networkdepartment.dao"Targetproject= "src">            < Propertyname= "Enablesubpackages"value= "true" />          </Sqlmapgenerator>                <!--generated map file package name and location -     <Javaclientgeneratortype= "Xmlmapper"Targetpackage= "Cn.networkdepartment.dao"Targetproject= "src">            < Propertyname= "Enablesubpackages"value= "true" />          </Javaclientgenerator>          <!--The tables to be built (change tablename and Domainobjectname) -        <TableSchema= "Test"TableName= "Nd_class"Domainobjectname= "NClass"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false">          </Table>              <TableSchema= "Test"TableName= "Nd_integral"Domainobjectname= "Integral"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false">         </Table>            <TableSchema= "Test"TableName= "Nd_log"Domainobjectname= "Nlog"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false">         </Table>       <TableSchema= "Test"TableName= "Nd_maintain"Domainobjectname= "Maintain"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false">         </Table>         <TableSchema= "Test"TableName= "Nd_member"Domainobjectname= "Member"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false">         </Table>       <TableSchema= "Test"TableName= "Nd_post"Domainobjectname= "Npost"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false">         </Table>       <TableSchema= "Test"TableName= "Nd_systemset"Domainobjectname= "Systemset"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false">         </Table>     </Context>      </generatorconfiguration>  

3. Create a class to generate code from the main method, run this class

 Packagetest; ImportJava.io.File; Importjava.io.IOException; Importjava.sql.SQLException; Importjava.util.ArrayList; Importjava.util.List; ImportOrg.mybatis.generator.api.MyBatisGenerator; Importorg.mybatis.generator.config.Configuration; ImportOrg.mybatis.generator.config.xml.ConfigurationParser; Importorg.mybatis.generator.exception.InvalidConfigurationException; Importorg.mybatis.generator.exception.XMLParserException; ImportOrg.mybatis.generator.internal.DefaultShellCallback; /*** Create test class, load configuration file to generate Dao,mapper file automatically *@authorBryce **/ Public classMybatisgeneratorutil { Public Static voidMain (string[] args) {Try{System.out.println ("Start generator ..."); List<String> warnings =NewArraylist<string>(); BooleanOverwrite =true; File ConfigFile=NewFile (Mybatisgeneratorutil.class. GetResource ("/generator.xml"). GetFile ()); Configurationparser CP=Newconfigurationparser (warnings); Configuration Config=cp.parseconfiguration (configfile); Defaultshellcallback Callback=Newdefaultshellcallback (overwrite); Mybatisgenerator Mybatisgenerator=Newmybatisgenerator (config, callback, warnings); Mybatisgenerator.generate (NULL); System.out.println ("End generator!"); } Catch(IOException e) {e.printstacktrace (); } Catch(xmlparserexception e) {e.printstacktrace (); } Catch(invalidconfigurationexception e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); }        }            }  

You can then see that both the Entity class and the DAO have generated

MyBatis how to automatically generate entity classes, mapper configuration files and DAO interfaces

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.