在myeclipse中示例

一:ibatis自动生成代码

安装下面的插件:http://ibatis.apache.org/tools/abator

配置文件如下:abatorConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >
<abatorConfiguration >
  <abatorContext >
  <!--  数据库连接的信息:驱动类、连接地址、用户名、密码-->
    <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@192.168.4.136:1521:sgmparts" userId="flycartest" password="flycartest" >
  <!--classPathEntry 数据库的JDBC驱动的jar包地址-->
    <classPathEntry location="E:\MyWorkSpace\flycar-parent\web-demo\lib\ojdbc6.jar" />
    </jdbcConnection>
	<!--生成java实体类对应的项目以及所在的包-->
    <javaModelGenerator targetPackage="test.model" targetProject="web-demo" />
    <!--生成mapper.xml对应的项目以及所在的包-->
    <sqlMapGenerator targetPackage="test.mapper" targetProject="web-demo" />
    <!--生成dao层java类对应的项目以及所在的包-->
    <daoGenerator targetPackage="test.dao" targetProject="web-demo" type="GENERIC-CI" />
	<!--数据相关表配置,schema即为数据库名,tableName为表名,domainObjectName是要生成的实体类-->
    <table schema="" tableName="Student" domainObjectName="Student">    </table>
  </abatorContext>
</abatorConfiguration>
然后右键单击选择

这样就OK了,自动生成了相关代码


二:mybatis自动生成代码

首先配置maven插件:pom.xml

       <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>

然后是配置文件: 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>  
	<!--    <classPathEntry location="E:\MyWorkSpace\flycar-parent\web-demo\lib\ojdbc6.jar" />  -->
    <!-- classPathEntry:数据库的JDBC驱动的jar包地址--> 
    <classPathEntry location="E:\MyWorkSpace\mysql-connector-java-5.1.6-bin.jar" />  
  
    <context id="DB2Tables" targetRuntime="MyBatis3">  
  
        <commentGenerator>  
            <property name="suppressDate" value="true" />  
<!--             是否去除自动生成的注释 true:是 : false:否   -->
   			<property name="suppressAllComments" value="true" />
        </commentGenerator>  
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> 
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
            connectionURL="jdbc:mysql://127.0.0.1:3306/develop" userId="develop" password="develop">  
        </jdbcConnection>  

        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false" />  
        </javaTypeResolver>  
  
        <javaModelGenerator targetPackage="com.raxtone.flycar.admin.entity"  
            targetProject="E:\MyWorkSpace\flycar-parent\web-demo\src\main\java"> 
            <!-- enableSubPackages:是否让schema作为包的后缀 -->       
      		<property name="enableSubPackages" value="true" />  
   			<!-- 从数据库返回的值被清理前后的空格  -->   
      		<property name="trimStrings" value="true" />   
        </javaModelGenerator>  
  
        <sqlMapGenerator targetPackage="com.raxtone.flycar.admin.mapper"  
            targetProject="E:\MyWorkSpace\flycar-parent\web-demo\src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->   
            <property name="enableSubPackages" value="false" />  
        </sqlMapGenerator>  
  
        <javaClientGenerator type="XMLMAPPER"  
            targetPackage="com.raxtone.flycar.admin.mapper" targetProject="E:\MyWorkSpace\flycar-parent\web-demo\src\main\java">  
            <!-- enableSubPackages:是否让schema作为包的后缀 --> 
            <property name="enableSubPackages" value="true" />  
        </javaClientGenerator>  
        <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->

	  <!--  schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类,
			如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true,
			这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时,
			就不会生成对应的Example类了. -->
	        <table tableName="t_apply_person" domainObjectName="ApplyPerson" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >  </table>  
    </context>  
</generatorConfiguration> 

然后执行下图操作

mybatis-generator:generate




Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐