Mybatis在IDEA中使用generator逆向工程生成pojo,mapper

1.创建maven 工程,修改pom.xml

 

<build>
        <plugins>
            <!-- mybatis逆向工程 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--配置文件的位置-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
              </plugin>
         </plugins>
    </build>

 

2.在resources下新建   Personal-DB.propertie(数据库连接文件) (也可以写死)

   

jdbc.driverLocation=D:\\maven\\com\\oracle\\ojdbc14\\10.2.0.4.0\\ojdbc14-10.2.0.4.0.jar  
jdbc.driverClass=oracle.jdbc.driver.OracleDriver  
jdbc.connectionURL=jdbc:oracle:thin:@//localhost:1521/XE  
jdbc.userId=LOUIS  
jdbc.password=123456  

  

3.在resources下新建   generatorConfig.xml   文件 (内容如下)

<?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>
    <properties resource="Personal-DB.properties"></properties>
    <classPathEntry location="${jdbc.driverLocation}" />
    <!--classPathEntry location="D:\zngkpt\m2\repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" /-->
    <context id="context1" targetRuntime="MyBatis3">

        <commentGenerator>
            <!-- 去除自动生成的注释 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        
        <!-- 数据库连接配置 -->
        <jdbcConnection driverClass="${jdbc.driverClass}"
            connectionURL="${jdbc.connectionURL}"
            userId="${jdbc.userId}"
            password="${jdbc.password}" />
        <!--jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"
                        password="mysql" /-->

 <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--配置生成的实体包
            targetPackage:生成的实体包位置,默认存放在src目录下
            targetProject:目标工程名
         -->
        <javaModelGenerator targetPackage="com.unisits.zngkpt.common.userprivrman.pojo"
            targetProject="src/main/java" />
            
        <!-- 实体包对应映射文件位置及名称,默认存放在src目录下 -->
        <sqlMapGenerator targetPackage="com.unisits.zngkpt.common.userprivrman.mapper" targetProject="src/main/java" />

         <!-- mapper方法层,默认存放在src目录下 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.bldz.train.mapper" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 配置表 
            schema:不用填写
            tableName: 表名
            enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
            去除自动生成的例子
        -->
        <table schema="" tableName="sys_role" enableCountByExample="false" enableSelectByExample="false"
            enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
        </table>
        <table schema="" tableName="sys_permission" enableCountByExample="false" enableSelectByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
        </table>
        <table schema="" tableName="sys_role_permission" enableCountByExample="false" enableSelectByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
        </table>
        <table schema="" tableName="sys_user" enableCountByExample="false" enableSelectByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
        </table>
        <table schema="" tableName="sys_user_role" enableCountByExample="false" enableSelectByExample="false"
        enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
        </table>
        <table schema="" tableName="unit_info" enableCountByExample="false" enableSelectByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
        </table>
        <table schema="" tableName="unit_type" enableCountByExample="false" enableSelectByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" >
        </table>
    </context>
</generatorConfiguration>

3,到现在为止,所有的mybatis配置工作已经结束了,开始配置idea来运行生成pojo吧

点击菜单Run->Edit Configuration,然后在弹出窗体的左上角,如下,输入 mybatis-generator:generate -e

然后点击确定

 

 4.运行刚才编辑的maven,不出意外就会生成成功

  

 补充:最终 红色部分会生成 xml  对应method 的 mapper.java 文件 

转载:

    http://www.cnblogs.com/ningheshutong/p/6376970.html


 

猜你喜欢

转载自chuichuige.iteye.com/blog/2396956