mybatis之mybatis-generator

来源:互联网 发布:hydra linux 编辑:程序博客网 时间:2024/05/30 04:13

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="/Program Files/IBM/SQLLIB/java/db2java.zip" />  <context id="DB2Tables" targetRuntime="MyBatis3">    <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"        connectionURL="jdbc:db2:TEST"        userId="db2admin"        password="db2admin">    </jdbcConnection>    <javaTypeResolver >      <property name="forceBigDecimals" value="false" />    </javaTypeResolver>    <javaModelGenerator targetPackage="test.model" targetProject="\MBGTestProject\src">      <property name="enableSubPackages" value="true" />      <property name="trimStrings" value="true" />    </javaModelGenerator>    <sqlMapGenerator targetPackage="test.xml"  targetProject="\MBGTestProject\src">      <property name="enableSubPackages" value="true" />    </sqlMapGenerator>    <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao"  targetProject="\MBGTestProject\src">      <property name="enableSubPackages" value="true" />    </javaClientGenerator>    <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >      <property name="useActualColumnNames" value="true"/>      <generatedKey column="ID" sqlStatement="DB2" identity="true" />      <columnOverride column="DATE_FIELD" property="startDate" />      <ignoreColumn column="FRED" />      <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />    </table>  </context></generatorConfiguration>

<generatorConfiguration>是MyBatis Generator配置文件的的根元素;本身不具备任何属性;有三个子元素:
  • <properties>(0 or 1)
  • <classPathEntry>(0~N)
  • <context>(0~N)
<generatorConfiguration>包含以下内容:
<!DOCTYPE generatorConfiguration PUBLIC  "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<properties>元素只能存在一个或者没有,它是导入数据库属性配置的标签;例如:
<properties resource="datasource.properties"></properties>

 


 

<classPathEntry>元素用于指定特定数据库的jdbc驱动jar包的位置;无子元素;例如:

<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />

 


 

<context>元素用于生成一组对象指定的环境。子元素用于指定要连接的数据库、要生成的对象的类型以及要内省的表

  • 必须的属性:id
  • 可选的属性:
    • defaultModelType(设置默认生成的模型类型)
      • conditional(默认值)
      • flat
      • hierarchical
    • targetRuntime(此属性用于指定生成的代码运行的目标)
      • MyBatis3(默认值)
      • MyBatis3Simple
      • Ibatis2Java2
      • Ibatis2Java5
    • introspectedColumnlmpl
  • 子元素
    • <property>(0~N)
    • <plugin>(0~N)
    • <commentGenerator>(0 or 1)
    • <connection>
    • <jdbcConnection>
    • <javaTypeResolver>(0 or 1)
    • <javaModelGenerator>(1 Required)
    • <sqlMapGenerator>(0 or 1)
    • <javaClienGenerator>(0 or 1)
    • <table>(1~N)

 


 

<property>元素用于指定许多其他元素的属性;无属性;无子元素;例如:

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

 


 

<plugin>元素用于定义(引用)一个插件

 


 

<commentGenerator>元素用于在在创建class时,对注释进行控制;

支持的属性:

  • suppressAllComments:生成注释
    • false(默认)
    • true
  • suppressDate:生成的注释包含时间戳
    • false(默认)
    • true
  • addRemarkComments:生成的注释包含数据库表
    • false(默认)
    • true
  • dateFormat:将日期写入生成的注释时使用的日期的格式,默认情况下,日期字符串将从tostring()转化为指定格式的方法

例如:

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

 


 

<jdbcConnection>元素用于jdbc的数据库连接;

  • 必需的属性
    • driverClass:JDBC驱动的类名
    • connectionURL:JDBC连接的URL来访问数据库
  • 可选的属性
    • userId:连接数据库用的用户名
    • password:连接数据库用的密码

例如:

<jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"    connectionURL="jdbc:db2:MBGTEST"    userId="db2admin"    password="db2admin"></jdbcConnection>

 


 

<javaTypeResolver>元素用于在数据库类型和java类型之间的转换控制;(可选)

支持的属性:forceBigDecimals

  • false
  • true

例如:

 

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

 


 

<javaModelGenerator>元素用于定义的java模式发生器性能。java模型生成器生成主键类,记录类,并通过实例类的反思表匹配查询。此元素是一个必需的<context>元素的子元素;

  • 必须的属性
    • targetPackage:生成的类所放置的包
    • targetProject:生成对象的目标项目和源文件夹
  • 支持的属性
    • constructorBased:为每一个生成的类创建一个构造方法
    • enableSubPackages:是否准许子包
    • immutable:建立的Model对象是否不可改变(是否生成setter方法),默认为false
    • rootClass:指定所有已生成的java模型对象的根类
    • trimStrings:是否对类CHAR类型的列的数据进行trim操作

例如:

<javaModelGenerator targetPackage="test.model" targetProject="\MyProject\src">  <property name="enableSubPackages" value="true" />  <property name="trimStrings" value="true" /></javaModelGenerator>

 


 

 <sqlMapGenerator>元素用于定义SQLMap发生器性能;

  • 必需的属性
    • targetPackage:生成的类所放置的包
    • targetProject:生成对象的目标项目和源文件夹
  • 支持的属性
    • enableSuPackages:是否再生成一层package;默认为false(不生成);

例如:

<sqlMapGenerator targetPackage="test.model" targetProject="\MyProject\src">  <property name="enableSubPackages" value="true" /></sqlMapGenerator>

 


 

 <javaClientGenerator>元素用于定义java客户端的发生器性能;

  • 必需的属性
    • type:选择预定义得客户端代码生成器
      • MyBatis3
      • MyBatis3Simple
      • Ibatis2Java or Ibatis2Java5
    • targetPackage:生成实体类存放的包名,一般就是放在该包下
    • targetProject:指定目标项目路径,实现类就会生成在这个包中
  • 可选的属性
    • implementationPackage:如果指定的话,实现类将放在这个包中
  • 支持的属性(不常用,自己查看文档)
    • enableSubPackages
    • exampleMethodVisbility
    • methodNameCalculator
    • rootInterface:可以为所有生成的接口添加一个父接口,MBG只负责生成,不负责检查
    • userLeacyBuilder

例如:

<javaClientGenerator targetPackage="test.model" targetProject="\MyProject\src" type="XMLMAPPER">  <property name="enableSubPackages" value="true" /></javaClientGenerator>

 


 

 <table>元素用于选择数据库中的数据表进行处理

  • 必需的属性
    • tableName:数据库表的名称
  • 可选的属性
    • schema:数据库的schema,可以使用SQL通配符匹配。如果设置了该值,生成的SQL的表名会变成如:schema.tableName的形式
    • catalog:数据库的catalog,如果设置了该值,生成的SQL的表名会变成如:catalog.tableName的形式
    • alias:添加数据库表的别名
    • domainObjectName:生成对象的基本名称,如果没有指定,MGB会自动根据表名来生成名称
    • enableInsert:是否生成insert语句,默认为true
    • enableSelectByPriMaryKey:是否生成按照主键查询对象的语句(getBy or get),默认为true
    • enableSelectByExample:是否生成动态查询语句,默认为true(MyBatis3Simple为false)
    • enableUpdataByPrimaryKey:是否生成按照主键修改对象的语句,默认为true
    • enableDeleteByPrimaryKey:是否生成按照主键删除对象的语句,默认为true
    • enableDeleteByExample:是否生成动态删除语句,默认为true(MyBatis3Simple为false)
    • enableCountByExample:是否生成生成动态查询总条数语句(用于分页的总条数查询),默认为true(MyBatis3Simple为false)
    • enableUpdataByExample:是否生成动态修改语句(只修改对象中不为空的属性),默认为true
    • seletByPrimaryKeyQueryId:DAO跟踪工具会用到
    • selectByExampleQueryId:DAO跟踪工具会用到
    • modelType:与<context>中的<defaultModelType>元素一样,可以覆盖掉<defaultModelType>元素
    • escapeWildcards:查询时是否对schema和表名中的(‘_’ and ‘%’)进行转义,默认为false
    • delimitIdentifiers:是否给标识符增加分隔符,默认为false(如果schema和catlog或tableName为空,默认为true)
    • delimitAllColumns:是否所有生成的SQL中的列名都使用标识符引起来,默认为false
  • 支持的属性
    • constructorBased:为每一个生成的类创建一个构造方法
    • ignoreQualifiersAtRuntime:默认为false,如果为true,在生成的SQL中,table名字不会加上catlog或schema
    • immutable:建立的Model对象是否不可改变(是否生成setter方法)
    • modelOnly:此属性用于配置是否为表只生成实体类,默认为false
    • rootClass:指定所有已生成的java模型对象的根类
    • rootInterface:可以为所有生成的接口添加一个父接口,MBG只负责生成,不负责检查
    • runtimeCatlog:运行时的catlog,当生成表和运行环境的表的tableName不一样的时候可以使用该属性进行配置
    • runtimeSchema:运行时的schema,当生成表和运行环境的表的schema不一样的时候可以使用改属性进行配置
    • runtimeTableName:运行时的tableName,当生成表和运行环境的表的tableName不一样的时候可以使用该属性进行配置
    • selectAllOrderByClause:该属性值会追加到selectAll方法后的SQL中,会直接跟order by拼接后添加到SQL末尾(MyBatis3Simple时可用)
    • useActualColumnNames:如果设置为true,生成的model类会直接使用column本身的名字,而不会再使用驼峰命名方法;
    • useColumnIndexes:如果是true,MBG生成resultMaps的时候会使用列的索引,而不是结果中列名的顺序
    • useCompoundPropertyNames:如果true,那么MBG将使用由contatenating列名和列reparks生成属性名称
  • 子元素
    • <generatedKey>:自动生成主键的属性
      • column:主键的列名
      • jdbcType:jdbc类型

 例如:

<table tableName="MYTABLE" schema="MYSCHEMA">  <ignoreColumn column="fred"/>  <columnOverride column="BEG_DATE" property="startDate"/></table>

 


 

总结:

  上面的内容就是我根据MBG官方文档和一些查阅到的资料汇集到一起的,因为我也是正在学习这个,可能有些地方理解的不那么深,如看到错误请指出来,我会继续修改,下面是我最近写的项目用到的MBG配置,希望对需要的人有帮助:

datasource.properties

db.driverLocation = D:\\Project\\Javaweb\\mysql-connector-java-5.1.6-bin.jardb.driverClassName = com.mysql.jdbc.Driverdb.url = jdbc:mysql://127.0.0.1:3306/mmall?characterEncoding=utf-8db.username = rootdb.password =

 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="datasource.properties"></properties>    <!--指定特定数据库的jdbc驱动jar包的位置-->    <classPathEntry location="${db.driverLocation}"/>    <context id="default" targetRuntime="MyBatis3">        <!-- optional,旨在创建class时,对注释进行控制 -->        <commentGenerator>            <property name="suppressDate" value="true"/>            <property name="suppressAllComments" value="true"/>        </commentGenerator>        <!--jdbc的数据库连接 -->        <jdbcConnection                driverClass="${db.driverClassName}"                connectionURL="${db.url}"                userId="${db.username}"                password="${db.password}">        </jdbcConnection>        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->        <javaTypeResolver>            <property name="forceBigDecimals" value="false"/>        </javaTypeResolver>        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类            targetPackage     指定生成的model生成所在的包名            targetProject     指定在该项目下所在的路径        -->        <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->        <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java">            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->            <property name="enableSubPackages" value="false"/>            <!-- 是否对model添加 构造函数 -->            <property name="constructorBased" value="true"/>            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->            <property name="trimStrings" value="true"/>            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->            <property name="immutable" value="false"/>        </javaModelGenerator>        <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->        <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->        <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">            <property name="enableSubPackages" value="false"/>        </sqlMapGenerator>        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口        -->        <!-- targetPackage:mapper接口dao生成的位置 -->        <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java">            <!-- enableSubPackages:是否让schema作为包的后缀 -->            <property name="enableSubPackages" value="false" />        </javaClientGenerator>        <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>        <table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>        <table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>        <table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>        <table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>        <table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>        <table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>        <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">            <columnOverride column="detail" jdbcType="VARCHAR" />            <columnOverride column="sub_images" jdbcType="VARCHAR" />        </table>        <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>        <!-- geelynote mybatis插件的搭建 -->    </context></generatorConfiguration>
原创粉丝点击