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>
    <!--导入属性配置 在pom.xml文件中导入jar包之后就需要在进行导入了-->
    <!--<properties resource="datasource.properties"></properties>-->
    <!--指定特定数据库的jdbc驱动jar包的位置 尽量使用和驱动一样的jar包-->
    <classPathEntry location="${db.driverLocation}"/>
    <!--targetRuntime用MyBatis3, 也就是默认的, 其他不用管  defaultModelType introspectedColumnImpl 默认即可
        defaultModelType:指定生成对象的样式
            1,conditional(默认值):类似hierarchical;
            2,flat:所有内容(主键,blob)等全部生成在一个对象中;
            3,hierarchical:主键生成一个XXKey对象(key class),Blob等单独生成一个对象,其他简单属性在一个对象中(record class)
        targetRuntime:
            1,MyBatis3:默认的值,生成基于MyBatis3.x以上版本的内容,包括XXXBySample;
            2,MyBatis3Simple:类似MyBatis3,只是不生成XXXBySample;
         introspectedColumnImpl:类全限定名,用于扩展MBG
    -->
    <context id="default" targetRuntime="MyBatis3">

	
	<!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;
    		一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖
	-->
	<property name="autoDelimitKeywords" value="false"/>
	<!-- 生成的Java文件的编码 -->
	<property name="javaFileEncoding" value="UTF-8"/>
	<!-- 格式化java代码 -->
	<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
	<!-- 格式化XML代码 -->
	<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>

	<!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
	<property name="beginningDelimiter" value="`"/>
	<property name="endingDelimiter" value="`"/>
        <!-- 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>
            <!--当数据类型为DECIMAL或者NUMERIC的时候, 如果是true的话则总是使用java.math.BigDecimal-->
            <!--以下是false, 即默认值的情况-->            
	    <!--如果有小数或者decimal长度大于18, Java类型为BigDecimal--> 
           <!--如果没有小数, 以及decimal长度为10至18, Java类型为Long--> 
           <!--如果没有小数, 以及decimal长度为5至9, Java类型为Integer--> 
           <!--如果没有小数, 以及decimal长度少于5, Java类型为Short-->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- Model模型生成器(必要),用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径 如果目录不存在,MBG不会自动建目录        --> 
       <javaModelGenerator targetPackage="com.xuzhao.pojo" targetProject="./src/main/java">
            <!-- 是否允许子包,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false 即targetPackage.schemaName.tableName --> 
           <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加 构造函数 如果有指定immutable元素的话这个会被忽略-->
            <property name="constructorBased" value="true"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
            <!-- 设置一个根对象,如果设置了这个根对象,那么生成的keyClass或者recordClass会继承这个类;在Table的rootClass属性中可以覆盖该选项
                注意:如果在key class或者record class中有root class相同的属性,MBG就不会重新生成这些属性了,包括:
                            1,属性名相同,类型相同,有相同的getter/setter方法;每个entity都继承这个bean-->
            <property name="rootClass" value="com.entity.base.BasicEntity"/>
        </javaModelGenerator>
        <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
            <!--在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
            <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.xuzhao.dao" targetProject="./src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!--每个Mapper所继承的接口 但是MBG只负责生成,不负责检查 
           <property name="rootInterface" value="com.github.prontera.Mapper"/> 
           -->        
</javaClientGenerator>
        <!-- 选择一个table来生成相关文件,可以有一个或多个table,必须要有table元素
             选择的table会生成一下文件:
             1,SQL map文件
             2,生成一个主键类;
             3,除了BLOB和主键的其他字段的类;
             4,包含BLOB的类;
             5,一个用户生成动态查询的条件类(selectByExample, deleteByExample),可选;
             6,Mapper接口(可选)             tableName(必要):要生成对象的表名;
             注意:大小写敏感问题。正常情况下,MBG会自动的去识别数据库标识符的大小写敏感度,在一般情况下,MBG会
                 根据设置的schema,catalog或tablename去查询数据表,按照下面的流程:
                 1,如果schema,catalog或tablename中有空格,那么设置的是什么格式,就精确的使用指定的大小写格式去查询;
                 2,否则,如果数据库的标识符使用大写的,那么MBG自动把表名变成大写再查找;
                 3,否则,如果数据库的标识符使用小写的,那么MBG自动把表名变成小写再查找;
                 4,否则,使用指定的大小写格式查询;
             另外的,如果在创建表的时候,使用的""把数据库对象规定大小写,就算数据库标识符是使用的大写,在这种情况下也会使用给定的大小写来创建表名;
             这个时候,请设置delimitIdentifiers="true"即可保留大小写格式;
             可选:
             1,schema:数据库的schema;
             2,catalog:数据库的catalog;
             3,alias:为数据表设置的别名,如果设置了alias,那么生成的所有的SELECT SQL语句中,列名会变成:alias_actualColumnName
             4,domainObjectName:生成的domain类的名字,如果不设置,直接使用表名作为domain类的名字;可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面;
             5,enableInsert(默认true):指定是否生成insert语句;
             6,enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get);
             7,enableSelectByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询语句;
             8,enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update);
             9,enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete);
             10,enableDeleteByExample(默认true):MyBatis3Simple为false,指定是否生成动态删除语句;
             11,enableCountByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询总条数语句(用于分页的总条数查询);
             12,enableUpdateByExample(默认true):MyBatis3Simple为false,指定是否生成动态修改语句(只修改对象中不为空的属性);
             13,modelType:参考context元素的defaultModelType,相当于覆盖;
             14,delimitIdentifiers:参考tableName的解释,注意,默认的delimitIdentifiers是双引号,如果类似MYSQL这样的数据库,使用的是`(反引号,那么还需要设置context的beginningDelimiter和endingDelimiter属性) 
            15,delimitAllColumns:设置是否所有生成的SQL中的列名都使用标识符引起来。默认为false,delimitIdentifiers参考context的属性
             注意,table里面很多参数都是对javaModelGenerator,context等元素的默认属性的一个复写;          -->
        <table tableName="xxxx" domainObjectName="xxxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="xxxx" domainObjectName="xxxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="xxxx" domainObjectName="xxxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="xxxx" domainObjectName="xxxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="xxxx" domainObjectName="xxxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="xxxx" domainObjectName="xxxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="xxxx" domainObjectName="xxxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="xxxx" domainObjectName="xxxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <columnOverride column="xxx" jdbcType="VARCHAR" />
            <columnOverride column="xxx" jdbcType="VARCHAR" />
        </table>
        <table tableName="xxxxx" domainObjectName="xxxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context></generatorConfiguration>

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pom.xml  mybatis-generator插件配置
<plugin>
  <!--
          用maven mybatis插件
          如果不在plugin里面添加依赖包得引用的话,会找不到相关得jar包,
          在plugin外部得jar包,他不会去找到并执行,
          所以要把plugin运行依赖得jar配置都放在里面
   -->
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-maven-plugin</artifactId>
  <version>1.3.5</version>
  <configuration>
    <verbose>true</verbose>
    <!--配置文件的路径-->
    <configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile>
    <overwrite>true</overwrite>
  </configuration>
  <executions>
    <execution>
      <id>Generate MyBatis Artifacts</id>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <!-- 引用依赖库的版本 -->
      <version>5.1.38</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.5</version>
    </dependency>
  </dependencies>
</plugin>

运行方式:
1.使用maven插件的方式在控制台运行
 mvn mybatis-generator:generate
2.使用jar的方式运行
java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml
3.使用IDE工具的可视化进行运行



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值