mybatis oracle 使用mybatis-generator生成bean、mapper、dao

1、非maven的方式:

一、首先使用eclipse新建一個java項目

java項目

二、而後添加mybatis-generator的jar包和oracle的驅動包,我這分別使用mybatis-generator-core-1.3.5.jar和ojdbc6-11.2.0.1.0.jar ,兩個jar分別去官網下載便可,注意oracle的驅動包去官網下載的教程參見(> https://my.oschina.net/Sheamus/blog/1563614)

項目jar包

三、編寫mybatis-generator的配置文件

<?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="ojdbc6-11.2.0.1.0.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--數據庫連接地址帳號密碼-->
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@10.31.2.153:1521:orcl" userId="alarms" password="alarms">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model類存放位置-->
        <javaModelGenerator targetPackage="com.boco.model" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.boco.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao類存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.boco.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成對應表及類名-->
        <table tableName="DM_ALARM_TB" domainObjectName="Alarm" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_CHECKED_TABLES_TB" domainObjectName="CheckedTables" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_NORM_RULE_TB" domainObjectName="NormRule" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_NORM_TB" domainObjectName="Norm" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_RULE_ALARM_TB" domainObjectName="RuleAlarm" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_TASK_INFO_TB" domainObjectName="TaskInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_UNIFIED_ALARM_TB" domainObjectName="UnifiedAlarm" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

上面的配置文件都有註解,按照上面的配置就能夠了。java

四、最後配置完成以後到項目的目錄使用命令生成代碼:

jar包命令

java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite

命令成功 估計這個過程要等一下,完成以後刷新一下項目就能夠看到生成的代碼 。sql

2、使用maven的方式生成代碼:

一、建立一個maven項目

maven項目 注意:eclipse建立maven項目可能要修改一下builder path裏面的jdk版本,這樣纔會生成相應的maven項目目錄,因此新建完成以後修改一下就能夠了。 build path 我這裏的JDK是1.8,因此修改一下。數據庫

二、配置pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.boco.generator</groupId>
  <artifactId>generatorMaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>generatorMaven</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
	 <dependency>
	 	<groupId>com.oracle</groupId>
	 	<artifactId>ojdbc6</artifactId>
	 	<version>11.2.0.1.0</version>
	 </dependency>
      <dependency>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-core</artifactId>
          <version>1.3.5</version>
      </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
   <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                    <version>3.3</version>
                </plugin>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.5</version>
                    <dependencies>
                        <dependency>
                            <groupId>com.oracle</groupId>
                           	<artifactId>ojdbc6</artifactId>
	 						<version>11.2.0.1.0</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                         <!--配置文件的路徑-->
                         <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> 
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
  
</project>

三、在maven項目的resources目錄添加配置文件

maven的generator

<?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>
    <context id="test" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>  
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> 
         <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> 
        <commentGenerator>
            <!-- 這個元素用來去除指定生成的註釋中是否包含生成的日期 false:表示保護 -->
            <!-- 若是生成日期,會形成即便修改一個字段,整個實體類全部屬性都會發生變化,不利於版本控制,因此設置爲true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!--數據庫連接URL,用戶名、密碼 -->
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
            connectionURL="jdbc:oracle:thin:@10.31.2.153:1521:orcl" userId="alarms" password="alarms">
            </jdbcConnection>
        <javaTypeResolver>
            <!-- This property is used to specify whether MyBatis Generator should 
                force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.boco.pojo"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="com.boco.mapping"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.boco.dao" implementationPackage="com.boco.dao.impl"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        
        <!-- 要生成哪些表 -->
        <table tableName="DM_ALARM_TB" domainObjectName="Alarm" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_CHECKED_TABLES_TB" domainObjectName="CheckedTables" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_NORM_RULE_TB" domainObjectName="NormRule" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_NORM_TB" domainObjectName="Norm" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_RULE_ALARM_TB" domainObjectName="RuleAlarm" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_TASK_INFO_TB" domainObjectName="TaskInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="DM_UNIFIED_ALARM_TB" domainObjectName="UnifiedAlarm" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

    </context>
</generatorConfiguration>

四、執行maven命令

maven命令 命令 命令:apache

mybatis-generator:generate

成功

五、刷新項目就能夠在src/main/java目錄下看到生成的代碼。

相關文章
相關標籤/搜索