In the MyEclipse Maven environment, using mybatis-generator automatically generates a map file widget (interface) and the entity class

 In the case of relatively large number of data tables, write sql manually mapping files and entity classes, too much too cumbersome, and mybatis-generator can automatically generate need these things, reduced duplication of effort. mybatis-generator configuration prone to problems, although more online tutorials, but I have tried many times before the configuration is successful, I will publish their own pro-test configuration process out, try to make everyone in one step, without debugging, change to change go with.

1. Import of dependencies in pom.xml

<-! MyBatis mapping entity class and sql generation tool automatically JAR -> 
        < dependency > 
            < the groupId > org.mybatis.generator </ the groupId > 
            < the artifactId > MyBatis-Generator-Core </ the artifactId > 
            < Version > 1.3. 2 </ Version > 
            < type > JAR </ type > 
        </ dependency > 
        <-! JDBC database connectivity package -> 
        < dependency > 
            < the groupId >mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <!-- maven环境中的mybatis.generator插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</the artifactId > 
                < Version > 1.3.3 </ Version > 
                < Configuration > 
                <-! specified mybatis-generator configuration file location. 
                If the configuration item is omitted, the default lookup uses 'src / main / resources' under 'generatorConfig.xml' profile. 
                I.e., if the location is not specified profile, 'src / main / resources / generatorConfig.xml' not exist, 
                an error is reported to exit.    -> 
                    < configurationFile > the src / main / Resources / MyBatis-Generator / generatorConfig.xml </ configurationFile > 
                    < verbose > to true </ verbose > 
                    <!-->
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
        
    </build>

 

2. Create a database profile database.properties resources in the resource directory

= com.mysql.jdbc.Driver Driver 
# mysql and during transfer of data, a unicode encoding format, and character set to. 8-UTF 
URL = JDBC: mysql: //127.0.0.1:? 3306 / lottery_db useUnicode = to true & characterEncoding = UTF-. 8 & serverTimezone = UTC 
User = the root 
password = QWERT

 

3. Write mybatis-generator configuration file

Create a directory mybatis-generator in the resources resource directory, create a mybatis-generator configuration files in this directory generatorConfig.xml (with the pom.xml mybatis-generator-maven-plugin plugin consistent path configurationFile label set )

<? Xml Version = "1.0" encoding = "UTF-8" ?> 
<! DOCTYPE generatorConfiguration the PUBLIC "- // mybatis.org//DTD the Configuration MyBatis Generator 1.0 // EN" "http://mybatis.org/dtd /mybatis-generator-config_1_0.dtd " > 
< generatorConfiguration > 
    <-! introduction of database configuration files. -> 
    <! - regardless of the current position of generatorConfig.xml, with only about database.properties position if it database.properties in resources directory, direct. 
        Can write resource = "database.properties" directly, regardless of generatorConfig .xml exists in the resources directory or subdirectories resources. 
        Because we generate a map file with the command, but does not make the project up and running, if written as "resource = 'classpath: database.properties' " 
        will generate an error when mapping file with the command (the project is not running does not exist 'classpath' this statement).
         If database.properties file in a subdirectory xxx resources, where they write "Resource = 'xxx / database.properties'" ->
     commentGenerator< Properties Resource = "the database.properties"  /> 
    <! - JDBC Kit -> 
    <! - must be configured to specify the position of the jar package database connector -> 
    < classpathentry
         LOCATION = "E: \ maven_repository \ MySQL \ MySQL Java--connector \ 5.1.47 \ MySQL-Connector-Java-5.1.47.jar "  /> 
        
    <-! a context database a context -> 
    < context ID =" mapper_lottery_db " targetRuntime =" MyBatis 3 " > <! - specify mybatis framework version -> 
    
        < > < Property name = "of the type" value="DEFAULT"/>
             
            <! - whether to remove the automatically generated annotations true: is: to false: NO -> 
            < Property name = "suppressAllComments" value = "true"  /> 
            <! - This element is used to specify annotation remove generated contains date generated false: represents protection -> 
            <! - If the creation date, modification will cause even a field, all attributes of the class will change the entire entity, is not conducive to version control, so set to true -> 
            < property name = "suppressDate" value = "to true"  /> 
        </ commentGenerator > 

        <-! by unsigned US references database.properties the jdbc connection parameter -> 
        < JdbcConnection driverClass = "$ {Driver}" connectionURL="${url}"
            userId= "$} {User" password = "{$ password}"  /> 
            
        <-! generating entity classes -> 
        <-! targetPackage package entity classes represent placed, this package can not otherwise exist in the project, not present generator will automatically build the first package, to build entity classes 
             targetProject package represented position -> 
        < javaModelGenerator targetPackage = "cn.lottery.pojo" 
            targetProject = "the src / main / Java" > 
            <-! whether a new path in the current add a layer of Schema, 
            EG: Fase path cn.lottery.pojo, to true: cn.lottery.pojo [schemaName]. -> 
            < Property name= "enableSubPackages" value = "false"  /> 
            <-! whether to trim call set the time for the string type of field ->
            < Property name = "trimStrings" value = "false"  /> 
        </ javaModelGenerator > 

        <-! SQL mapping file -> 
        < sqlMapGenerator targetPackage = "mappers" 
            targetProject = "src / main / Resources" > 
            < Property name = " enableSubPackages " value =" false "  /> 
        </ sqlMapGenerator > 
        
        <-! Mapper Interface -> 
        <-!      of the type =" XMLMAPPER | ANNOTATEDMAPPER ", respectively, in order to xml or annotation to map SQL statement   ->
        < javaClientGenerator targetPackage = "cn.lottery.mapper" 
            of the type = "ANNOTATEDMAPPER" targetProject = "src / main / the Java " > 
            < Property name =" trimStrings " value =" to false "  /> 
        </ javaClientGenerator > 
        
        
        <! - required SQL statement data table -> 
        
        <! - a table a <table> tag, schema represents the database name, tableName table name indicates, domainObjectName entity represents the generated class name -> 
        <-! enableXXXXByExample set to true, will generate "pojonameXXXXByExample "entity class. 
        the generation of such principal entity SQL for complex operations, such as: sorting, de-duplication, greater than / less than the query matching section, these generally do not need, 
        even if it requires these operations Sql, there was no need to generate a new entity class     -> 
        < Table Schema = "lottery_db" tableName="admin_user"
            domainObjectName = "the AdminUser" enableCountByExample = "to false" 
            enableUpdateByExample = "to false" enableDeleteByExample = "to false" 
            enableSelectByExample = "to false" selectByExampleQueryId = "to false" > 
            <-! By default, the field names and the name of the same attribute of the entity type, where may be omitted -> 
            < property name = "useActualColumnNames" value = "to true"  /> 
            <-! If you want the field names and attribute names are not exactly correspond, the label may be provided on columnOverride Related properties   -> 
        <-!      <columnOverride column = "login_name" property = "loginName"/> -->
        </table>
         
     ...... ......       </ context > </ generatorConfiguration >
    

 

4. The command to generate the required file

Many are using online tutorials been run as maven generated documents, but in my environment myeclipse always run as an error, only use the command line "mvn clean mybatis-generator: generate" finally successfully generated the relevant file.

 

myeclipse comes with a command-line terminal, in the upper left corner click on the icon to display terminal.

image

After the terminal is opened, the default working directory in the root directory of the current project. If not in the root directory of the project, the project will have to switch to the root directory, use the "dir" command in this directory to be able to see "pom.xml" file.

image

Then enter "mvn clean mybatis-generator: generate", the output terminal when displaying green see "BUILD SUCCESS" successfully generated map file mybatis the relevant sql.

image

Finally, press the "F5" key to refresh in the current project, you can see the interface, the entity class and map files generated sql

image

Guess you like

Origin www.cnblogs.com/gocode/p/usage-of-mybatis-generator.html