MyBatis Mastering (Chapter 5): MyBatis code generator

  

MyBatis Mastering (Chapter 5): MyBatis code generator

MyBatis Generator, abbreviations will be used hereinafter instead of MBG.

MBG rich configuration can be generated by different types of code, the code database table contains the corresponding entity classes, Mapper interface class, Mapper XML file Example and other objects,

The code file contains almost all of the single-table operation method. If you prefer to read Chinese documents, you can also view the organization of Chinese translation by the author documents the link is http: // 

MyBatis Generator solved have the greatest impact on database operations
some simple CRUD (insert, query, update, delete) operations,
you still need the United handwritten SQL and object queries and stored procedures.

 5 . 1 XML configuration in detail 

MBG has a wealth of configuration available, these need to be configured in the form of XML tags and attributes to achieve, so this section on XML configuration MBG of detail. 

First, in accordance with MBG add the required XML file header. After the header, you need to write on the root of the XML file <generatorConfiguration>

<?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>
    <!-- <classPathEntry location="F:\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/> -->
    
    <context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <property name="javaFileEncoding" value="UTF-8"/>

        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="addRemarkComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis"
                        userId="root"
                        password="">
        </jdbcConnection>

        <javaModelGenerator targetPackage="test.model" targetProject="src\main\java">
            <property name="trimStrings" value="true" />
            <property name="rootClass" value="tk.mybatis.simple.model.BaseEntity" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="test.xml"  targetProject="src\main\resources"/>

        <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao"  targetProject="src\main\java"/>

        <table tableName="%">
            <generatedKey column="id" sqlStatement="MySql"/>
        </table>
    </context>
</generatorConfiguration>

The following first describes  generatorConfiguration . 3 child of the tag label, namely: properties, classPathEntry, context. In this configuration when the label 3, the order must be strictly configured Examples of these tags.

  The first is the properties tab. This tag is used to specify an external property element, a maximum configuration, may not be configured.

  tag specifies properties external properties need to parse a file for use in the configuration file incoming properties, may be used in the configuration $ {property} referenced in this form, the attribute value of the attribute file. For the latter need to configure JDBC information would be useful.

 

 



 

 

 

 

 

===================

end

Guess you like

Origin www.cnblogs.com/MarlonKang/p/11495292.html