Mybatis -代码自动生成(generatorConfig.xml)

对于一个字段太多的表,如果一个一个的去写 mapper和pojo太累还容易出错,可用mybatis的工具处理这些事情

参考:http://blog.csdn.net/jinshiyill/article/details/51546676
官方网址: http://www.mybatis.org/generator/configreference/xmlconfig.html
原码:http://download.csdn.net/detail/wangxy799/9773975

1. 环境:

Maven 3.39
Oracle 11g
Mybatis 3.4.1
Maven project

2. 目录结构

Mybatis -代码自动生成(generatorConfig.xml)_第1张图片

3. pom添加插件

<dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.1version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>
    dependencies>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>
            plugin>
        plugins>
    build>

4. 新建generatorConfig.xml 文件(放在src/main/resources目录下)

  


<generatorConfiguration>
          
        <properties resource="application.properties"/>

        
        <classPathEntry
            location="${generator.location}" />

         
        <context id="sqlserverTables">
            
            <plugin type="org.mybatis.generator.plugins.SerializablePlugin">plugin>

            
            <commentGenerator>
                <property name="suppressAllComments" value="true" />
                
            commentGenerator>  

            
            <jdbcConnection driverClass="${db.example.driver}"
                connectionURL="${db.example.url}" userId="${db.example.username}" 
                password="${db.example.password}">
            jdbcConnection>

            
            <javaTypeResolver>
            
                <property name="forceBigDecimals" value="false" />
            javaTypeResolver>

            
            <javaModelGenerator targetPackage="${generator.targetPackage}"
                targetProject="./src/main/java">
                 
                <property name="enableSubPackages" value="false" />
                
                <property name="trimStrings" value="true" />
            javaModelGenerator>

            
            <sqlMapGenerator targetPackage="${generator.targetPackage}"
                targetProject="./src/main/java">
                <property name="enableSubPackages" value="false" />
            sqlMapGenerator>

            
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="${generator.targetPackage}" targetProject="./src/main/java">
                <property name="enableSubPackages" value="false" />
            javaClientGenerator>


            
              
            <table tableName="${gererator.tableName}" domainObjectName="${gererator.objectName}"
                schema="${gererator.schema}"
                enableCountByExample="false" enableUpdateByExample="false"
                enableDeleteByExample="false" enableSelectByExample="false"
                selectByExampleQueryId="false">
                   
                
                
                <property name="useActualColumnNames" value="false" />
            table>
        context>
    generatorConfiguration>    

5. 设置需要生成的表及创建目录参数(application.properties放在src/main/resources目录下)

db.example.type=oracle
db.example.driver=oracle.jdbc.driver.OracleDriver
db.example.url=jdbc:oracle:thin:@ip:port:sid
db.example.username=username
db.example.password=password

#MBGInfo
generator.location=D:/project/eas_std/BaseApp/lib/ojdbc14-10.2.0.5.jar
generator.targetPackage=com.zteict.example.menu
gererator.schema=fbp
gererator.tableName=fbp_menu
gererator.objectName=FbpMenu

6. 项目右键-RUN AS-MAVEN BUILD..

输入 mybatis-generator:generate
Mybatis -代码自动生成(generatorConfig.xml)_第2张图片

7. 生成的文件

FbpMenu:
Mybatis -代码自动生成(generatorConfig.xml)_第3张图片
FbpMenuMapper:
Mybatis -代码自动生成(generatorConfig.xml)_第4张图片
Xml:
Mybatis -代码自动生成(generatorConfig.xml)_第5张图片

你可能感兴趣的:(spring+mybatis)