Using the mapper dedicated MyBatis generator plugin

Source: Internet
Author: User

Executing MBG with Maven

Here's a complete example, mybatis-spring, as explained below.

One advantage of using Maven plugins is that you can refer to the attribute usage form in maven ${property} generatorConfig.xml .

First look at Maven's Pom.xml file (which shows only the relevant parts):

<properties>    <!--  MyBatis Generator--    <!--  Java interfaces and entity classes    --< Targetjavaproject>${basedir}/src/main/java</targetjavaproject>    <targetMapperPackage> Tk.mybatis.mapper.mapper</targetmapperpackage>    <targetModelPackage>tk.mybatis.mapper.model< /targetmodelpackage>    <!--  XML generation Path--    <TARGETRESOURCESPROJECT>${BASEDIR}/SRC /main/resources</targetresourcesproject>    <targetXMLPackage>mapper</targetXMLPackage>    <!--  Dependent version--    <mapper.version>1.0.0</mapper.version>    < Mysql.version>5.1.29</mysql.version></properties>

The above is properties part of the configuration in Pom.xml. Several paths and package names commonly used in the MBG configuration file are configured here. It also contains the version of generic Mapper and the version of the database JDBC driver.

The following is the MAVEN plugin configuration for MBG:

 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>        Mybatis-generator-maven-plugin</artifactid> <version>1.3.2</version> <configuration>        <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <depend encies> <dependency> <groupId>mysql</groupId> <artifactid>mysql-connector- java</artifactid> <version>${mysql.version}</version> </dependency> <depen dency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <v Ersion>${mapper.version}</version> </dependency> </dependencies></plugin> 

The MBG plugin is configured here, and the generatorConfig.xml path to the configuration file is configured. There are also two dependencies, namely the JDBC driver and the generic mapper (the MBG plugin is available).

Let's look at this generatorConfig.xml configuration file:

<?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><propertiesResource="Config.properties"/><contextId="Mysql"Targetruntime="Mybatis3simple"Defaultmodeltype="Flat"><propertyName="Beginningdelimiter"Value="`"/><propertyName="Endingdelimiter"Value="`"/><pluginType="${mapper.plugin}"><propertyName="Mappers"Value="${mapper. Mapper} "/></plugin><jdbcconnectiondriverclass="${jdbc.driverclass}"Connectionurl="${jdbc.url}"Userid="${jdbc.user}"password="${jdbc.password}"></jdbcConnection><javamodelgeneratorTargetpackage="${targetmodelpackage}"targetproject="${targetjavaproject}"/><sqlmapgeneratorTargetpackage= "${targetxmlpackage}" targetproject= "${targetresourcesproject} "/> <javaclientgenerator targetpackage= "${targetmapperpackage}" targetproject= "${targetjavaproject}"  Type= "Xmlmapper" /> <table tablename=< Span class= "s" > "%" > <generatedkey column=  "id" sqlstatement= "Mysql" identity=  "true" /> </table> </context>< Span class= "NT" ></GENERATORCONFIGURATION>           

You can see that most of the properties in this configuration file are ${} replaced with a form. With the <properties resource="config.properties"/> introduction of the config.properties property configuration, the file reads as follows:

# database Configuration Jdbc.driverclass = Com.mysql.jdbc.Driverjdbc.url = Jdbc:mysql://localhost:3306/testjdbc.user = Rootjdbc.password = #c3p0jdbc. maxpoolsize=50jdbc.minpoolsize=10jdbc.maxstatements=100jdbc.testconnection=true# Universal Mapper Configuration Mapper.plugin = Tk.mybatis.mapper.generator.MapperPluginmapper.Mapper = Tk.mybatis.mapper.common.Mapper

The purpose of using the configuration file is because this configuration is used in many places in the system, so it is convenient to use a property file for consistency.

In addition to referencing properties in the configuration file, the section also uses the properties in Pom.xml. This approach is more flexible to use.

Run

How does it work after it is configured?

The command-line window of the Pom.xml directory is executed mvn mybatis-generator:generate (provided the MVN is configured).

The generated code

The following are examples of automatically generated code, which can be found here in mybatis-spring.

First, the entity class UserInfo
Package Tk.mybatis.mapper.model;import javax.persistence.*, @Table (name = "User_info") public class UserInfo {    @Id    @Column (name = "Id")    @GeneratedValue (strategy = generationtype.identity)    private Integer Id;    /**     * User name */    private String username;    /**     * Password * */    private String password;    /**     * @return ID     *    /Public Integer getId () {        return Id;    }    /**     * @param ID     *    /public void setId (Integer id) {        this.id = ID;    }    /**     * Get username     *     * @return Username-user name *     /public    String GetUserName () {        return username;< c31/>}    /**     * Set user name     *     * @param username user name     *    /public void Setusername (String username) {        this.username = username;    }}

The code is too long, omitted part, full view:UserInfo

You can see that the comments generated here are meaningful, and the comments originate from the comments in the database table fields.

The contents of several annotations are also generated automatically here.

Second, mapper interface UserInfoMapper
Package Tk.mybatis.mapper.mapper;import Tk.mybatis.mapper.common.mapper;import Tk.mybatis.mapper.model.UserInfo; Public interface Userinfomapper extends mapper<userinfo> {}

The interface automatically inherits the configured Universal Mapper interface, which automatically contains generic entities.

Iii.. mapper.xml file UserInfoMapper.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" ><mapperNamespace="Tk.mybatis.mapper.mapper.UserInfoMapper"><resultmapId="Baseresultmap"Type="Tk.mybatis.mapper.model.UserInfo"><!--warning-@mbggenerated--<idcolumn="Id"property="id"Jdbctype="INTEGER"/><resultcolumn="Username"property="Username"Jdbctype="VARCHAR"/><resultcolumn="Password"property="Password"Jdbctype="VARCHAR"/><resultcolumn="Usertype"property="Usertype"Jdbctype="VARCHAR"/><resultcolumn="Enabled"property="Enabled"Jdbctype="INTEGER"/><resultcolumn="Realname"property= "realname" jdbctype= "VARCHAR" /> <result column= "QQ" property=  "QQ" jdbctype= "VARCHAR" />  <result column= "email" property= "email" Span class= "NA" >jdbctype= "VARCHAR" /> <result column= "tel" property= "tel"  Jdbctype= "VARCHAR" /> </resultmap> </mapper>               

The XML file contains only the mapping configuration for the entity resultMap .

Note: If you select the Pom.xml file in Eclipse, right-click the run As-->maven build...--> enter it in the Goals box:mybatis-generator:generate

If you enter the MAVEN command at the command line, note: This command must be running under the current project directory:

MVN mybatis-generator:generate

After the code is generated, the merit is farewell.

Using the mapper dedicated MyBatis generator plugin

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.