Use MAVEN reverse engineering to generate PO Classes and mapper (MyBatis) in eclipse

Source: Internet
Author: User
Tags generator numeric trim
1, add the following code in Pom.xml project>build, let MAVEN environment support Mybatis-generator component

<pluginManagement> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> &lt ;configuration> <configurationFile>src/main/resources/generator.xml</configurationFile> <ver bose>true</verbose> <overwrite>true</overwrite> </configuration> <executions > <execution> <id>generate MyBatis artifacts</id> <goals> <goal& 
						gt;generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactid>mybatis-gen erator-core</artifactid> <version>1.3.2</version> </dependency> </dependencie S> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> &LT;ARTIFACTID&GT;MAVEN-SUREFIRE-PLUGIN&L t;/artifactid> <version>2.19.1</version> <configuration> <skiptests>true</sk iptests> </configuration> </plugin> <plugin> <groupid>org.apache.maven.plugi Ns</groupid> <artifactId>maven-resources-plugin</artifactId> <version>3.0.1</version&
					Gt <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> < /plugins> </pluginManagement>

Note: If you have already introduced Mysql-connector-java in dependencies, you do not need to add the following dependency, otherwise add
<dependency>  
                        <groupId>mysql</groupId>  
                        <artifactid>mysql-connector-java</ artifactid>  
                        <version>5.1.35</version>  
                        <scope>runtime</scope>  
                    </ Dependency>
2. generator.xml configuration file

Tables to be reversed:


Reverse Code Generation directory structure:


Corresponding configuration:


<?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 Driver package Location--< Classpathentry location= "E:\apache-maven-3.3.9\repo\mysql\mysql-connector-java\5.1.18\mysql-connector-java-5.1. 18.jar "/> <context id=" Tables "targetruntime=" MyBatis3 "> <commentGenerator> <!--whether to remove auto-generated annotation tr UE: Yes: false: No--<property name= "suppressallcomments" value= "true"/> </commentGenerator> <!-- Database link URL, user name, password--<jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "jdbc:mysql:// 192.168.100.52:3306/dev_test "userid=" Database user name "password=" Database Password "> <!--<jdbcconnection driverclass=" Oracle.jdbc.driver.OracleDriver "Connectionurl=" Jdbc:oracle:thin: @localhost: 1521:orcl "userid=" MSA "password=" MSA "> </Jdbcconnection> <javaTypeResolver> <!--default false to resolve JDBC DECIMAL and NUMERIC types to Integer, true when JDBC Decim Al and numeric types resolved to Java.math.BigDecimal--<property name= "Forcebigdecimals" value= "true"/> </javatypereso Lver> <!--generate the package name and location of the entity class, where you configure the generated entity class to be placed com.loan.test.entity this package--<javamodelgenerator targetpackage= " Com.loan.test.entity "targetproject=". \src\main\java\ "> <!--enablesubpackages: Do you want the schema to be the suffix of the package--<p Roperty name= "Enablesubpackages" value= "true"/> <!--the value returned from the database is cleared before and after the space--<property name= "Trimstrings" Value= "true"/> </javaModelGenerator> <!--generated SQL Map file package name and location, here configuration will generate SQL map file placed under Com.loan.test.dao.xml this package- -<sqlmapgenerator targetpackage= "Com.loan.test.dao.xml" targetproject= "\src\main\java\" > <!--enabl Esubpackages: Whether to have schema as the package suffix--<property name= "Enablesubpackages" value= "true"/> </sqlmapgenerator&gt
		; <!--generate the DAO's package name and location, where you configure the DAO that will be generatedClass is placed under Com.loan.test.dao.mapper this package--<javaclientgenerator type= "Xmlmapper" targetpackage= " Com.loan.test.dao.mapper "targetproject=". \src\main\java\ "> <!--enablesubpackages: Do you want the schema to be the suffix of the package--&lt ;p roperty name= "Enablesubpackages" value= "true"/> </javaClientGenerator> <!--to generate those tables ( Change TableName and Domainobjectname as you like)-<table tablename= "test" domainobjectname= "test" enablecountbyexample= "
			False "enableupdatebyexample=" false "enabledeletebyexample=" false "enableselectbyexample=" false " Selectbyexamplequeryid= "false"/> </context> </generatorConfiguration>

3. Using maven command to generate reverse engineering

Project Right-->run as->maven build...,goals: Enter Mybatis-generator:generate

. 4, refresh the project, results


1) Test.java

Package com.loan.test.entity;

Import Java.math.BigDecimal;

public class Test {
    private Integer ID;

    Private String userName;

    Private Integer cardId;

    Private BigDecimal Moneyamount;

    Public Integer getId () {
        return ID;
    }

    public void SetId (Integer id) {
        this.id = ID;
    }

    Public String GetUserName () {
        return userName;
    }

    public void Setusername (String userName) {
        This.username = UserName = = null? Null:userName.trim ();
    }

    Public Integer Getcardid () {
        return cardId;
    }

    public void Setcardid (Integer cardId) {
        this.cardid = cardId;
    }

    Public BigDecimal Getmoneyamount () {
        return moneyamount;
    }

    public void Setmoneyamount (BigDecimal moneyamount) {
        this.moneyamount = Moneyamount;
    }
}
2) Testmapper.java

Package com.loan.test.dao.mapper;

Import Com.loan.test.entity.Test;

Public interface Testmapper {
    int deletebyprimarykey (Integer ID);

    int Insert (Test record);

    int insertselective (Test record);

    Test Selectbyprimarykey (Integer ID);

    int updatebyprimarykeyselective (Test record);

    int Updatebyprimarykey (Test record);
}
3) Testmapper.xml

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < Mapper namespace= "Com.loan.test.dao.mapper.TestMapper" > <resultmap id= "baseresultmap" type= " Com.loan.test.entity.Test "> <id column=" id "jdbctype=" INTEGER "property=" id "/> <result column=" User_n Ame "jdbctype=" VARCHAR "property=" UserName "/> <result column=" card_id "jdbctype=" INTEGER "property=" CardId "/&G
    T <result column= "Money_amount" jdbctype= "DECIMAL" property= "Moneyamount"/> </resultMap> <sql id= "Base_ Column_list "> ID, user_name, card_id, Money_amount </sql> <select id=" Selectbyprimarykey "Parametertyp E= "Java.lang.Integer" resultmap= "Baseresultmap" > select <include refid= "Base_column_list"/> from Te St WHERE id = #{id,jdbctype=integer} </select> <delete id= "Deletebyprimarykey" parametertype= "Java.lang".Integer "> Delete from Test where id = #{id,jdbctype=integer} </delete> <insert id=" Insert "Paramet Ertype= "Com.loan.test.entity.Test" > INSERT into Test (ID, user_name, card_id, Money_amount) VALUES (#{id , Jdbctype=integer}, #{username,jdbctype=varchar}, #{cardid,jdbctype=integer}, #{moneyamount,jdbctype=decimal}) &L t;/insert> <insert id= "insertselective" parametertype= "Com.loan.test.entity.Test" > insert INTO Test &lt
      Trim prefix= "(" suffix= ")" suffixoverrides= "," > <if test= "id! = NULL" > ID, </if> <if test= "UserName! = null" > user_name, </if> <if test= "CardId! = NULL" > CA rd_id, </if> <if test= "Moneyamount! = null" > Money_amount, </if> </trim > <trim prefix= "Values (" suffix= ")" suffixoverrides= "," > <if test= "id! = NULL" > #{id,jdb Ctype=integer}, &Lt;/if> <if test= "UserName! = null" > #{username,jdbctype=varchar}, </if> <if te
        St= "CardId! = null" > #{cardid,jdbctype=integer}, </if> <if test= "Moneyamount! = null" > #{moneyamount,jdbctype=decimal}, </if> </trim> </insert> <update id= "UPDATEBYPR Imarykeyselective "parametertype=" com.loan.test.entity.Test "> Update test <set> <if test=" Userna 
        me = null "> user_name = #{username,jdbctype=varchar}, </if> <if test=" CardId! = null "> card_id = #{cardid,jdbctype=integer}, </if> <if test= "Moneyamount! = null" > Money _amount = #{moneyamount,jdbctype=decimal}, </if> </set> where id = #{id,jdbctype=integer} < /update> <update id= "Updatebyprimarykey" parametertype= "com.loan.test.entity.Test" > Update test set use R_name = #{username,jdbcType=varchar}, card_id = #{cardid,jdbctype=integer}, Money_amount = #{moneyamount,jdbctype=decimal} where
    id = #{id,jdbctype=integer} </update> <resultmap id= "Baseresultmap" type= "Com.loan.test.entity.Test" > <id column= "id" jdbctype= "INTEGER" property= "id"/> <result column= "user_name" jdbctype= "VARCHAR" property= "u Sername "/> <result column=" card_id "jdbctype=" INTEGER "property=" CardId "/> <result column=" Money_amo Unt "jdbctype=" DECIMAL "property=" Moneyamount "/> </resultMap> <sql id=" base_column_list "> ID, user _name, card_id, Money_amount </sql> <select id= "Selectbyprimarykey" parametertype= "Java.lang.Integer" result map= "Baseresultmap" > select <include refid= "Base_column_list"/> from test where id = #{id,jdbct Ype=integer} </select> <delete id= "Deletebyprimarykey" parametertype= "Java.lang.Integer" > Delete from Test where id = #{id,Jdbctype=integer} </delete> <insert id= "Insert" parametertype= "Com.loan.test.entity.Test" > INSERT INTO Test (ID, user_name, card_id, Money_amount) VALUES (#{id,jdbctype=integer}, #{username,jdbctype=varchar}, #{ca Rdid,jdbctype=integer}, #{moneyamount,jdbctype=decimal}) </insert> <insert id= "Insertselective" Parame
      Tertype= "Com.loan.test.entity.Test" > insert INTO Test <trim prefix= "(" suffix= ")" suffixoverrides= "," > 
      &LT;IF test= "id! = NULL" > ID, </if> <if test= "UserName! = null" > User_name, </if> <if test= "CardId! = null" > card_id, </if> <if test= "Moneyamoun T! = null "> Money_amount, </if> </trim> <trim prefix=" VALUES ("suffix=") "Suffixo Verrides= "," > <if test= "id! = NULL" > #{id,jdbctype=integer}, </if> <if test= "u
   Sername! = NULL ">     #{username,jdbctype=varchar}, </if> <if test= "CardId! = null" > #{cardid,jdbctype=inte GER}, </if> <if test= "Moneyamount! = null" > #{moneyamount,jdbctype=decimal}, </if > </trim> </insert> <update id= "updatebyprimarykeyselective" parametertype= "com.loan.test.entity . Test "> Update test <set> <if test=" UserName! = null "> user_name = #{username,jdbctype =varchar}, </if> <if test= "CardId! = null" > card_id = #{cardid,jdbctype=integer}, &L t;/if> <if test= "Moneyamount! = null" > Money_amount = #{moneyamount,jdbctype=decimal}, </i f> </set> where id = #{id,jdbctype=integer} </update> <update id= "Updatebyprimarykey" Parame Tertype= "com.loan.test.entity.Test" > Update test set user_name = #{username,jdbctype=varchar}, card_id = #{cardid,jdbctype=integER}, Money_amount = #{moneyamount,jdbctype=decimal} where id = #{id,jdbctype=integer} </update> </map Per>

Configuration complete:

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.