Automatic generation of entity classes and Mapper mapping files and interface files using MyBatis reverse

Source: Internet
Author: User

Problem Description:

Recently in the web, want to generate the entity class from the database, then manipulate the database, and then do some additions and deletions to work,

Discover that you can directly generate JavaBean directly using MyBatis, as well as mapping files that can generate mapper directly. Here's how:

Create a new MAVEN project:

(1) Add a jar package (3 dependencies below) to be dependent on the Pom file

<dependency>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-core</artifactId>

<version>1.3.5</version>

</dependency>
<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.38</version>

</dependency>
<!--Https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core--
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
<version>3.0</version>
</dependency>

(2) Create a generatorconfig.xml file in the root directory.

<?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>
<context id= "Testtables" targetruntime= "MyBatis3" >
<commentGenerator>
<!--whether to remove automatically generated comments true: Yes: false: no--
<property name= "Suppressallcomments" value= "true"/>
</commentGenerator>
<!--database connection information: Driver class, connection address, user name, password MySQL database connection--
<jdbcconnection driverclass= "Com.mysql.jdbc.Driver"
Connectionurl= "JDBC:MYSQL://ZHB-PC:3306/RRV" userid= "Report"
password= "Report" >
</jdbcConnection>
<!--Oracle Database connection--
<!--<jdbcconnection driverclass= "Oracle.jdbc.OracleDriver" connectionurl= "jdbc:oracle:thin:@127.0.0.1:1521 : YYCG "
Userid= "YYCG"
password= "YYCG" >
</jdbcConnection>

<!--default False to parse the JDBC decimal and NUMERIC types to Integer, true when JDBC decimal and
NUMERIC type resolves to java.math.BigDecimal--
<javaTypeResolver>
<property name= "Forcebigdecimals" value= "false"/>
</javaTypeResolver>

<!--targetproject: Generate POS class Location--
<javamodelgenerator targetpackage= "Com.report.pojo"
targetproject= "Src/main/java" >
<!--enablesubpackages: Do you want the schema to be a suffix of the package--
<property name= "Enablesubpackages" value= "false"/>
<!--the value returned from the database is cleared before and after the space-
<property name= "Trimstrings" value= "true"/>
</javaModelGenerator>
<!--targetproject:mapper Map file generated location--
<sqlmapgenerator targetpackage= "Com.itheima.mapper"
targetproject= "Src/main/resources" >
<!--enablesubpackages: Do you want the schema to be a suffix of the package--
<property name= "Enablesubpackages" value= "false"/>
</sqlMapGenerator>
<!--Targetpackage:mapper Interface generated Location--
<javaclientgenerator type= "Xmlmapper"
Targetpackage= "Com.report.dao"
targetproject= "Src/main/java" >
<!--enablesubpackages: Do you want the schema to be a suffix of the package--
<property name= "Enablesubpackages" value= "false"/>
</javaClientGenerator>
<!--specifying database tables--
<!--<table tablename= "Items" ></table>-
<table tablename= "rule_conf" ></table>
<table tablename= "Rule_run_result" ></table>
<!--<table tablename= "user" ></table>-
<!--<table schema= "" Tablename= "Sys_user" ></table>
<table schema= "" Tablename= "Sys_role" ></table>
<table schema= "" Tablename= "Sys_permission" ></table>
<table schema= "" Tablename= "Sys_user_role" ></table>
<table schema= "" Tablename= "Sys_role_permission" ></table>-

<!--some table fields need to specify a Java type
<table schema= "" Tablename= "" >
<columnoverride column= "" javatype= ""/>
</table>
</context>
</generatorConfiguration>

(3) write a main class and run it to build.

Import Java.io.File;
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.internal.DefaultShellCallback;
public class Generate {
public void Generator () throws exception{
list<string> warnings = new arraylist<string> ();
Boolean overwrite = true;
File ConfigFile = new file ("Generatorconfig.xml"); The file created in the second step of the root directory.
Configurationparser cp = new Configurationparser (warnings);
Configuration config = cp.parseconfiguration (configfile);
Defaultshellcallback callback = new Defaultshellcallback (overwrite);
Mybatisgenerator mybatisgenerator = new Mybatisgenerator (config,
callback, warnings);
Mybatisgenerator.generate (NULL);
}
public static void Main (string[] args) throws Exception {
try {
Generate Generatorsqlmap = new Generate ();
Generatorsqlmap.generator ();
} catch (Exception e) {
E.printstacktrace ();
}
}
}

Summary: Packages defined in the XML file do not need to be created manually, and will be generated directly at execution time.

Automatic generation of entity classes and Mapper mapping files and interface files using MyBatis reverse

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.