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="C:\Users\Zhu\.m2\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" />

    <context id="MysqlTables" targetRuntime="MyBatis3">

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

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/easy_spring" userId="root"
            password="123456">
        </jdbcConnection>

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

        <javaModelGenerator targetPackage="com.zhu.easyspring.entity"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.zhu.easyspring.dao"
            targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.zhu.easyspring.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="user" domainObjectName="User">
        </table>
        <table tableName="role" domainObjectName="Role">
        </table>
        <table tableName="permission" domainObjectName="Permission">
        </table>

    </context>
</generatorConfiguration>

首先贴上一份Mybatis Generator的示例配置,心急的童鞋可以直接拿去用,有几个注意点需要改一下:

  1. classPathEntry:用于指定MyBatis Generator (MBG)运行的classpath路径,MBG在下面几种情况下会从这个路径来载入类:载入JDBC驱动、载入JavaModelGenerator的根类来检查重写的方法。必须要有localtion属性,所以这里只需要将location改成你本机的JDBC驱动的jar包所在的目录即可。
  2. 下一个需要修改的节点是jdbcConnection,这个就设置一下连接数据库的几个基本属性即可:URLusernamepassword
  3. 需要修改javaModelGeneratorsqlMapGeneratorjavaClientGenerator,这三个节点是分别用来生成实体类+Example类、Mapper接口和映射文件的,这里只需要修改targetPackage替换成你自己的即可
  4. 最后的话就是配置table节点来具体写出你需要生成实体的数据库表,这里关于table节点的配置是相当多,这里就写几个我最近用到的一些:
        <table tableName="t_address" domainObjectName="Address" modelType="flat" >
            <columnOverride column="default" property="isDefault" delimitedColumnName="true"></columnOverride>
        </table>

如上是对t_address表来生成实体,其中table节点的常用属性有:

  • tableName指定了表名
  • domainObjectName指定了实体类的名称
  • modelType指定了生成实体的类型,这里modelType总共分为三种:

    • conditional:类似hierarchical,如果分离的类仅有一个属性那么就不分离了,比如某张表只有一个主键,那么就不会专门生成一个主键类,只会生成一个类
    • flat:一张表只会生成唯一的一个类,其中包含了所有的字段
    • hierarchical:如果表有主键的话,那么首先会生成一个主键类,另外还会生成一个所有BLOB属性的类,最后还有一个类包含了余下的字段,并且这三个类会有一个合理的层次。

另外table节点还可以包含子节点:

  • columnOverride:重写某个字段的名字,比如数据中用到了某个java的保留字,假设是class,那么就必须重写该字段映射到实体类中的属性名,那么就可以这么写
<columnOverride column="class" property="cls"></columnOverride>
  • 而对于数据库的保留字,比如某张表的字段名存在order,那么在生成sql的话,该字段order会被数据库认为是保留字order by的order,这时我们的做法通常是在该字段上加““符号,在用MGB生成时我们可以这么写
<columnOverride column="order" property="order" delimitedColumnName="true"></columnOverride>

delimitedColumnName这个属性就会告诉MGB在生成映射文件的时候会在该字段上加上分隔符,而对于分隔符的设置具体是在context节点下,有这么两个属性:

<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>

这样基本上可以满足一般的需求了,如果还有额外的需求,请移步MBG官网的配置参考文档

MyBatis Generator(MBG)是一个自动生成MyBatis框架代码的工具。MBG通过读取数据库表结构信息,生成基本的Mapper、Model和Example代码。 在使用MBG时,可以通过配置文件指定生成的代码的格式、包名、注释等信息。MBG支持通过插件扩展生成的代码的功能,例如自动生成分页查询代码、生成基于XML的批量操作代码等。 以下是MBG配置文件中的插件配置示例: ``` <generatorConfiguration> <!-- 插件配置 --> <context id="mysql" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" /> <plugin type="org.mybatis.generator.plugins.PaginationPlugin"> <property name="pageLimit" value="10" /> </plugin> <plugin type="org.mybatis.generator.plugins.BatchInsertPlugin" /> <plugin type="org.mybatis.generator.plugins.BatchUpdatePlugin" /> <plugin type="org.mybatis.generator.plugins.BatchDeletePlugin" /> <!-- 其他配置 --> </context> </generatorConfiguration> ``` 上述配置中,配置了如下插件: - SerializablePlugin:自动生成Serializable接口实现类,用于支持缓存等特性。 - RowBoundsPlugin:自动生成基于RowBounds的分页查询接口方法。 - PaginationPlugin:自动生成基于MySQL的分页查询接口方法,并设置每页显示条数为10。 - BatchInsertPlugin:自动生成批量插入数据的接口方法。 - BatchUpdatePlugin:自动生成批量更新数据的接口方法。 - BatchDeletePlugin:自动生成批量删除数据的接口方法。 通过配置插件,可以快速生成常用的代码,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值