eclipse下利用mybatis-generator以及连接oracle数据库自动生成接口,mapper,以及model

我们都知道现在的项目下很多的实体类的属性比较多,自己手动写的话相当的浪费时间,所以下面介绍如果自动生成实体类,以及接口层的几个免费方法和对应的mapper文件,话不多说,上代码:

1:首先肯定是构建项目,new一个maven项目,打开pom文件

<build>
        <plugins>
            
            <plugin>
                  <groupId>org.mybatis.generator</groupId>
                  <artifactId>mybatis-generator-maven-plugin</artifactId>
                 <version>1.3.2</version>
                  <configuration>
                     <verbose>true</verbose>
                      <overwrite>true</overwrite>
                  </configuration>
             </plugin>
            
        </plugins>
    </build>

在pom文件中添加插件

 

2:在src/main/resources下加入generateConfig.xml 代码:

其中generateConfig.xml文件通过右击项目/new/other/找到mybatis/点开选中bybatis generator Configuration file /Next/finish即可生成

<?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="E:\CodeExamples\ssm20171129\WebContent\WEB-INF\lib\ojdbc6.jar" />  //这个是数据库的驱动包的完成路径oracle就是oracle的驱动包,反之,就用mysql的驱动包
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
            connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"   //这是连接数据库的相关信息,若用mysql数据库用mysql的连接信息即可
            userId="scott" password="tiger">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!--域模型层,生成的目标包,项目目标源文件 -->
        <javaModelGenerator targetPackage="com.transmateSchool.www.domain"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--XML映射文件,生成的位置(目标包),源代码文件夹 -->
        <sqlMapGenerator targetPackage="sqlmap" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!--XML对应的Mapper类 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.mybatis.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!--下面是数据库表名和项目中需要生成类的名称,建议和数据库保持一致,如果有多个表,添加多个节点即可 -->

        <table tableName="emp" domainObjectName="emp"
            enableCountByExample="false" enableSelectByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false">

        </table>
        
        <table tableName="dept" domainObjectName="dept"
            enableCountByExample="false" enableSelectByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false">

        </table>
    </context>

</generatorConfiguration>

3:右击项目/run  as /maven  build/  在goals中输入mybatis-generator:generate  /apply /run即可

 

posted @ 2018-05-21 16:22  性感的小辣椒  阅读(6246)  评论(0编辑  收藏  举报